-
-
Save grantmichaels/2689765 to your computer and use it in GitHub Desktop.
Real-time Comet with Faye, Node.js and Coffee
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 = '' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- 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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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