Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Fayesimple pub/sub messaginghttp://faye.jcoglan.com/
• Publish-subscribe messaging system• Based on the Bayeux protocol• Servers/clients in Ruby and Javascript
npm install fayegem install faye
Server
var Faye   = require(faye)var server = new Faye.NodeAdapter({ mount: /faye })server.listen(8000)
require fayeserver = 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 channelclient.subscrib...
<script type="text/javascript"        src="http://localhost:8000/faye/client.js"></script><script type="text/javascript"> ...
require fayerequire eventmachineEM.run {  client = Faye::Client.new(http://localhost:8000/faye)    # subscribe to a channe...
Example App
• Simple chat-room application • Sinatra • Faye
require sinatraget / do  erb :indexend
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...
var client = new Faye.Client(http://localhost:8000/faye)// Publish a message...$(#new_message).bind(submit,function() {  v...
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, fun...
require fayerequire loggerFaye::WebSocket.load_adapter(thin)faye = Faye::RackAdapter.new(:mount => /faye, :timeout => 25)l...
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,   ...
require fayerequire faye-redisserver = Faye::RackAdapter.new(  :mount   => /faye,  :timeout => 25,  :engine => {    :type ...
An Introduction to Faye
Upcoming SlideShare
Loading in …5
×

An Introduction to Faye

8,103 views

Published on

A basic introduction to Faye (http://faye.jcoglan.com/) the pubsub messaging system. Given during a brown bag/learning lunch session at work.

An Introduction to Faye

  1. 1. Fayesimple pub/sub messaginghttp://faye.jcoglan.com/
  2. 2. • Publish-subscribe messaging system• Based on the Bayeux protocol• Servers/clients in Ruby and Javascript
  3. 3. npm install fayegem install faye
  4. 4. Server
  5. 5. var Faye = require(faye)var server = new Faye.NodeAdapter({ mount: /faye })server.listen(8000)
  6. 6. require fayeserver = Faye::RackAdapter.new(:mount => /faye)server.listen(8000)
  7. 7. Client
  8. 8. 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!})
  9. 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. 10. require fayerequire eventmachineEM.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! })}
  11. 11. Example App
  12. 12. • Simple chat-room application • Sinatra • Faye
  13. 13. require sinatraget / do erb :indexend
  14. 14. var Faye = require(faye)var server = new Faye.NodeAdapter({ mount: /faye })server.listen(8000)
  15. 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. 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. 17. Demo
  18. 18. Server
  19. 19. Events• handshake• subscribe• unsubscribe• publish• disconnect
  20. 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. 21. require fayerequire loggerFaye::WebSocket.load_adapter(thin)faye = Faye::RackAdapter.new(:mount => /faye, :timeout => 25)log = Logger.new(STDOUT)log.level = Logger::INFOfaye.bind(:handshake) do |client_id| log.info("[handshake] - client: #{client_id}")endfaye.bind(:subscribe) do |client_id,channel| log.info("[subscribe] - client: #{client_id}, channel: #{channel}")endfaye.bind(:unsubscribe) do |client_id,channel| log.info("[unsubscribe] - client: #{client_id}, channel: #{channel}")endfaye.bind(:publish) do |client_id,channel,data| log.info("[publish] - client: #{client_id}, channel: #{channel}, data: #{data.inspect}")endfaye.bind(:disconnect) do |client_id| log.info("[disconnect] - client: #{client_id}")endrun faye
  22. 22. Extensions• Override default behaviour... • incoming() • outgoing()
  23. 23. Engines• Change the back-end... • faye-redis
  24. 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. 25. require fayerequire faye-redisserver = Faye::RackAdapter.new( :mount => /faye, :timeout => 25, :engine => { :type => Faye::Redis, :host => localhost, :port => 6379 })server.listen(8000)

×