Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Streaming Files to the Browser with Ruby

I'm trying to find an efficient way to watch the server log on a web page. I don't mind building a gem; I just want to work out the best way to do it.

Is there a way to keep a stream open to a file with Ruby (not any frameworks) and to the browser? Or will it have to be done by polling the file every x seconds?

like image 415
Pravin Mishra Avatar asked Mar 24 '23 13:03

Pravin Mishra


2 Answers

This is a frequent question. You can't really stream to a browser "easily". In the end you will have to have some small javascript bit on the client side to handle and display the incoming data, and a separate server process on the server end to read the file and pump the data back to the client.

Most browsers today support WebSockets, which allow for bidirectional direct communications between the browser client and any service on the server end. So that's what I would use.

Before WebSockets you had all kinds of hacks using AJAX long polling or other workarounds. But today I'd say WebSockets is the way to go.

There are several WebSocket libraries for ruby, which makes it easier to set up something like this both on the server and client end:

Best Ruby on Rails WebSocket tool
Google - Ruby Websockets

Also for inspiration you might want to take a look at a node.js implementation of what you are trying to do:

frontail(1) – tail -F output in browser

Which uses Socket.IO for its communications implementation.

like image 161
Casper Avatar answered Apr 06 '23 12:04

Casper


Here is the working solution, which i achieved using cramp framework.

Streaming Files to the Browser with Ruby

This is first step to in, a lot of work need to do....

Source code is available on github.

like image 43
Pravin Mishra Avatar answered Apr 06 '23 14:04

Pravin Mishra