SlideShare una empresa de Scribd logo
1 de 26
Descargar para leer sin conexión
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

Más contenido relacionado

Similar a 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 a 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
 

Último

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
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
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024The Digital Insurer
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel Araújo
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusZilliz
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
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
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 

Último (20)

Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
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...
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024AXA XL - Insurer Innovation Award Americas 2024
AXA XL - Insurer Innovation Award Americas 2024
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
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
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 

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)