Pub / Sub in Ruby

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
Pub / Sub in Ruby
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
1 of 22

Recommended

Connect front end to back end using SignalR and Messaging by
Connect front end to back end using SignalR and MessagingConnect front end to back end using SignalR and Messaging
Connect front end to back end using SignalR and MessagingParticular Software
2.1K views25 slides
SCCI'15 - Devology - Session 6 - Servers and PHP by
SCCI'15 - Devology - Session 6 - Servers and PHPSCCI'15 - Devology - Session 6 - Servers and PHP
SCCI'15 - Devology - Session 6 - Servers and PHPSCCI-CU
532 views14 slides
Codecamp iasi-26 nov 2011-web sockets by
Codecamp iasi-26 nov 2011-web socketsCodecamp iasi-26 nov 2011-web sockets
Codecamp iasi-26 nov 2011-web socketsCodecamp Romania
868 views19 slides
2016 W3C Conference #9 : 컨테이너와 웹 어플리케이션 by
2016 W3C Conference #9 : 컨테이너와 웹 어플리케이션2016 W3C Conference #9 : 컨테이너와 웹 어플리케이션
2016 W3C Conference #9 : 컨테이너와 웹 어플리케이션양재동 코드랩
622 views33 slides
Using Communication and Messaging API in the HTML5 World by
Using Communication and Messaging API in the HTML5 WorldUsing Communication and Messaging API in the HTML5 World
Using Communication and Messaging API in the HTML5 WorldGil Fink
705 views31 slides
ITCamp 2011 - Florin Cardasim - Duplex Communications with WCF and Azure by
ITCamp 2011 - Florin Cardasim - Duplex Communications with WCF and AzureITCamp 2011 - Florin Cardasim - Duplex Communications with WCF and Azure
ITCamp 2011 - Florin Cardasim - Duplex Communications with WCF and AzureFlorin Cardasim
694 views23 slides

More Related Content

What's hot

Building a Messaging Framework on the Salesforce1 Platform by
Building a Messaging Framework on the Salesforce1 PlatformBuilding a Messaging Framework on the Salesforce1 Platform
Building a Messaging Framework on the Salesforce1 PlatformAnup Jadhav
1K views31 slides
Increase automation to rest by
Increase automation to restIncrease automation to rest
Increase automation to restShivaling Sannalli
127 views37 slides
Lifecycleofhostdeployedwithforemanandautomated by
LifecycleofhostdeployedwithforemanandautomatedLifecycleofhostdeployedwithforemanandautomated
LifecycleofhostdeployedwithforemanandautomatedKanwar Batra
258 views1 slide
SCCM Intune Windows 10 Co Management Architecture Decisions by
SCCM Intune Windows 10 Co Management Architecture DecisionsSCCM Intune Windows 10 Co Management Architecture Decisions
SCCM Intune Windows 10 Co Management Architecture DecisionsAnoop Nair
3K views21 slides
ESB Online Training Part 2 by
ESB Online Training Part 2ESB Online Training Part 2
ESB Online Training Part 2Vince Soliza
650 views27 slides
Learn Advanced JAVA at ASIT by
Learn Advanced JAVA at ASITLearn Advanced JAVA at ASIT
Learn Advanced JAVA at ASITASIT
346 views13 slides

What's hot(20)

Building a Messaging Framework on the Salesforce1 Platform by Anup Jadhav
Building a Messaging Framework on the Salesforce1 PlatformBuilding a Messaging Framework on the Salesforce1 Platform
Building a Messaging Framework on the Salesforce1 Platform
Anup Jadhav1K views
Lifecycleofhostdeployedwithforemanandautomated by Kanwar Batra
LifecycleofhostdeployedwithforemanandautomatedLifecycleofhostdeployedwithforemanandautomated
Lifecycleofhostdeployedwithforemanandautomated
Kanwar Batra258 views
SCCM Intune Windows 10 Co Management Architecture Decisions by Anoop Nair
SCCM Intune Windows 10 Co Management Architecture DecisionsSCCM Intune Windows 10 Co Management Architecture Decisions
SCCM Intune Windows 10 Co Management Architecture Decisions
Anoop Nair3K views
ESB Online Training Part 2 by Vince Soliza
ESB Online Training Part 2ESB Online Training Part 2
ESB Online Training Part 2
Vince Soliza650 views
Learn Advanced JAVA at ASIT by ASIT
Learn Advanced JAVA at ASITLearn Advanced JAVA at ASIT
Learn Advanced JAVA at ASIT
ASIT346 views
Performance testing automation with Dynatrace @LDNWebPerf - AndreasGrabner by Stephen Thair
Performance testing automation with Dynatrace  @LDNWebPerf - AndreasGrabnerPerformance testing automation with Dynatrace  @LDNWebPerf - AndreasGrabner
Performance testing automation with Dynatrace @LDNWebPerf - AndreasGrabner
Stephen Thair2.6K views
VPC Hands-on by Akira Sosa
VPC Hands-onVPC Hands-on
VPC Hands-on
Akira Sosa229 views
Real Time with Rails 5 by Lucas Renan
Real Time with Rails 5Real Time with Rails 5
Real Time with Rails 5
Lucas Renan299 views
BlazeMeter Presents at the High Performance Drupal Meetup by BlazeMeter
BlazeMeter Presents at the High Performance Drupal MeetupBlazeMeter Presents at the High Performance Drupal Meetup
BlazeMeter Presents at the High Performance Drupal Meetup
BlazeMeter2.9K views
Scaling CometD by Kevin Nilson by Jug Chennai
Scaling CometD by Kevin Nilson Scaling CometD by Kevin Nilson
Scaling CometD by Kevin Nilson
Jug Chennai741 views
What is new in Symfony 3,3, 3,4, 4.0, 4,1 + Flex by Haehnchen
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
Haehnchen790 views
Betfair's Site Rebuild: Fast - We promise by Tim Morrow
Betfair's Site Rebuild: Fast - We promiseBetfair's Site Rebuild: Fast - We promise
Betfair's Site Rebuild: Fast - We promise
Tim Morrow1.3K views
Java Servlet Lifecycle by AathikaJava
Java Servlet LifecycleJava Servlet Lifecycle
Java Servlet Lifecycle
AathikaJava82 views
Design & Secure Your Cloud Infrastructure by Anoop Nair
Design & Secure Your Cloud Infrastructure Design & Secure Your Cloud Infrastructure
Design & Secure Your Cloud Infrastructure
Anoop Nair1.1K views

Similar to Pub / Sub in Ruby

Recipes for a successful production cloudfoundry deployment - CF Summit 2014 by
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 2014Vinícius Carvalho
1.2K views31 slides
Cloud Foundry Cookbook: Recipes for a Successful Cloud Foundry Deployment in ... by
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 ...VMware Tanzu
3.8K views31 slides
WebSocket by
WebSocketWebSocket
WebSocketnjamnjam
1.9K views13 slides
Comet from JavaOne 2008 by
Comet from JavaOne 2008Comet from JavaOne 2008
Comet from JavaOne 2008Joe Walker
3K views60 slides
Palestra VCR by
Palestra VCRPalestra VCR
Palestra VCRCássio Marques
4.7K views67 slides
DDD Melbourne 2014 security in ASP.Net Web API 2 by
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 2Pratik Khasnabis
1.3K views27 slides

Similar to Pub / Sub in Ruby(20)

Recipes for a successful production cloudfoundry deployment - CF Summit 2014 by Vinícius Carvalho
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
Vinícius Carvalho1.2K views
Cloud Foundry Cookbook: Recipes for a Successful Cloud Foundry Deployment in ... by VMware Tanzu
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 ...
VMware Tanzu3.8K views
WebSocket by njamnjam
WebSocketWebSocket
WebSocket
njamnjam1.9K views
Comet from JavaOne 2008 by Joe Walker
Comet from JavaOne 2008Comet from JavaOne 2008
Comet from JavaOne 2008
Joe Walker3K views
DDD Melbourne 2014 security in ASP.Net Web API 2 by Pratik Khasnabis
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
Pratik Khasnabis1.3K views
Realtime web experience with signal r by Ran Wahle
Realtime web experience with signal rRealtime web experience with signal r
Realtime web experience with signal r
Ran Wahle3.1K views
Realtime web experience with signalR by Ran Wahle
Realtime web experience with signalRRealtime web experience with signalR
Realtime web experience with signalR
Ran Wahle1.6K views
Using communication and messaging API in the HTML5 world - GIl Fink, sparXsys by Codemotion Tel Aviv
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 by SHAKIL AKHTAR
Architecting &Building Scalable Secure Web APIArchitecting &Building Scalable Secure Web API
Architecting &Building Scalable Secure Web API
SHAKIL AKHTAR2.7K views
Ntu workshop : REST, PayPal APIs & Async by Aeshan Wijetunge
Ntu workshop : REST, PayPal APIs & AsyncNtu workshop : REST, PayPal APIs & Async
Ntu workshop : REST, PayPal APIs & Async
Aeshan Wijetunge268 views
JUDCon 2013- JBoss Data Grid and WebSockets: Delivering Real Time Push at Scale by C2B2 Consulting
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
C2B2 Consulting2.2K views
Cloud Foundry Meetup Tokyo #1 Route service by Gwenn Etourneau
Cloud Foundry Meetup Tokyo #1 Route serviceCloud Foundry Meetup Tokyo #1 Route service
Cloud Foundry Meetup Tokyo #1 Route service
Gwenn Etourneau864 views
Chickens & Eggs: Managing secrets in AWS with Hashicorp Vault by Jeff Horwitz
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
Jeff Horwitz6.2K views
Node.JS and WebSockets with Faye by Matjaž Lipuš
Node.JS and WebSockets with FayeNode.JS and WebSockets with Faye
Node.JS and WebSockets with Faye
Matjaž Lipuš3.5K views
Exploring Contact Lens and Amazon Connect by CloudHesive
Exploring Contact Lens and Amazon ConnectExploring Contact Lens and Amazon Connect
Exploring Contact Lens and Amazon Connect
CloudHesive141 views
Canadian CNCF: "Emissary-ingress 101: An introduction to the CNCF incubation-... by Daniel Bryant
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-...
Daniel Bryant368 views
Consuming Web Services in Microsoft Silverlight 3 by goodfriday
Consuming Web Services in Microsoft Silverlight 3Consuming Web Services in Microsoft Silverlight 3
Consuming Web Services in Microsoft Silverlight 3
goodfriday2K views

Recently uploaded

FIMA 2023 Neo4j & FS - Entity Resolution.pptx by
FIMA 2023 Neo4j & FS - Entity Resolution.pptxFIMA 2023 Neo4j & FS - Entity Resolution.pptx
FIMA 2023 Neo4j & FS - Entity Resolution.pptxNeo4j
17 views26 slides
Dapr Unleashed: Accelerating Microservice Development by
Dapr Unleashed: Accelerating Microservice DevelopmentDapr Unleashed: Accelerating Microservice Development
Dapr Unleashed: Accelerating Microservice DevelopmentMiroslav Janeski
12 views29 slides
Fleet Management Software in India by
Fleet Management Software in India Fleet Management Software in India
Fleet Management Software in India Fleetable
12 views1 slide
How Workforce Management Software Empowers SMEs | TraQSuite by
How Workforce Management Software Empowers SMEs | TraQSuiteHow Workforce Management Software Empowers SMEs | TraQSuite
How Workforce Management Software Empowers SMEs | TraQSuiteTraQSuite
5 views3 slides
Introduction to Git Source Control by
Introduction to Git Source ControlIntroduction to Git Source Control
Introduction to Git Source ControlJohn Valentino
6 views18 slides
Copilot Prompting Toolkit_All Resources.pdf by
Copilot Prompting Toolkit_All Resources.pdfCopilot Prompting Toolkit_All Resources.pdf
Copilot Prompting Toolkit_All Resources.pdfRiccardo Zamana
16 views4 slides

Recently uploaded(20)

FIMA 2023 Neo4j & FS - Entity Resolution.pptx by Neo4j
FIMA 2023 Neo4j & FS - Entity Resolution.pptxFIMA 2023 Neo4j & FS - Entity Resolution.pptx
FIMA 2023 Neo4j & FS - Entity Resolution.pptx
Neo4j17 views
Dapr Unleashed: Accelerating Microservice Development by Miroslav Janeski
Dapr Unleashed: Accelerating Microservice DevelopmentDapr Unleashed: Accelerating Microservice Development
Dapr Unleashed: Accelerating Microservice Development
Miroslav Janeski12 views
Fleet Management Software in India by Fleetable
Fleet Management Software in India Fleet Management Software in India
Fleet Management Software in India
Fleetable12 views
How Workforce Management Software Empowers SMEs | TraQSuite by TraQSuite
How Workforce Management Software Empowers SMEs | TraQSuiteHow Workforce Management Software Empowers SMEs | TraQSuite
How Workforce Management Software Empowers SMEs | TraQSuite
TraQSuite5 views
Introduction to Git Source Control by John Valentino
Introduction to Git Source ControlIntroduction to Git Source Control
Introduction to Git Source Control
John Valentino6 views
Copilot Prompting Toolkit_All Resources.pdf by Riccardo Zamana
Copilot Prompting Toolkit_All Resources.pdfCopilot Prompting Toolkit_All Resources.pdf
Copilot Prompting Toolkit_All Resources.pdf
Riccardo Zamana16 views
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx by animuscrm
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
2023-November-Schneider Electric-Meetup-BCN Admin Group.pptx
animuscrm15 views
Unlocking the Power of AI in Product Management - A Comprehensive Guide for P... by NimaTorabi2
Unlocking the Power of AI in Product Management - A Comprehensive Guide for P...Unlocking the Power of AI in Product Management - A Comprehensive Guide for P...
Unlocking the Power of AI in Product Management - A Comprehensive Guide for P...
NimaTorabi215 views
DRYiCE™ iAutomate: AI-enhanced Intelligent Runbook Automation by HCLSoftware
DRYiCE™ iAutomate: AI-enhanced Intelligent Runbook AutomationDRYiCE™ iAutomate: AI-enhanced Intelligent Runbook Automation
DRYiCE™ iAutomate: AI-enhanced Intelligent Runbook Automation
HCLSoftware6 views
predicting-m3-devopsconMunich-2023-v2.pptx by Tier1 app
predicting-m3-devopsconMunich-2023-v2.pptxpredicting-m3-devopsconMunich-2023-v2.pptx
predicting-m3-devopsconMunich-2023-v2.pptx
Tier1 app9 views
Quality Engineer: A Day in the Life by John Valentino
Quality Engineer: A Day in the LifeQuality Engineer: A Day in the Life
Quality Engineer: A Day in the Life
John Valentino7 views
Gen Apps on Google Cloud PaLM2 and Codey APIs in Action by Márton Kodok
Gen Apps on Google Cloud PaLM2 and Codey APIs in ActionGen Apps on Google Cloud PaLM2 and Codey APIs in Action
Gen Apps on Google Cloud PaLM2 and Codey APIs in Action
Márton Kodok15 views
Bootstrapping vs Venture Capital.pptx by Zeljko Svedic
Bootstrapping vs Venture Capital.pptxBootstrapping vs Venture Capital.pptx
Bootstrapping vs Venture Capital.pptx
Zeljko Svedic14 views
360 graden fabriek by info33492
360 graden fabriek360 graden fabriek
360 graden fabriek
info33492143 views

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
  • 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