WebSocket

Domingo Ernesto Savoretti edited this page Apr 24, 2014 · 10 revisions
Clone this wiki locally

NSR becomes real-time

Beginning with v0.9.0 NSR adds websocket protocol - as described in RFC 6455 - The WebSocket Protocol - IETF Tools - handling, so that any client script that implements its client-side counterpart - this will be, for most purposes, native WebSocket object which is nowadays supported by almost all modern browsers, with an uniform [API] (http://www.w3.org/TR/2011/WD-websockets-20110419/) - can establish bidirectional communication with a NSR driven application.

The inclusion was driven by the certainty that websocket protocol will become - if it isn't yet - a first class citizen in the web app universe, as much as http protocol, hence having a tool that lets the programmer handle both would become useful, much more so if the interface of the former follows similar usage pattern to that of the latter. Which leads us to the following point.

Implementation - Usage

All websocket functionality is contained in node-simple-router/lib/ws, which exports the following objects/methods:

  • opcodes
  • WebSocketConnection
  • WebSocketServer
  • createWebSocketServer

Most of the time the usage pattern will stay within the lines illustrated by the following code, which shows a naive echo server:

//Step 1: create a standard http server and put it to work.
var http = require('http');
var httpServer = http.createrServer(function(request, response) {
  response.end(renderEchoPage()); //a page to send strings and show the response sent by ws server
});
httpServer.listen(1234);

//Step 2: create a websocket server
var ws = require('node-simple-router/lib/ws');
var wsServer = ws.createWebSocketServer(function (websocket) {
  //All websocket functionality goes here.
  websocket.on('data', function(opcode, data) {
    websocket.send(data); //Just echo what was received.
  });
  websocket.on('close', function(code, reason) {
    console.log("Websocket closed:", code, reason);   
  });
});

//Step 3: put it to work, listening for connections attached to the main http server.
wsServer.listen(httpServer);
//An alternative would be to put wsServer to listen at its own, dedicated port, like so:
// wsServer.listen(1235);
// but you usually will want it to listen at the same port that the main httpServer does.

It's worth noting that a WebSocketServer starts its lifecycle accepting regular http requests, adding a handler to "upgrade" requests (the ones that get sent to the server when the client invokes an url that uses the ws scheme, like ws://echo.websocket.org) in order to "promote" the raw socket which drives the request to a permanent status as a websocket. This knowledge is necessary if you want to roll your own server - although in that case you'll need a bunch more than this - or just simply understand what goes on under the hood.

In my case, the reading that drove all this, and which I strongly recommend if your truly interested in WebSocket technology, is The Definitive Guide to HTML5 WebSocket. It's very thorough and fully enjoyable. Furthermore, a substantial part of the code which lives in ws.coffee (the source file for NSR websockets) was inspired in the model offered in chapter 3 "The WebSocket Protocol", for which the authors - Vanessa Wang, Frank Salim and Peter Moskovits - must be credited.

An interesting add-on in order to make this tool truly useful would be a pubsub and/or RPC protocol layered on top of NSR WebSocket, but that will take some time. Currently an implementation of [WAMP - The Web Application Messaging Protocol] (wamp.ws) is under active development.