A WebSocket server written in low-level node.js, should be 90-100% spec compatible.
Pull request Compare This branch is 1 commit ahead, 88 commits behind ThisIsMissEm:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



**IMPORTANT: ** This module only works with node v0.1.94 and later.

This is a server for the WebSocket Protocol. It is designed to work with both draft75 and draft76 or later of the protocol specification.


An example of a simple server that will echo the messages received back out.

	var sys = require("sys");
	var ws = require('./lib/ws');

	var server = ws.createServer();

	server.addListener("listening", function(){
	  sys.log("Listening for connections.");

	// Handle WebSocket Requests
	server.addListener("connection", function(conn){
	  sys.log("<"+conn._id+"> connected");
	  server.broadcast("<"+conn._id+"> connected");

	  conn.addListener("close", function(){
	    sys.log("<"+conn._id+"> onClose");
	    server.broadcast("<"+conn._id+"> disconnected");

	  conn.addListener("message", function(message){
	    sys.log("<"+conn._id + "> "+message);
	    server.broadcast("<"+conn._id+"> "+message);

	// Handle HTTP Requests that don't UPGRADE to websockets
	server.addListener("request", function(req, res){
	  res.writeHead(200, {'Content-Type': 'text/plain'});
	  res.end('We can handle normal http connections too!\n');

Coupled with a websocket client like the example.html, and you have a working websocket chat client (sort of.)


The server acts like a normal http server in many respects, and exposes much of node's http.Server events and methods. However, there are a few differences, and things that haven't yet been implemented.

ws.creareServer() returns an instance of ws.Server, which acts like http.Server. However, not all methods and events that act on http.Server will act on ws.Server. Your application can handle normal http requests by listening for the "request" event.

ws.createServer() and ws.Server() takes an options object as its only parameter. The options object has a these defaults:

	{ version: "draft75"  // [string] Maybe be either draft75 or draft76
	, origin: "*"         // [string | array] Any valid domain name
	, subprotocol: null   // [string | array]

After a websocket client connects to the server, the server will emit the 'connection' event, with the ws/connection instance for the connection. This signifies that the connection has undertaken the necessary websocket handshaking and is now ready to send and receive data.

server.listen(port, host)

The same as the http.Server listen method.


Sends message to all connected clients.


The same as the http.Server close method.

Event: listening

function () { }

Emits when the server is ready to start accepting clients, after listen has been called.

Event: connection

function (connection) { }

Emits when a websocket client connects to the server. The connection is an instance of ws/connection.

Event: request

function (request, response) { }

Emits when a client connects using standard HTTP to the server. This is the same as the http.Server request event. Use this to handle normal http connections that won't upgrade to WebSocket.

Event: stream

function (stream) { }

The same as the http.Server stream event.

Event: close

function (errno) { }

Emits when the server is closed. Currently inherited from http.Server


This is an instance of a client connecting to the ws.Server, this is similar to the req on a http.Server.


Publishes a message to the client.


Closes the client's connection.

Event: readyStateChange

function (readyState) { }

Emits each time the connections status changes, the codes are as follows:

	0. unknown
	1. opening
	2. waiting
	3. handshaking
	4, connected
	5. closed

Event: close

function () { }

Emits when a client connection is closed or closes.

Event: message

function (message) { }

Emits when a client sends a message to the server.