Find file
Fetching contributors…
Cannot retrieve contributors at this time
90 lines (73 sloc) 3.16 KB


I want to use websockets on an app that has a perl cgi backend. That stack wont do it easily so this is my workaround. Clients can subscribe to updates, and servers can post updates, and this app can poll for updates if there are subscribed clients.

Build Status


The server sets a and express server. Clients load '/client.js' which gives them rest2websocket client which a user can subscribe to resources. Subscriptions are sent to the server and will trigger polling if a polling rule matches or will wait for something to post to it. When all clients that are subscribed to a resource either unsubscribe or disconnect the server will stop polling a polled resource.


The r2w client is an event emitter that will emit data for any subscribed resource. The event name being the resource's url. If the content type of the resource is application-json we'll decode it for you.

// client
var r2w = new Rest2websocket();
r2w.subscribe('', function(body, response){
  //  {
  //    url:  '',
  //    content-type: 'application/json'
  //  }
  console.log(typeof body);
  // object


There are two ways to get the server to emit a resource. Post to it from whatever, probably your legacy app as it knows best. (Access control doesn't exist yet.) or setup a polling.json.


Anyone who posts to /publish with an object of the following configuration will get their data sent to subscribed clients. Data can be a string or a javascript object. Headers are optional and will be passed as a mock request object. If data is a object content-type will get set to 'application/json', if data is a string it will be set to 'text/plain'.

websocket pub/sub api
  // posted to /publish
  // Headers:
  //    content-type: application-json
  // Body
      url: 'http://whatever/whoever/idk',
      data: String() || Object(),
      headers: {
        content-type: 'any type you want'


Polling needs to be configured. On startup the server will read a polling.json if one exists.

  /* defaults if a key is missing - regex key is required
    'regex': '.*', //^ and $ are tacked upon the string here 
    'regex-flags': 'i', // not sure if any of the others make sense
    'interval': 30,
    'timeout': 5,
    'concurrent': 5, // for all resources this regex matches 
    'method': 'GET',
    'querystring': '', //gets appended to the url of a subscription if a querystring isn't present
    'headers': {
      'X-Requested-With': 'XMLHttpRequest'

    'regex': '\d+',
    'interval': 5
    'regex': '*'



(things that are out of scope of the initial release)

  • override nodejs's default max host connection limit to something sane
  • support some sort of access control scheeme
    • maybe pass along supplied cookies and do a test get of the resource