Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Websockets and Rails
Search
Shrikanth
October 02, 2014
Technology
68
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Websockets and Rails
Using websockets in Ruby on Rails. Also integrating your node server with rails.
Shrikanth
October 02, 2014
More Decks by Shrikanth
See All by Shrikanth
Ruby Introduction
shrikanthkr
0
40
Other Decks in Technology
See All in Technology
Post AI時代のリファクタリング実践記 / Refactoring in the post AI era
kaminashi
1
230
How to Design Configuration for Rails Applications
willnet
1
780
JAWSUG TOHOKU 青森支部 「LoGoチャット」のAWS移行で 乗り越えたこと
taketakekaho
0
330
Webの常識が通じないHTMLメールのダークモード
teamlab
PRO
0
120
Kiro Crewをはじめよう
kizawa2020
1
160
⾮構造化データを価値に変える AI エージェント基盤とその組織
ivry_presentationmaterials
0
190
Local CI × GitHub Actions
gogutan
2
190
実践!Cloud Run ハンズオン!! / Cloud Run handson
y0hgi
2
280
「Docker Desktop不要の時代」は 来たのか?
ueponx
0
770
DGX Spark でどこまでハッキングができるのか?
kash1064
3
1.3k
エンタメ企業のM&Aを支えるデータ基盤 ― 統合から店舗のリアルタイム分析まで / Data Engineering Summit 2026
genda
0
170
AI駆動で「他アプリの音を加工できる」アプリを作った
satomasahiro2005
0
150
Featured
See All Featured
The Psychology of Web Performance [Beyond Tellerrand 2023]
tammyeverts
49
3.6k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
990
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
1.1k
Art, The Web, and Tiny UX
lynnandtonic
304
22k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
31
10k
The Language of Interfaces
destraynor
162
27k
Kristin Tynski - Automating Marketing Tasks With AI
techseoconnect
PRO
0
550
Automating Front-end Workflow
addyosmani
1369
210k
We Analyzed 250 Million AI Search Results: Here's What I Found
joshbly
2
2k
JavaScript: Past, Present, and Future - NDC Porto 2020
reverentgeek
52
6.1k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
530
4 Signs Your Business is Dying
shpigford
187
23k
Transcript
Rails and Sockets
Websocket Rails Gem https://github.com/websocket-rails/websocket-rails
Steps • Add Gem “websocket-rails” • bundle install • rails
g websocket_rails:install • config.middleware.delete Rack::Lock • rails server
B Client var dispatcher = new WebSocketRails('localhost:3000/websocket'); Initialize the socket
File: application.js
Bind for changes channel = dispatcher.subscribe('posts'); channel.bind('new', function(data) { console.log('a
new post about '+data.title+' arrived!'); }); File: application.js
Server rails g scaffold posts title:string description:string In some_action (create)
WebsocketRails[:posts].trigger 'new', latest_post File: posts_controller create action
Events from clients
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
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
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
• sinatra-websocket • webmachine-ruby • faye-websocket • tubesock Few more!
Rails and Node http://liamkaufman.com/blog/2013/02/27/adding-real-time-to-a- restful-rails-app/
Basic Flow
Node and Redis Prerequisites
Setting up Rails Server gem 'redis' gem 'socket.io-rails' File: gemfile
Initializing Redis on Server $redis = Redis.new(:host => 'localhost', :port=>
6379) File: initializers/redis_init.rb (manually_created)
$redis.publish 'rt-change', @post.to_json Publishing from rails to a channel File:
posts_controller.rb
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
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
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
Rails - rails s Node - node <folder>/<file-name>.js Start the
servers File: app/<folder>/<file-name>.js
redis_pub = require('redis').createClient(); redis_pub.publish('my_callback', JSON.parse(message)); Callback from node File: app/<folder>/<file-name>.js
$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
Load distribution. Easy integration with APNS,MPNS and GCM services. Also
has queuing systems. Advantages
Few ways to manage node server Forever - https://github.com/nodejitsu/forever PM2
- https://github.com/Unitech/pm2
Questions?
https://github.com/shrikanthkr/wsrails https://github.com/shrikanthkr/redis-socketio Samples
Thank You!