SlideShare a Scribd company logo
1 of 22
Download to read offline
Publish / Subscribe
in Ruby
Adrien Siami - Dimelo
@Intrepidd
What’s Pub / Sub ?
• Bidirectional communication between server and
clients

• Clients subscribe to channels

• Server or client pushes events to some channels

• Clients are notified in realtime!
Use case
• Private messaging

• Broadcasting

• Server-sent events
Possible Solutions (in ruby)
• Pusher

• PubNub

• Faye
Pusher
Pros

• SaaS

• Batteries Included (Auth, Presence)

Cons 

• SaaS
PubNub
Pros

• SaaS

• Batteries Included (Auth, Presence)

Cons

• SaaS

• One event = one channel
Faye
Pros

• Built with ! (And Javascript)

• Open Source and self hosted

• Extensible

• Server level hooks

• Globbing (/users/*)

• Great Maintainer (@jcoglan)

Cons

• Les batteries included
Benchmarks
Websocket Json-p Cors Failure rate
Faye 84.68% 5.46% 9.81% 0.62%
Pusher 88.4% 6,67% 4,94% 3.7%
Faye Overview - Javascript
var client = new Faye.Client('http://foo.bar/faye');
!
client.subscribe('/user/123', function(message) {
console.log(message.content);
});
!
client.publish('/user/999', {content: 'Hey dude wassup?'});
Faye Overview - Ruby
require 'faye'
require 'eventmachine'
!
EM.run {
!
client = Faye::Client.new('http://foo.bar/faye')
!
client.subscribe('/user/123') do |message|
puts message.content
end
!
client.publish('/user/999', {content: 'Hey dude wassup?'})
}
Faye Hosting
• It’s a Rack system ! I Know this.

• EventMachine, one thread, tons of connections

• In-Memory by default

• Faye engines, e.g faye-redis, for multiple process
architecture
Benchmarks
Server
Without redis!
(1 worker)!
With redis!
(1 worker)
with redis !
(4 workers)
Passenger
+ nginx
43 sec 1m 25 sec 53 sec
Node!
1m 18 sec 1m 42 sec
Thin 38 sec 1m 28 sec
Puma 30 sec 1m 28 sec 43 sec
• Connect 10k clients
• Subscribe
• send some messages
• disconnect
Gotchas
• Tune your file descriptors

• Use EM.epoll

• PhusionPassenger.advertised_concurrency_level = 0

• passenger_max_requests 0;
DIY !
• Authentication

• Presence
Faye Authentication
• JWT based signature, generated by the Rails
Server when allowed

• Inserted into the faye request through a plugin
executing an Ajax Call

• Check at the faye server level
dimelo/faye-authentication
Faye Authentication
In a controller app (route /faye/auth):
def auth
if can?(:read, params[:message].try(:channel))
render :json => {signature: Faye::Authentication.sign(params[:message], 'faye
secret')}
else
render :text => "Forbidden", :status => '403'
end
In your JS :
client.addExtension(new FayeAuthentication(client));
Faye Authentication
On the faye server (config.ru):
server = Faye::RackAdapter.new mount: '/faye'
server.add_extension Faye::Authentication::ServerExtension.new('faye secret')
run server
Faye Presence
• Keep track of several clients per user

• Notify connection / disconnection

• Keep infos about the session (away / available)
Faye Presence
{
"event": "user-disconnected",
"user": {
"id": 42,
"infos" : {
"status": "away"
}
}
}
Thank you.
Dimelo contest!
• Submit a pull request on a 20+ stars ruby Github repository
• Have it merged
• Have a chance to win a Xbox One or a PS4
• http://contest.dimelo.com
• http://jobs.dimelo.com

More Related Content

What's hot

Lifecycleofhostdeployedwithforemanandautomated
LifecycleofhostdeployedwithforemanandautomatedLifecycleofhostdeployedwithforemanandautomated
Lifecycleofhostdeployedwithforemanandautomated
Kanwar Batra
 
Cache control directive
Cache control directiveCache control directive
Cache control directive
Mohamed Mamoon
 
BlazeMeter Presents at the High Performance Drupal Meetup
BlazeMeter Presents at the High Performance Drupal MeetupBlazeMeter Presents at the High Performance Drupal Meetup
BlazeMeter Presents at the High Performance Drupal Meetup
BlazeMeter
 

What's hot (20)

Building a Messaging Framework on the Salesforce1 Platform
Building a Messaging Framework on the Salesforce1 PlatformBuilding a Messaging Framework on the Salesforce1 Platform
Building a Messaging Framework on the Salesforce1 Platform
 
Increase automation to rest
Increase automation to restIncrease automation to rest
Increase automation to rest
 
Lifecycleofhostdeployedwithforemanandautomated
LifecycleofhostdeployedwithforemanandautomatedLifecycleofhostdeployedwithforemanandautomated
Lifecycleofhostdeployedwithforemanandautomated
 
SCCM Intune Windows 10 Co Management Architecture Decisions
SCCM Intune Windows 10 Co Management Architecture DecisionsSCCM Intune Windows 10 Co Management Architecture Decisions
SCCM Intune Windows 10 Co Management Architecture Decisions
 
ESB Online Training Part 2
ESB Online Training Part 2ESB Online Training Part 2
ESB Online Training Part 2
 
Learn Advanced JAVA at ASIT
Learn Advanced JAVA at ASITLearn Advanced JAVA at ASIT
Learn Advanced JAVA at ASIT
 
Cache control directive
Cache control directiveCache control directive
Cache control directive
 
Performance testing automation with Dynatrace @LDNWebPerf - AndreasGrabner
Performance testing automation with Dynatrace  @LDNWebPerf - AndreasGrabnerPerformance testing automation with Dynatrace  @LDNWebPerf - AndreasGrabner
Performance testing automation with Dynatrace @LDNWebPerf - AndreasGrabner
 
VPC Hands-on
VPC Hands-onVPC Hands-on
VPC Hands-on
 
Real Time with Rails 5
Real Time with Rails 5Real Time with Rails 5
Real Time with Rails 5
 
BlazeMeter Presents at the High Performance Drupal Meetup
BlazeMeter Presents at the High Performance Drupal MeetupBlazeMeter Presents at the High Performance Drupal Meetup
BlazeMeter Presents at the High Performance Drupal Meetup
 
Soap xp-wg
Soap xp-wgSoap xp-wg
Soap xp-wg
 
Scaling CometD by Kevin Nilson
Scaling CometD by Kevin Nilson Scaling CometD by Kevin Nilson
Scaling CometD by Kevin Nilson
 
Hidden gems in cf2016
Hidden gems in cf2016Hidden gems in cf2016
Hidden gems in cf2016
 
Locking Down CF Servers
Locking Down CF ServersLocking Down CF Servers
Locking Down CF Servers
 
Web Services Testing
Web Services TestingWeb Services Testing
Web Services Testing
 
What is new in Symfony 3,3, 3,4, 4.0, 4,1 + Flex
What is new in Symfony 3,3, 3,4, 4.0, 4,1 + FlexWhat is new in Symfony 3,3, 3,4, 4.0, 4,1 + Flex
What is new in Symfony 3,3, 3,4, 4.0, 4,1 + Flex
 
Betfair's Site Rebuild: Fast - We promise
Betfair's Site Rebuild: Fast - We promiseBetfair's Site Rebuild: Fast - We promise
Betfair's Site Rebuild: Fast - We promise
 
Java Servlet Lifecycle
Java Servlet LifecycleJava Servlet Lifecycle
Java Servlet Lifecycle
 
Design & Secure Your Cloud Infrastructure
Design & Secure Your Cloud Infrastructure Design & Secure Your Cloud Infrastructure
Design & Secure Your Cloud Infrastructure
 

Similar to Pub / Sub in Ruby

Node.JS and WebSockets with Faye
Node.JS and WebSockets with FayeNode.JS and WebSockets with Faye
Node.JS and WebSockets with Faye
Matjaž Lipuš
 

Similar to Pub / Sub in Ruby (20)

Recipes for a successful production cloudfoundry deployment - CF Summit 2014
Recipes for a successful production cloudfoundry deployment - CF Summit 2014Recipes for a successful production cloudfoundry deployment - CF Summit 2014
Recipes for a successful production cloudfoundry deployment - CF Summit 2014
 
Cloud Foundry Cookbook: Recipes for a Successful Cloud Foundry Deployment in ...
Cloud Foundry Cookbook: Recipes for a Successful Cloud Foundry Deployment in ...Cloud Foundry Cookbook: Recipes for a Successful Cloud Foundry Deployment in ...
Cloud Foundry Cookbook: Recipes for a Successful Cloud Foundry Deployment in ...
 
WebSocket
WebSocketWebSocket
WebSocket
 
Comet from JavaOne 2008
Comet from JavaOne 2008Comet from JavaOne 2008
Comet from JavaOne 2008
 
Palestra VCR
Palestra VCRPalestra VCR
Palestra VCR
 
DDD Melbourne 2014 security in ASP.Net Web API 2
DDD Melbourne 2014 security in ASP.Net Web API 2DDD Melbourne 2014 security in ASP.Net Web API 2
DDD Melbourne 2014 security in ASP.Net Web API 2
 
Realtime web experience with signal r
Realtime web experience with signal rRealtime web experience with signal r
Realtime web experience with signal r
 
Realtime web experience with signalR
Realtime web experience with signalRRealtime web experience with signalR
Realtime web experience with signalR
 
Using communication and messaging API in the HTML5 world - GIl Fink, sparXsys
Using communication and messaging API in the HTML5 world - GIl Fink, sparXsysUsing communication and messaging API in the HTML5 world - GIl Fink, sparXsys
Using communication and messaging API in the HTML5 world - GIl Fink, sparXsys
 
Architecting &Building Scalable Secure Web API
Architecting &Building Scalable Secure Web APIArchitecting &Building Scalable Secure Web API
Architecting &Building Scalable Secure Web API
 
Operating your Production API
Operating your Production APIOperating your Production API
Operating your Production API
 
Ntu workshop : REST, PayPal APIs & Async
Ntu workshop : REST, PayPal APIs & AsyncNtu workshop : REST, PayPal APIs & Async
Ntu workshop : REST, PayPal APIs & Async
 
JUDCon 2013- JBoss Data Grid and WebSockets: Delivering Real Time Push at Scale
JUDCon 2013- JBoss Data Grid and WebSockets: Delivering Real Time Push at ScaleJUDCon 2013- JBoss Data Grid and WebSockets: Delivering Real Time Push at Scale
JUDCon 2013- JBoss Data Grid and WebSockets: Delivering Real Time Push at Scale
 
ASP.NET MVC 5 and SignalR 2
ASP.NET MVC 5 and SignalR 2ASP.NET MVC 5 and SignalR 2
ASP.NET MVC 5 and SignalR 2
 
Cloud Foundry Meetup Tokyo #1 Route service
Cloud Foundry Meetup Tokyo #1 Route serviceCloud Foundry Meetup Tokyo #1 Route service
Cloud Foundry Meetup Tokyo #1 Route service
 
Chickens & Eggs: Managing secrets in AWS with Hashicorp Vault
Chickens & Eggs: Managing secrets in AWS with Hashicorp VaultChickens & Eggs: Managing secrets in AWS with Hashicorp Vault
Chickens & Eggs: Managing secrets in AWS with Hashicorp Vault
 
Node.JS and WebSockets with Faye
Node.JS and WebSockets with FayeNode.JS and WebSockets with Faye
Node.JS and WebSockets with Faye
 
Exploring Contact Lens and Amazon Connect
Exploring Contact Lens and Amazon ConnectExploring Contact Lens and Amazon Connect
Exploring Contact Lens and Amazon Connect
 
Canadian CNCF: "Emissary-ingress 101: An introduction to the CNCF incubation-...
Canadian CNCF: "Emissary-ingress 101: An introduction to the CNCF incubation-...Canadian CNCF: "Emissary-ingress 101: An introduction to the CNCF incubation-...
Canadian CNCF: "Emissary-ingress 101: An introduction to the CNCF incubation-...
 
Consuming Web Services in Microsoft Silverlight 3
Consuming Web Services in Microsoft Silverlight 3Consuming Web Services in Microsoft Silverlight 3
Consuming Web Services in Microsoft Silverlight 3
 

Recently uploaded

CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdfAzure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
Azure_Native_Qumulo_High_Performance_Compute_Benchmarks.pdf
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
10 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 202410 Trends Likely to Shape Enterprise Technology in 2024
10 Trends Likely to Shape Enterprise Technology in 2024
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
MarTech Trend 2024 Book : Marketing Technology Trends (2024 Edition) How Data...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 

Pub / Sub in Ruby

  • 1. Publish / Subscribe in Ruby Adrien Siami - Dimelo @Intrepidd
  • 2. What’s Pub / Sub ? • Bidirectional communication between server and clients • Clients subscribe to channels • Server or client pushes events to some channels • Clients are notified in realtime!
  • 3. Use case • Private messaging • Broadcasting • Server-sent events
  • 4. Possible Solutions (in ruby) • Pusher • PubNub • Faye
  • 5. Pusher Pros • SaaS • Batteries Included (Auth, Presence) Cons • SaaS
  • 6. PubNub Pros • SaaS • Batteries Included (Auth, Presence) Cons • SaaS • One event = one channel
  • 7. Faye Pros • Built with ! (And Javascript) • Open Source and self hosted • Extensible • Server level hooks • Globbing (/users/*) • Great Maintainer (@jcoglan) Cons • Les batteries included
  • 8.
  • 9. Benchmarks Websocket Json-p Cors Failure rate Faye 84.68% 5.46% 9.81% 0.62% Pusher 88.4% 6,67% 4,94% 3.7%
  • 10. Faye Overview - Javascript var client = new Faye.Client('http://foo.bar/faye'); ! client.subscribe('/user/123', function(message) { console.log(message.content); }); ! client.publish('/user/999', {content: 'Hey dude wassup?'});
  • 11. Faye Overview - Ruby require 'faye' require 'eventmachine' ! EM.run { ! client = Faye::Client.new('http://foo.bar/faye') ! client.subscribe('/user/123') do |message| puts message.content end ! client.publish('/user/999', {content: 'Hey dude wassup?'}) }
  • 12. Faye Hosting • It’s a Rack system ! I Know this. • EventMachine, one thread, tons of connections • In-Memory by default • Faye engines, e.g faye-redis, for multiple process architecture
  • 13. Benchmarks Server Without redis! (1 worker)! With redis! (1 worker) with redis ! (4 workers) Passenger + nginx 43 sec 1m 25 sec 53 sec Node! 1m 18 sec 1m 42 sec Thin 38 sec 1m 28 sec Puma 30 sec 1m 28 sec 43 sec • Connect 10k clients • Subscribe • send some messages • disconnect
  • 14. Gotchas • Tune your file descriptors • Use EM.epoll • PhusionPassenger.advertised_concurrency_level = 0 • passenger_max_requests 0;
  • 16. Faye Authentication • JWT based signature, generated by the Rails Server when allowed • Inserted into the faye request through a plugin executing an Ajax Call • Check at the faye server level dimelo/faye-authentication
  • 17. Faye Authentication In a controller app (route /faye/auth): def auth if can?(:read, params[:message].try(:channel)) render :json => {signature: Faye::Authentication.sign(params[:message], 'faye secret')} else render :text => "Forbidden", :status => '403' end In your JS : client.addExtension(new FayeAuthentication(client));
  • 18. Faye Authentication On the faye server (config.ru): server = Faye::RackAdapter.new mount: '/faye' server.add_extension Faye::Authentication::ServerExtension.new('faye secret') run server
  • 19. Faye Presence • Keep track of several clients per user • Notify connection / disconnection • Keep infos about the session (away / available)
  • 20. Faye Presence { "event": "user-disconnected", "user": { "id": 42, "infos" : { "status": "away" } } }
  • 22. Dimelo contest! • Submit a pull request on a 20+ stars ruby Github repository • Have it merged • Have a chance to win a Xbox One or a PS4 • http://contest.dimelo.com • http://jobs.dimelo.com