sockets and rails

28
Rails and Sockets

Upload: shrikanth-rajarajan

Post on 15-Jan-2015

609 views

Category:

Technology


1 download

DESCRIPTION

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

TRANSCRIPT

Page 1: Sockets and rails

Rails and Sockets

Page 2: Sockets and rails

Websocket Rails Gem

https://github.com/websocket-rails/websocket-rails

Page 3: Sockets and rails

Steps● Add Gem “websocket-rails”● bundle install● rails g websocket_rails:install● config.middleware.delete Rack::Lock● rails server

Page 4: Sockets and rails

B

Client

var dispatcher = new WebSocketRails('localhost:3000/websocket');

Initialize the socket

File: application.js

Page 5: Sockets and rails

Bind for changes

channel = dispatcher.subscribe('posts');channel.bind('new', function(data) { console.log('a new post about '+data.title+' arrived!');});

File: application.js

Page 6: Sockets and rails

Server

rails g scaffold posts title:string description:string

In some_action (create) WebsocketRails[:posts].trigger 'new', latest_post

File: posts_controller create action

Page 7: Sockets and rails

Events from clients

Page 8: Sockets and rails

Event Router - Clientvar 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

Page 9: Sockets and rails

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

Page 10: Sockets and rails

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

Page 13: Sockets and rails

Basic Flow

Page 14: Sockets and rails

Node and Redis

Prerequisites

Page 15: Sockets and rails

Setting up Rails Server

gem 'redis'gem 'socket.io-rails'

File: gemfile

Page 16: Sockets and rails

Initializing Redis on Server

$redis = Redis.new(:host => 'localhost', :port=> 6379)

File: initializers/redis_init.rb (manually_created)

Page 17: Sockets and rails

$redis.publish 'rt-change', @post.to_json

Publishing from rails to a channel

File: posts_controller.rb

Page 18: Sockets and rails

Setting up Node ServerListen 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

Page 19: Sockets and rails

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

Page 20: Sockets and rails

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

Page 21: Sockets and rails

Rails - rails sNode - node <folder>/<file-name>.js

Start the servers

File: app/<folder>/<file-name>.js

Page 22: Sockets and rails

redis_pub = require('redis').createClient();redis_pub.publish('my_callback', JSON.parse(message));

Callback from node

File: app/<folder>/<file-name>.js

Page 23: Sockets and rails

$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

Page 24: Sockets and rails

Load distribution.

Easy integration with APNS,MPNS and GCM services.

Also has queuing systems.

Advantages

Page 25: Sockets and rails

Few ways to manage node server

Forever - https://github.com/nodejitsu/forever

PM2 - https://github.com/Unitech/pm2

Page 26: Sockets and rails

Questions?

Page 28: Sockets and rails

Thank You!