SlideShare a Scribd company logo
1 of 26
Download to read offline
Faye
simple pub/sub messaging
http://faye.jcoglan.com/
• Publish-subscribe messaging system
• Based on the Bayeux protocol
• Servers/clients in Ruby and Javascript
npm install faye




gem install faye
Server
var Faye   = require('faye')
var server = new Faye.NodeAdapter({ mount: '/faye' })

server.listen(8000)
require 'faye'

server = Faye::RackAdapter.new(:mount => '/faye')
server.listen(8000)
Client
var Faye   = require('faye')
var client = new Faye.Client('http://localhost:8000/faye')

// subscribe to a channel
client.subscribe('/messages', function(message) {
   console.log('We got a message: ' + message.text)
})

// publish to a channel
client.publish('/messages', {
   text: 'HAI!'
})
<script type="text/javascript"
        src="http://localhost:8000/faye/client.js"></script>

<script type="text/javascript">
  var client = new Faye.Client('http://localhost:8000/faye')

  client.subscribe('/messages', function(message) {
     alert('We got a message: ' + message.text)
  })
</script>
require 'faye'
require 'eventmachine'

EM.run {
  client = Faye::Client.new('http://localhost:8000/faye')

    # subscribe to a channel
    client.subscribe('/messages') do |message|
      puts message.inspect
    end

    # publish to a channel
    client.publish('/messages', { 'text' => 'HAI!' })
}
Example App
• Simple chat-room application
 • Sinatra
 • Faye
require 'sinatra'

get '/' do
  erb :index
end
var Faye   = require('faye')
var server = new Faye.NodeAdapter({ mount: '/faye' })
server.listen(8000)
<!DOCTYPE html>
<html>
<head>
  <title>Chattr</title>
  <link rel="stylesheet" href="chattr.css" type="text/css" media="screen" />
</head>
<body>
  <h1>Lets Chat...</h1>

  <ul id="chat"></ul>
  <form id="new_message" action="#" method="get" accept-charset="utf-8">
    <input type="text" name="message" id="message" value="" />
    <input type="submit" name="send" id="send" value="Send" />
  </form>

  <script src="jquery.min.js" charset="utf-8"></script>
  <script src="http://localhost:8000/faye/client.js" charset="utf-8"></script>
  <script src="chattr.js" charset="utf-8"></script>
</body>
</html>
var client = new Faye.Client('http://localhost:8000/faye')

// Publish a message...
$('#new_message').bind('submit',function() {
  var now     = new Date()
  var message = {
    content: $('#message').val(),
    timestamp: now.getHours() + ":" + now.getMinutes()
  }

     client.publish('/messages', message)
     $('#message').val('')
     return false
})

// Subscribe to message feed...
client.subscribe('/messages', function(message) {
  var str = ''
  str += '<li>'
  str += ' <span class="created_at">'+ message.timestamp +'</span>'
  str += ' '+ message.content
  str += '</li>'

     $('#chat').append(str)
})
Demo
Server
Events

• handshake
• subscribe
• unsubscribe
• publish
• disconnect
var Faye   = require('faye')
var server = new Faye.NodeAdapter({ mount: '/faye' })
server.listen(8000)

server.bind('handshake', function(client_id) {
   console.log("[handshake] - client: '"+ client_id +"'")
})

server.bind('subscribe', function(client_id, channel) {
   console.log("[subscribe] - client: '"+ client_id +"', channel: '"+ channel +"'")
})

server.bind('unsubscribe', function(client_id, channel) {
   console.log("[unsubscribe] - client: '"+ client_id +"', channel: '"+ channel
+"'")
})

server.bind('publish', function(client_id, channel, data) {
   console.log("[publish] - client: '"+ client_id +"', channel: '"+ channel +"'")
   console.log("[publish] - data:")
   console.log(data)
})

server.bind('disconnect', function(client_id) {
   console.log("[disconnect] - client: '"+ client_id +"'")
})
require 'faye'
require 'logger'

Faye::WebSocket.load_adapter('thin')
faye = Faye::RackAdapter.new(:mount => '/faye', :timeout => 25)

log = Logger.new(STDOUT)
log.level = Logger::INFO

faye.bind(:handshake) do |client_id|
  log.info("[handshake] - client: '#{client_id}'")
end

faye.bind(:subscribe) do |client_id,channel|
  log.info("[subscribe] - client: '#{client_id}', channel: '#{channel}'")
end

faye.bind(:unsubscribe) do |client_id,channel|
  log.info("[unsubscribe] - client: '#{client_id}', channel: '#{channel}'")
end

faye.bind(:publish) do |client_id,channel,data|
  log.info("[publish] - client: '#{client_id}', channel: '#{channel}', data: '#
{data.inspect}'")
end

faye.bind(:disconnect) do |client_id|
  log.info("[disconnect] - client: '#{client_id}'")
end

run faye
Extensions

• Override default behaviour...
 • incoming()
 • outgoing()
Engines


• Change the back-end...
 • faye-redis
var faye       = require('faye')
var faye_redis = require('faye-redis')

var server = new faye.NodeAdapter({
   mount:   '/faye',
   timeout: 25,
   engine:  {
     type:  faye_redis,
     host:  'localhost',
     port:  6379
   }
})
server.listen(8000)
require 'faye'
require 'faye-redis'

server = Faye::RackAdapter.new(
  :mount   => '/faye',
  :timeout => 25,
  :engine => {
    :type => Faye::Redis,
    :host => 'localhost',
    :port => 6379
  }
)
server.listen(8000)
An Introduction to Faye

More Related Content

Similar to An Introduction to Faye

Server Side Events
Server Side EventsServer Side Events
Server Side Eventsthepilif
 
[WSO2 Integration Summit Madrid 2019] Integration + Ballerina
[WSO2 Integration Summit Madrid 2019] Integration + Ballerina[WSO2 Integration Summit Madrid 2019] Integration + Ballerina
[WSO2 Integration Summit Madrid 2019] Integration + BallerinaWSO2
 
Practical PHP by example Jan Leth-Kjaer
Practical PHP by example   Jan Leth-KjaerPractical PHP by example   Jan Leth-Kjaer
Practical PHP by example Jan Leth-KjaerCOMMON Europe
 
The Zen of Lithium
The Zen of LithiumThe Zen of Lithium
The Zen of LithiumNate Abele
 
Top 10 F5 iRules to migrate to a modern load balancing platform
Top 10 F5 iRules to migrate to a modern load balancing platformTop 10 F5 iRules to migrate to a modern load balancing platform
Top 10 F5 iRules to migrate to a modern load balancing platformAvi Networks
 
Mashing up JavaScript – Advanced Techniques for modern Web Apps
Mashing up JavaScript – Advanced Techniques for modern Web AppsMashing up JavaScript – Advanced Techniques for modern Web Apps
Mashing up JavaScript – Advanced Techniques for modern Web AppsBastian Hofmann
 
Websockets talk at Rubyconf Uruguay 2010
Websockets talk at Rubyconf Uruguay 2010Websockets talk at Rubyconf Uruguay 2010
Websockets talk at Rubyconf Uruguay 2010Ismael Celis
 
The Browser Environment - A Systems Programmer's Perspective [sinatra edition]
The Browser Environment - A Systems Programmer's Perspective [sinatra edition]The Browser Environment - A Systems Programmer's Perspective [sinatra edition]
The Browser Environment - A Systems Programmer's Perspective [sinatra edition]Eleanor McHugh
 
8 Minutes On Rack
8 Minutes On Rack8 Minutes On Rack
8 Minutes On Rackdanwrong
 
Node.js API 서버 성능 개선기
Node.js API 서버 성능 개선기Node.js API 서버 성능 개선기
Node.js API 서버 성능 개선기JeongHun Byeon
 
Phpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friendsPhpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friendsMichael Peacock
 
Tadhack madrid June 2014: Joris Swinnen and WebRTC Nederland "Invite my colle...
Tadhack madrid June 2014: Joris Swinnen and WebRTC Nederland "Invite my colle...Tadhack madrid June 2014: Joris Swinnen and WebRTC Nederland "Invite my colle...
Tadhack madrid June 2014: Joris Swinnen and WebRTC Nederland "Invite my colle...Bart Uelen
 
HTML5 - The 2012 of the Web
HTML5 - The 2012 of the WebHTML5 - The 2012 of the Web
HTML5 - The 2012 of the WebRobert Nyman
 
feature toggles for ops
feature toggles for opsfeature toggles for ops
feature toggles for opsBram Vogelaar
 

Similar to An Introduction to Faye (20)

Server Side Events
Server Side EventsServer Side Events
Server Side Events
 
[WSO2 Integration Summit Madrid 2019] Integration + Ballerina
[WSO2 Integration Summit Madrid 2019] Integration + Ballerina[WSO2 Integration Summit Madrid 2019] Integration + Ballerina
[WSO2 Integration Summit Madrid 2019] Integration + Ballerina
 
web3j Overview
web3j Overviewweb3j Overview
web3j Overview
 
Practical PHP by example Jan Leth-Kjaer
Practical PHP by example   Jan Leth-KjaerPractical PHP by example   Jan Leth-Kjaer
Practical PHP by example Jan Leth-Kjaer
 
The Zen of Lithium
The Zen of LithiumThe Zen of Lithium
The Zen of Lithium
 
Top 10 F5 iRules to migrate to a modern load balancing platform
Top 10 F5 iRules to migrate to a modern load balancing platformTop 10 F5 iRules to migrate to a modern load balancing platform
Top 10 F5 iRules to migrate to a modern load balancing platform
 
NodeJS "Web en tiempo real"
NodeJS "Web en tiempo real"NodeJS "Web en tiempo real"
NodeJS "Web en tiempo real"
 
Mashing up JavaScript – Advanced Techniques for modern Web Apps
Mashing up JavaScript – Advanced Techniques for modern Web AppsMashing up JavaScript – Advanced Techniques for modern Web Apps
Mashing up JavaScript – Advanced Techniques for modern Web Apps
 
Websockets talk at Rubyconf Uruguay 2010
Websockets talk at Rubyconf Uruguay 2010Websockets talk at Rubyconf Uruguay 2010
Websockets talk at Rubyconf Uruguay 2010
 
Mashing up JavaScript
Mashing up JavaScriptMashing up JavaScript
Mashing up JavaScript
 
MMC Rest API - Servers
MMC Rest API - ServersMMC Rest API - Servers
MMC Rest API - Servers
 
The Browser Environment - A Systems Programmer's Perspective [sinatra edition]
The Browser Environment - A Systems Programmer's Perspective [sinatra edition]The Browser Environment - A Systems Programmer's Perspective [sinatra edition]
The Browser Environment - A Systems Programmer's Perspective [sinatra edition]
 
8 Minutes On Rack
8 Minutes On Rack8 Minutes On Rack
8 Minutes On Rack
 
Node.js API 서버 성능 개선기
Node.js API 서버 성능 개선기Node.js API 서버 성능 개선기
Node.js API 서버 성능 개선기
 
Phpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friendsPhpne august-2012-symfony-components-friends
Phpne august-2012-symfony-components-friends
 
Tadhack madrid June 2014: Joris Swinnen and WebRTC Nederland "Invite my colle...
Tadhack madrid June 2014: Joris Swinnen and WebRTC Nederland "Invite my colle...Tadhack madrid June 2014: Joris Swinnen and WebRTC Nederland "Invite my colle...
Tadhack madrid June 2014: Joris Swinnen and WebRTC Nederland "Invite my colle...
 
Socket.IO
Socket.IOSocket.IO
Socket.IO
 
HTML5 - The 2012 of the Web
HTML5 - The 2012 of the WebHTML5 - The 2012 of the Web
HTML5 - The 2012 of the Web
 
Payments On Rails
Payments On RailsPayments On Rails
Payments On Rails
 
feature toggles for ops
feature toggles for opsfeature toggles for ops
feature toggles for ops
 

Recently uploaded

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsRoshan Dwivedi
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 

An Introduction to Faye

  • 2. • Publish-subscribe messaging system • Based on the Bayeux protocol • Servers/clients in Ruby and Javascript
  • 3. npm install faye gem install faye
  • 5. var Faye = require('faye') var server = new Faye.NodeAdapter({ mount: '/faye' }) server.listen(8000)
  • 6. require 'faye' server = Faye::RackAdapter.new(:mount => '/faye') server.listen(8000)
  • 8. var Faye = require('faye') var client = new Faye.Client('http://localhost:8000/faye') // subscribe to a channel client.subscribe('/messages', function(message) { console.log('We got a message: ' + message.text) }) // publish to a channel client.publish('/messages', { text: 'HAI!' })
  • 9. <script type="text/javascript" src="http://localhost:8000/faye/client.js"></script> <script type="text/javascript"> var client = new Faye.Client('http://localhost:8000/faye') client.subscribe('/messages', function(message) { alert('We got a message: ' + message.text) }) </script>
  • 10. require 'faye' require 'eventmachine' EM.run { client = Faye::Client.new('http://localhost:8000/faye') # subscribe to a channel client.subscribe('/messages') do |message| puts message.inspect end # publish to a channel client.publish('/messages', { 'text' => 'HAI!' }) }
  • 12. • Simple chat-room application • Sinatra • Faye
  • 13. require 'sinatra' get '/' do erb :index end
  • 14. var Faye = require('faye') var server = new Faye.NodeAdapter({ mount: '/faye' }) server.listen(8000)
  • 15. <!DOCTYPE html> <html> <head> <title>Chattr</title> <link rel="stylesheet" href="chattr.css" type="text/css" media="screen" /> </head> <body> <h1>Lets Chat...</h1> <ul id="chat"></ul> <form id="new_message" action="#" method="get" accept-charset="utf-8"> <input type="text" name="message" id="message" value="" /> <input type="submit" name="send" id="send" value="Send" /> </form> <script src="jquery.min.js" charset="utf-8"></script> <script src="http://localhost:8000/faye/client.js" charset="utf-8"></script> <script src="chattr.js" charset="utf-8"></script> </body> </html>
  • 16. var client = new Faye.Client('http://localhost:8000/faye') // Publish a message... $('#new_message').bind('submit',function() { var now = new Date() var message = { content: $('#message').val(), timestamp: now.getHours() + ":" + now.getMinutes() } client.publish('/messages', message) $('#message').val('') return false }) // Subscribe to message feed... client.subscribe('/messages', function(message) { var str = '' str += '<li>' str += ' <span class="created_at">'+ message.timestamp +'</span>' str += ' '+ message.content str += '</li>' $('#chat').append(str) })
  • 17. Demo
  • 19. Events • handshake • subscribe • unsubscribe • publish • disconnect
  • 20. var Faye = require('faye') var server = new Faye.NodeAdapter({ mount: '/faye' }) server.listen(8000) server.bind('handshake', function(client_id) { console.log("[handshake] - client: '"+ client_id +"'") }) server.bind('subscribe', function(client_id, channel) { console.log("[subscribe] - client: '"+ client_id +"', channel: '"+ channel +"'") }) server.bind('unsubscribe', function(client_id, channel) { console.log("[unsubscribe] - client: '"+ client_id +"', channel: '"+ channel +"'") }) server.bind('publish', function(client_id, channel, data) { console.log("[publish] - client: '"+ client_id +"', channel: '"+ channel +"'") console.log("[publish] - data:") console.log(data) }) server.bind('disconnect', function(client_id) { console.log("[disconnect] - client: '"+ client_id +"'") })
  • 21. require 'faye' require 'logger' Faye::WebSocket.load_adapter('thin') faye = Faye::RackAdapter.new(:mount => '/faye', :timeout => 25) log = Logger.new(STDOUT) log.level = Logger::INFO faye.bind(:handshake) do |client_id| log.info("[handshake] - client: '#{client_id}'") end faye.bind(:subscribe) do |client_id,channel| log.info("[subscribe] - client: '#{client_id}', channel: '#{channel}'") end faye.bind(:unsubscribe) do |client_id,channel| log.info("[unsubscribe] - client: '#{client_id}', channel: '#{channel}'") end faye.bind(:publish) do |client_id,channel,data| log.info("[publish] - client: '#{client_id}', channel: '#{channel}', data: '# {data.inspect}'") end faye.bind(:disconnect) do |client_id| log.info("[disconnect] - client: '#{client_id}'") end run faye
  • 22. Extensions • Override default behaviour... • incoming() • outgoing()
  • 23. Engines • Change the back-end... • faye-redis
  • 24. var faye = require('faye') var faye_redis = require('faye-redis') var server = new faye.NodeAdapter({ mount: '/faye', timeout: 25, engine: { type: faye_redis, host: 'localhost', port: 6379 } }) server.listen(8000)
  • 25. require 'faye' require 'faye-redis' server = Faye::RackAdapter.new( :mount => '/faye', :timeout => 25, :engine => { :type => Faye::Redis, :host => 'localhost', :port => 6379 } ) server.listen(8000)