Rails Refresh Page For All Users On Command

六月ゝ 毕业季﹏ 提交于 2020-01-03 06:45:08

问题


I'm looking to create a Rails application that will be used as programme that outlines the running order of a service / event for my local church. The basic concept is that the technical team who control the sound, lighting, visuals etc and the volunteers can log in to the application and view the running order which will contain all the elements of the service including timings etc.

I want a producer to be able to control the running order, so that he / she has basically a 'Next' / 'Back' button and can cycle through each element of the show. This way the team all know exactly what stage of the service they are on and what they need to do.

My question: How can I make it so that when the producer presses the 'Next' button, the application refreshes / updates for all users and they see the latest element? Everyone needs to be viewing the same information - and quickly too.

Any help appreciated!

Thanks

Lee


回答1:


You can use either ajax which poll to your controller#action on constant intervals and get the updated views.

Or, you can go for web sockets which will automatically poll for the changes in your browser. And when there is a change in your server you will push out the changes to client browsers. see https://github.com/html5hacks/html5hacks/blob/master/source/_posts/2013-04-21-push-notifications-to-the-browser-with-server-sent-events.markdown or you can google it yourself




回答2:


You could either let the application poll every 5 seconds or something to check whether something has changed.

Another option would be to use html push notifications.

This way your server could send a notification to everyone on the page indicating something has changed. The javascript on the client-side would then handle such a notification. It would look something like this.

if (!!window.EventSource) {
  var source = new EventSource('my_stream');
}

source.addEventListener('message', function(e) {
  if(e.data.page_changed) {
    // Update the page accordingly
  }
}, false);

Note that push notifications are part of html5 and is not (fully) supported by all browsers.




回答3:


This would be a perfect use of socket.io (http://socket.io/). A client can tell the server to broadcast a refresh command to every other client.




回答4:


Well, you have assigned jquery and ajax tags to your question, so I assume you understand that Rails is just a backend solution and can not handle the client-side updates itself (at least, prior to Rails 4). In case your task is just that simple as you described and relies that much on instant updates for all users, I would recommend switching to some javascript platform as they were invented for this kind of task. The most popular are:

  1. Meteor.js
  2. Node.js

Nevertheless, the same thing can still be done with rails backend. Here you've also got plenty of approaches to pick from:

  1. AJAX polling
  2. AJAX long-polling
  3. Websockets
  4. Server-side events

Check out a great topic about each of these methods: What are Long-Polling, Websockets, Server-Sent Events (SSE) and Comet?. The examples of implementations are easily found over the web as well.

Most of these ways would require some client-side coding, however, the amount of it could be reduced with a new feature of Rails 4: Server-Side events using ActionController::Live and ActionController::Streaming. The following resources may be helpful:

  1. Action Controller Overview: Live Streaming of Arbitrary Data
  2. ActionController::Live screencast
  3. Sample Implementation of Action Controller Live


来源:https://stackoverflow.com/questions/24206098/rails-refresh-page-for-all-users-on-command

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!