an introduction to faye
DESCRIPTION
A basic introduction to Faye (http://faye.jcoglan.com/) the pubsub messaging system. Given during a brown bag/learning lunch session at work.TRANSCRIPT
![Page 1: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/1.jpg)
Fayesimple pub/sub messaginghttp://faye.jcoglan.com/
![Page 2: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/2.jpg)
• Publish-subscribe messaging system
• Based on the Bayeux protocol
• Servers/clients in Ruby and Javascript
![Page 3: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/3.jpg)
npm install faye
gem install faye
![Page 4: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/4.jpg)
Server
![Page 5: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/5.jpg)
var Faye = require('faye')var server = new Faye.NodeAdapter({ mount: '/faye' })
server.listen(8000)
![Page 6: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/6.jpg)
require 'faye'
server = Faye::RackAdapter.new(:mount => '/faye')server.listen(8000)
![Page 7: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/7.jpg)
Client
![Page 8: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/8.jpg)
var Faye = require('faye')var client = new Faye.Client('http://localhost:8000/faye')
// subscribe to a channelclient.subscribe('/messages', function(message) { console.log('We got a message: ' + message.text)})
// publish to a channelclient.publish('/messages', { text: 'HAI!'})
![Page 9: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/9.jpg)
<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>
![Page 10: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/10.jpg)
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!' })}
![Page 11: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/11.jpg)
Example App
![Page 12: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/12.jpg)
• Simple chat-room application
• Sinatra
• Faye
![Page 13: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/13.jpg)
require 'sinatra'
get '/' do erb :indexend
![Page 14: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/14.jpg)
var Faye = require('faye')var server = new Faye.NodeAdapter({ mount: '/faye' })server.listen(8000)
![Page 15: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/15.jpg)
<!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>
![Page 16: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/16.jpg)
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)})
![Page 17: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/17.jpg)
Demo
![Page 18: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/18.jpg)
Server
![Page 19: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/19.jpg)
Events
• handshake
• subscribe
• unsubscribe
• publish
• disconnect
![Page 20: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/20.jpg)
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 +"'")})
![Page 21: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/21.jpg)
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
![Page 22: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/22.jpg)
• Override default behaviour...
• incoming()
• outgoing()
Extensions
![Page 23: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/23.jpg)
Engines
• Change the back-end...
• faye-redis
![Page 24: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/24.jpg)
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)
![Page 25: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/25.jpg)
require 'faye'require 'faye-redis'
server = Faye::RackAdapter.new( :mount => '/faye', :timeout => 25, :engine => { :type => Faye::Redis, :host => 'localhost', :port => 6379 })server.listen(8000)
![Page 26: An Introduction to Faye](https://reader033.vdocuments.net/reader033/viewer/2022061113/545c4d02b0af9fa42c8b47a9/html5/thumbnails/26.jpg)