Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Very simple example chat on Node.js
<title>Node.js Zoo Chat</title>
<script src="" type="text/javascript"></script>
<script type="text/javascript">
function onSend(eventData){
var msgArr = $(this).serializeArray();
var message = {
nickname : msgArr[0].value,
text : msgArr[1].value
$.post('/send', message, function (data){
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();
cache: false,
type: "GET",
url: "/recv",
success: function(data){
<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">
<div id="messages"></div>
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 "/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) {
var app = module.exports = express.createServer();
app.get('/', function(req, res){
});'/send', function(req, res){
var message = {
nickname: req.param('nickname', 'Anonymous'),
text: req.param('text', '')
res.json({status: 'ok'});
app.get('/recv', function(req, res){

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: 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.