creating a tchat application in 15 minutes

Author Alaa-eddine K.

npm install
npm install express

The tchat server


first let's import node modules

var express = require('express')
  , app = express(app)
  , server = require('http').createServer(app);
var EurecaServer = require('').EurecaServer;

then we initialise eureca server object and serve a static page using express

var eurecaServer = new EurecaServer({allow : ['tchat.welcome', 'tchat.send']});

//attach eureca to express server

//serve index.html as default static file
app.get('/', function (req, res, next) {

Note eurecaServer object was initialised with allow parameter indicating that client side welcome() and send() methods can be called from server.
Those allowed methods will be declared under "tchat" namespace in the client side (see client code).


for each connected we'll store a reference so we can call its methods later. This reference is deleted when the clien disconnect

var connections = {};

eurecaServer.onConnect(function (connection) {
    console.log('New client ',, connection.eureca.remoteAddress);
	connections[] = {nick:null, client:eurecaServer.getClient(};

eurecaServer.onDisconnect(function (connection) {    
    console.log('Client quit',;
	delete connections[];


since this is just a tutorial, we will not check for real login/password here, instead we'll just check if the user indicated a non empty nick.

//a namespace for chat methods on the server side
var tchatServer = eurecaServer.exports.tchatServer = {};

tchatServer.login = function (nick) {
	console.log('Client %s auth with %s',, nick);
	var id =;
	if (nick !== undefined) //here we can check for login/password validity for example
		connections[id].nick = nick;  
		//tchat.welcome() is a client side function indicated that the client authentication is ok

Broadcast messages

Last step is to add a method that will receive messages and send them to all connected clients

//clients will call this method to send messages
tchatServer.send = function (message) {
	var sender = connections[];
	for (var c in connections) // just loop and send message to all connected clients
		if (connections[c].nick) //if the client is not connected nick is null
			connections[c].client.tchat.send(sender.nick, message);

Server ready !

console.log(' tchat server listening on port 8000')

The tchat client

HTML code

<!doctype html>
		<title> tchat</title>
		<script src="" ></script>
		<script src="/eureca.js"></script>
		<div id="auth">
			Pseudo : <input type="text" id="nick" /><button id="logBtn">Log In</button>
		<div id="main" style="display:none">
			<ul id="msgbox"></ul>
			<input type="text" id="msg" /><button id="sendBtn">Send</button>
			var client = new Eureca.Client();
			/* all the following JS code comes here */

Get server reference

Once the client is ready, it receive a proxy allowing it to call server side functions. we'll store this variable for later use, and to check if we are connected to the server

	var server;
	client.ready(function (proxy) {
		server = proxy;

Implement client side functions

as we saw earlier, the server calls two client functions : tchat.welcome() and tchat.send() ... here is how we implement them

	var tchat = client.exports.tchat = {};
	tchat.send = function(nick, message)
		var tchatline = $('<li><b>'+nick+' </b><span>'+message+'</span></li>');
	tchat.welcome = function()
		$('#auth').fadeOut('fast', function() {

DOM Stuff

In this part we generate a random client nick
we handle login button click event : call server side login() and sending client nick
we handle send button click event : send a message to the server so it can propagate it to all clients

	$('#nick').val('anonymous-'+new Date().getTime());
	$('#logBtn').click(function() {
		if (!server) return; //client not ready
		var nick = $('#nick').val();
	$('#sendBtn').click(function() {
		if (!server) return; //client not ready

and finaly, some CSS :)

	#msgbox {
		border:3px solid #555;
	#msgbox li {
		font:700 12px arial;
	#msgbox li b {
		font:700 14px arial;

Run the tchat application

To test the application, run the server using : node server.js (or better : node --harmony-proxies server.js) then go to this url http://localhost:8000/.

Your turn !

This tutorial showed how to create a very simple tchat application but it still lacks many features.
some ideas to enhance code :

Download tutorial code

Click here to download