SanjiBukai
SanjiBukai

Reputation: 563

How to update a web page from requests made by another client (in rails)?

Here is my need: I have to displays some information from a web page.
The web browser is actually on the same machine (localhost).
I want the data to be updated dynamically by the server initiative.

Since HTTP protocol is actually a request/response protocol, I know that to get this functionality, the connection between the server and the client (which is local here) should be kept open in some way (Websocket, Server-Sent Events, etc..)

Yes, "realtime" is really a fashion trend nowadays and there are many frameworks out there to do this (meteor, etc...)

And indeed, it seems that Rails supports this functionnality too in addition to using Websockets (Server-Sent Events in Rails 4 and ActionCable in Rails 5)
So achieving this functionnality would not be a big deal, I guess...

Nevertheless what I really want is to trigger an update of the webpage (displayed here locally) from a request made by another client..

This picture will explain that better :

Update from another client

  1. At the beginning, the browser connects to the (local) server (green arrows).
    I guess that a thread is executed where all the session data (instance variables) are stored.
    In order to use some "realtime" mechanisms, the connection remains open and therefore the thread Y is not terminated. (I guess this is how it works)
  2. A second user is connecting (blue arrows) to the server (could be or not be the same web page) and make some actions (eg. posting a form).
    Here the response to that external client does not matter. Just an HTTP OK response is fine. But a confirmation web page could also be returned.
    But in anyway the thread X (and/or the connection) has no particular reason to be kept.

Ok, here is my question (BTW thank you for reading me thus far).
How can I echo this new data on the local web browser ?

I see 2 differents ways to do this :

Which mechanisms should I use in either method to achieve this functionnality ?
For method A, how can I exchange data between threads ?
For method B, how can I use an already opened socket ?

But which of these two methods (or another one) is actually the best way to do that?

Again thank you for reading me thus far, and sorry for my bad english.
I hope I've been clear enough to expose my need.

Upvotes: 1

Views: 1149

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230411

You are overthinking this. There is no need to think of such low-level mechanisms as threads and sockets. Most (all?) pub-sub live-update tools (ActionCable, faye, etc.) operate in terms of "channels" and "events".

So, your flow will look like this:

  1. Client A (web browser) makes a request to your server and subscribes to events from channel "client-a-events" (or something).

  2. Client B (the other browser) makes a request to your server with instructions to post an event to channel "client-a-events".

  3. Pub-sub library does its magic.

  4. Client A gets an update and updates the UI accordingly.

Check out this intro guide: Action Cable Overview.

Upvotes: 4

Related Questions