Sockets and rails

1,933 views

Published on

Using websockets with rails, and also using node servers with rails.

Published in: Technology, Education
  • Be the first to comment

  • Be the first to like this

Sockets and rails

  1. 1. Rails and Sockets
  2. 2. Websocket Rails Gem https://github.com/websocket-rails/websocket-rails
  3. 3. Steps ● Add Gem “websocket-rails” ● bundle install ● rails g websocket_rails:install ● config.middleware.delete Rack::Lock ● rails server
  4. 4. B Client var dispatcher = new WebSocketRails('localhost:3000/websocket'); Initialize the socket File: application.js
  5. 5. Bind for changes channel = dispatcher.subscribe('posts'); channel.bind('new', function(data) { console.log('a new post about '+data.title+' arrived!'); }); File: application.js
  6. 6. Server rails g scaffold posts title:string description:string In some_action (create) WebsocketRails[:posts].trigger 'new', latest_post File: posts_controller create action
  7. 7. Events from clients
  8. 8. Event Router - Client var success = function(res) { console.log("Created: " + res.post.title); } var failure = function(res) { console.log("Failed to create post: " + res) } dispatcher.trigger(‘create_event', post, success, failure); File: application.js
  9. 9. Event Router - Server WebsocketRails::EventMap.describe do # using a Hash to specify the target subscribe :create_event, 'sockets#create_from_socket' end File: config/events.rb
  10. 10. class SocketsController < WebsocketRails::BaseController def create_from_socket post = Post.new message if post.save trigger_success post: post else trigger_failure post: post.errors end end end Event Router - Server File: sockets_controller
  11. 11. ● sinatra-websocket ● webmachine-ruby ● faye-websocket ● tubesock Few more!
  12. 12. Rails and Node http://liamkaufman.com/blog/2013/02/27/adding-real-time-to-a- restful-rails-app/
  13. 13. Basic Flow
  14. 14. Node and Redis Prerequisites
  15. 15. Setting up Rails Server gem 'redis' gem 'socket.io-rails' File: gemfile
  16. 16. Initializing Redis on Server $redis = Redis.new(:host => 'localhost', :port=> 6379) File: initializers/redis_init.rb (manually_created)
  17. 17. $redis.publish 'rt-change', @post.to_json Publishing from rails to a channel File: posts_controller.rb
  18. 18. Setting up Node Server Listen and subscribe to changes in rt-change (rails) channel var io = require('socket.io').listen(5001), redis = require('redis').createClient(); redis.subscribe('rt-change'); io.on('connection', function(soc){ console.log(soc.id); }); }); File: app/<folder>/<file-name>.js
  19. 19. Setting up Node Server Publishing changes to client channel redis.on('message', function(channel, message){ io.sockets.emit('client-channel',JSON.parse(message)); }); File: app/<folder>/<file-name>.js
  20. 20. Setting up Rails Client Include //= require socket.io in application.js socket = io.connect("http://0.0.0.0:5001"); socket.on("client-channel", function(message){ alert(message); }); File: application.js
  21. 21. Rails - rails s Node - node <folder>/<file-name>.js Start the servers File: app/<folder>/<file-name>.js
  22. 22. redis_pub = require('redis').createClient(); redis_pub.publish('my_callback', JSON.parse(message)); Callback from node File: app/<folder>/<file-name>.js
  23. 23. $redis.subscribe("my_callback") do |on| on.message do |channel, message| puts message.to_s end end A task to listen on callbacks from node File: app/lib/tasks/<file-name>.rake
  24. 24. Load distribution. Easy integration with APNS,MPNS and GCM services. Also has queuing systems. Advantages
  25. 25. Few ways to manage node server Forever - https://github.com/nodejitsu/forever PM2 - https://github.com/Unitech/pm2
  26. 26. Questions?
  27. 27. https://github.com/shrikanthkr/wsrails https://github.com/shrikanthkr/redis-socketio Samples
  28. 28. Thank You!

×