Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Real Time with Rails 5

199 views

Published on

Yet another chat app with Action Cable - Rails 5.

Presented at GURU Sorocaba meetup.

Published in: Software
  • Be the first to comment

  • Be the first to like this

Real Time with Rails 5

  1. 1. real time with rails 5
  2. 2. lucas renan
  3. 3. guru sorocaba
  4. 4. action cable seamlessly integrates websockets with the rest of your rails app
  5. 5. yet another chat app
  6. 6. !
  7. 7. app/channels/rooms_channel.rb class RoomsChannel < ApplicationCable::Channel def subscribed stream_from 'room_sorocaba_channel' # can be dynamic end def unsubscribed # Any cleanup needed when channel is unsubscribed end def send_message(data) ActionCable.server.broadcast('room_sorocaba_channel', data) end end
  8. 8. app/assets/javascripts/cable.js (function() { this.App || (this.App = {}); App.cable = ActionCable.createConsumer(); }).call(this);
  9. 9. app/assets/javascripts/channels/rooms.js App.rooms = App.cable.subscriptions.create("RoomsChannel", { connected: function() { // Called when the subscription is ready for use on the server }, disconnected: function() { // Called when the subscription has been terminated by the server }, received: function(data) { // Called when there's incoming data on the websocket for this channel }, send_message: function() { return this.perform('send_message'); } });
  10. 10. config/routes.rb # Serve websocket cable requests in-process mount ActionCable.server => '/cable'
  11. 11. config/cable.yml development: adapter: async test: adapter: async production: adapter: redis url: redis://localhost:6379/1
  12. 12. config/environments/production.rb config.action_cable.url = ENV['CABLE_URL'] config.action_cable.allowed_request_origins = ENV['APP_URL']
  13. 13. app/views/layouts/application.html.erb <%= action_cable_meta_tag %>
  14. 14. https://github.com/lucasrenan/lucas-chat
  15. 15. heroku tutorial https://blog.heroku.com/ realtimerailsimplementingwebsocketsinrails5withacti oncable
  16. 16. https://lucas-chat-rails5.herokuapp.com/chat
  17. 17. thanks :)

×