Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Very simple example chat on Node.js
<html>
<head>
<title>Node.js Zoo Chat</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function(){
$('form#send').submit(onSend);
longPoll();
$('#nickname').focus();
});
function onSend(eventData){
eventData.preventDefault();
var msgArr = $(this).serializeArray();
var message = {
nickname : msgArr[0].value,
text : msgArr[1].value
};
$.post('/send', message, function (data){
$('#text').val('').focus();
},
'json');
}
function longPoll(data){
if (data && data.messages) {
for (var i = 0; i < data.messages.length; i++) {
var message = data.messages[i];
$('<p><b>'+message.nickname+':</b> <span>'+message.text+'</span></p>').hide().prependTo('#messages').slideDown();
}
}
$.ajax({
cache: false,
type: "GET",
url: "/recv",
success: function(data){
longPoll(data);
}
});
}
</script>
</head>
<body>
<h1>Node.js Zoo Chat</h1>
<form action="/send" method="post" id="send">
<label for="nickname">Nickname:</label> <input name="nickname" size="10" id="nickname" />
<label for="text">Message:</label> <input name="text" size="40" id="text" />
<input type="submit">
</form>
<div id="messages"></div>
</body>
</html>
express = require("express")
callbacks = []
// function sending messages to clients
appendMessage = (message) ->
resp = messages: [ message ]
callbacks.shift() resp while callbacks.length > 0
// create server on express
app = module.exports = express.createServer()
app.use express.bodyParser()
// simply return index.html
app.get "/", (req, res) ->
res.sendfile "index.html"
// process client message
app.post "/send", (req, res) ->
message =
nickname: req.param("nickname", "Anonymous")
text: req.param("text", "")
appendMessage message
res.json status: "ok"
// wait for new messages
app.get "/recv", (req, res) ->
callbacks.push (message) ->
res.json message
// listen port
app.listen process.env.PORT
var express = require('express');
var callbacks = [];
function appendMessage(message){
var resp = {messages: [message]};
while (callbacks.length > 0) {
callbacks.shift()(resp);
}
}
var app = module.exports = express.createServer();
app.use(express.bodyParser());
app.get('/', function(req, res){
res.sendfile('index.html');
});
app.post('/send', function(req, res){
var message = {
nickname: req.param('nickname', 'Anonymous'),
text: req.param('text', '')
};
appendMessage(message);
res.json({status: 'ok'});
});
app.get('/recv', function(req, res){
callbacks.push(function(message){
res.json(message);
});
});
app.listen(process.env.PORT);
@rukeba

This comment has been minimized.

Copy link
Owner Author

commented Nov 8, 2011

It has one requirement: express

To install it execute on linux or mac:

npm install express

on windows:

ryppi.py install express
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.