real time with rails 5

22
real time with rails 5

Upload: lucas-renan

Post on 12-Apr-2017

70 views

Category:

Software


0 download

TRANSCRIPT

Page 1: Real Time with Rails 5

real time with rails 5

Page 2: Real Time with Rails 5

lucas renan

Page 3: Real Time with Rails 5
Page 4: Real Time with Rails 5

guru sorocaba

Page 5: Real Time with Rails 5
Page 6: Real Time with Rails 5

action cableseamlessly integrates websockets with the rest of

your rails app

Page 7: Real Time with Rails 5
Page 8: Real Time with Rails 5
Page 9: Real Time with Rails 5
Page 10: Real Time with Rails 5

yet another chat app

Page 11: Real Time with Rails 5

!

Page 12: Real Time with Rails 5

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) endend

Page 13: Real Time with Rails 5

app/assets/javascripts/cable.js

(function() { this.App || (this.App = {});

App.cable = ActionCable.createConsumer();

}).call(this);

Page 14: Real Time with Rails 5

app/assets/javascripts/channels/rooms.jsApp.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'); }});

Page 15: Real Time with Rails 5

config/routes.rb

# Serve websocket cable requests in-process mount ActionCable.server => '/cable'

Page 16: Real Time with Rails 5

config/cable.yml

development: adapter: async

test: adapter: async

production: adapter: redis url: redis://localhost:6379/1

Page 17: Real Time with Rails 5

config/environments/production.rb

config.action_cable.url = ENV['CABLE_URL'] config.action_cable.allowed_request_origins = ENV['APP_URL']

Page 18: Real Time with Rails 5

app/views/layouts/application.html.erb

<%= action_cable_meta_tag %>

Page 19: Real Time with Rails 5

https://github.com/lucasrenan/lucas-chat

Page 21: Real Time with Rails 5

https://lucas-chat-rails5.herokuapp.com/chat

Page 22: Real Time with Rails 5

thanks :)