SlideShare une entreprise Scribd logo
1  sur  26
Télécharger pour lire hors ligne
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

Contenu connexe

Similaire à 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
 

Similaire à 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
 

Dernier

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
 
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
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesBoston Institute of Analytics
 
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
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Principled Technologies
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
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
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
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
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
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
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
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
 
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
 
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
 
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
 

Dernier (20)

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
 
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
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
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
 
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
Deploy with confidence: VMware Cloud Foundation 5.1 on next gen Dell PowerEdg...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
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
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
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
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
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)
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
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...
 
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
 
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
 
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
 

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)