Rails 4 & server sent events

7,696 views
7,350 views

Published on

www.thesoftwarehouse.pl

Published in: Technology
0 Comments
8 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
7,696
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
27
Comments
0
Likes
8
Embeds 0
No embeds

No notes for slide

Rails 4 & server sent events

  1. 1. Rails 4 & Server-Sent Events Piotr Karbownik
  2. 2. SSE - EventSource API Server-Sent Events (SSE) is a HTML5 standard describing how servers can initiate data transmission towards clients once an initial client connection has been established. Browsers: Firefox, Chrome, Opera, Safari
  3. 3. SSE != Websockets • SSE connections can only push data to the browser. • It’s still HTTP - no new protocol • Automatic reconnection
  4. 4. SSE - applications • Notifications • Continuous data streams • News feeds
  5. 5. SSE – on the frontend function newStream(){ var source = new EventSource('/streaming'); source.addEventListener('update', function(e) { //do stuff }); }
  6. 6. SSE – on the frontend function newStream(){ var source = new EventSource('/streaming'); source.onmessage (function(e) { //do stuff }); }
  7. 7. EventSource Object Methods onopen -When a connection to the server is opened onmessage - When a message is received onerror - When an error occurs
  8. 8. SSE – message format Sample: event: my_event data: {"username": „Peter"} event - The event's type. If this is specified, an event will be dispatched on the browser to the listener for the specified event name. data - field for the message. id - the event ID to set the EventSource object's last event ID value retry - The reconnection time to use when attempting to send the event.
  9. 9. Rails 4 - ActionController::Live Live Streaming: the ability to send partial responses to the client immediately.
  10. 10. What about the server? Server must support: • Long running requests • Live streaming • Concurrency • Examples: Puma, Rainbows, Thin
  11. 11. Config environments /development.rb: • config.cache_classes = true • config.eager_load = true database.yml • pool == server max threads • for Puma default is 16
  12. 12. Sample code - 1
  13. 13. Sample code
  14. 14. Sample code - 2
  15. 15. Summary • Easy to implement • Real time • Good for notifications • Problems with client connections amount and status.
  16. 16. …and the end Sample apps: https://github.com/cynamonium/srug- streamapp-2/ https://github.com/cynamonium/srug- streamapp-1

×