Skip to content

Instantly share code, notes, and snippets.

@emk
Created September 19, 2010 03:39
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save emk/586347 to your computer and use it in GitHub Desktop.
Save emk/586347 to your computer and use it in GitHub Desktop.
Real-time Comet with Faye, Node.js and Coffee
# Client-side Comet code. See comet_server.coffee for instructions.
client = new Faye.Client '/faye', timeout: 90
# Attach a security key to all our subscription messages.
client.addExtension
outgoing: (msg, callback) ->
return callback(msg) unless msg.channel is '/meta/subscribe'
(msg.ext ?= {}).authToken = 'secret'
callback(msg)
# Listen for messages on the specified channel.
client.subscribe '/msg', (message) ->
document.chat.output.value += "#{message}\n"
# This function is called when the user enters a message.
window.sendMessage = ->
client.publish '/msg', document.chat.input.value
document.chat.input.value = ''
<!-- Client-side HTML. See comet_server.coffee for instructions. -->
<html>
<head>
<title>Faye Comet demo with Node.js and Coffee</title>
<script type="text/javascript" src="/faye.js"></script>
<script type="text/javascript" src="/comet_client.js"></script>
</head>
<body>
<h1>Faye Comet demo with Node.js and Coffee</h1>
<p>Open multiple copies of this page and enter text below.</p>
<form name='chat' action='javascript:sendMessage()'>
<label for='input'>Input:</label>
<input name='input' type='text' size='40'><br/>
<textarea name='output' rows='20' cols='40'></textarea>
</form>
</body>
</html>
# Browser-based chat using Node.js, Comet, Faye and CoffeeScript
# by Eric Kidd
#
# To run this code, install Node.js and npm (the Node package manager),
# and run:
#
# npm install coffee-script
# npm install express
# npm install faye
# coffee comet_server.coffee
#
# Then visit http://127.0.0.1:8000/ in your web browser. This has been
# tested with Chrome & Mobile Chrome.
sys = require 'sys'
fs = require 'fs'
coffee = require 'coffee-script'
express = require 'express'
faye = require 'faye'
# Compile our client-side code to JavaScript. We do this every time we
# serve it to make tweaking the code easier.
comet_client_js = ->
coffee.compile(fs.readFileSync('./comet_client.coffee', 'utf-8'))
# Serve static HTML page and client-side JavaScript.
app = express.createServer()
app.get '/', (req, res) ->
res.contentType 'text/html'
res.sendfile './comet_client.html'
app.get '/comet_client.js', (req, res) ->
res.contentType 'text/javascript'
res.send(comet_client_js())
# Hook up Faye's Comet messaging service.
adapter = new faye.NodeAdapter mount: '/faye', timeout: 45
adapter.attach(app)
# Check the authentication token on every subscription request.
adapter.addExtension
incoming: (msg, callback) ->
return callback(msg) unless msg.channel is '/meta/subscribe'
unless msg.subscription is '/msg' and msg.ext?.authToken is 'secret'
msg.error = "Invalid authToken"
callback(msg)
app.listen 8000
sys.puts "Server running at http://127.0.0.1:8000/"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment