Coder Social home page Coder Social logo

dynamic.io's Introduction

dynamic.io

Build Status NPM version

dynamic.io is a subclass of the socket.io server that knows how to deal with multiple hostnames and dynamically created namespaces that delete themselves when idle. It works with the standard socket.io client.

It also provides an optional socket.io/status page for debugging.

This subclass is perfectly usable as a standard socket.io server, in the ordinary way (it passes socket.io unit tests, for example). But it supports a few more options as well as the "setupNamespace" method for handling namespace callbacks.

Dynamic namespaces can be set up using "setupNamespace", which accepts a namespace name (or /.*/ for any-namespace, or any other regexp to match regexps) and a callback that can initialize a (passed) namespace instance when it is dynamically created. (Or, if it's pre-existing at setupNamespace time.) Return false from this callback to reject the namespace.

New options include:

  • host (default /.*/) - set to the host name (or regexp) to direct to "/"; all other hosts will direct to namespaces "//host/namespace". To send all connections to fully qualified namespaces, set host:true.
  • publicStatus (default false) - set to true to serve a debugging page on socket.io/status
  • retirement (default 10000) - the number of milliseconds to wait after a namespace becomes empty until starting to consider deleting it.

If you find this useful, please contribute test, documentation, and fixes.

A usage example:

// Specify host in options if you want to handle virtual hosts.
// Then only connections with a Host header matching host will
// map to "/".  (The default is /.*/, which maps all hosts to '/';
// host can be a string or a RegExp).  All other host namespaces
// will get a prefix of "//otherhost.com".  The Namespace method
// nsp.fullname() gets the fully qualified namespace name, while
// and nsp.name still // returns just '/' (or '/mynamespace')
// without the host; nsp.host returns the host.

io = require('dynamic.io')({host: 'myhost.com'});

// By the way, you can override gethost if you need to normalize.
io.getHost = function(conn) {
  return conn.request.headers.host.replace(/^www\./, '');
}

// Namespaces other than '/' are created and deleted dynamically.
// You can register namespaces with specific names, or with
// the '*' wildcard, and your setup function will be called whenever
// that namespace is created (or re-created after expiration).
io.setupNamespace('*', function(nsp) {
  // Set retirement to set up the number of milliseconds this
  // namespace should hang around after its last socket disconnects.
  // Default is 10 seconds.
  nsp.retirement = Math.max(nsp.retirement, 30 * 1000);
  // Set up the namespace as normal in socket.io.
  nsp.on('connect', function(socket) {
    console.log('got a socket connect on', nsp.fullname());
    socket.on('disconnect', function() {
      console.log('somebody disconnected from', nsp.fullname());
    });
  });
  // Return false from the setupNamespace callback if
  // you want to ignore this namespace.
  return true;
});

// Just use the server as normal.
io.listen(process.env.PORT);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.