Automattic/socket.io — pull request #1865 by davidbau (2014-11-15 12:50:27+00:00)
Title: Add setupNamespace and dynamic namespace support

This pull requests adds dynamic namespace support, virtual host support, and an optional /socket.io/status page to help monitor dynamic (and regular) namespaces.

It addresses https://github.com/Automattic/socket.io/issues/1854

From the library user's point of view, it doesn't change any existing behavior.

+++ Three new options.

host: true - enables hostname qualification on namespaces
host: 'roothost.com' - enables hostname qualification, except the matching host is still unqualified
host: '*' - the default - all hosts are unqualified

retirement: 10000 (default) - the number of ms to wait before cleaning up an empty dynamic namespace

serveStatus: false (default) - set to true to enable socket.io/status

+++ One main new public API

server.setupNamespace(pattern, fn) - call to set up all namespaces matching "pattern" (which may be a string or regexp).

The rules are: each namespace instance is called with at most one setup function. When new dynamic namespace si connected by a client, a setup function is found and called. Exact string matches are preferred over regexps, and within those groups, last registered name/pattern gets precedence. If no pattern matches a client's requested namespace name, an error is sent to the client.

+++ Other public functions

server.getHost(conn) - override to normalize hostnames
namespace.host - has the hostname
namespace.fullname() - '//host/name'
namespace.retriement - set to milliseconds to persist after going empty (defaulted)
namespace.expire(fn) - register an expiration callback for when a stale namespace is deleted