/web-rockets

Start/stop a socket.io server in a few lines. Useful for prototyping.

Primary LanguageJavaScript

web-rockets

Start/stop and customize a socket.io server in a few lines. Useful for prototyping.

Install

npm install web-rockets

Usage

import WebRockets from 'web-rockets';

new WebRockets();

A Web Socket Server needs a HTTP server. We'll start one for you if you don't specify any.

Express integration

import http from 'http';
import express from 'express';

const httpServer = http.createServer(express());

httpServer.listen(() => new WebRockets(httpServer));

Express emitter

import HTTPServer from 'express-emitter';

const http = new HTTPServer()
  .on('listening', () => new WebRockets(http.server));

Stop and restart

const webRockets = new WebRockets()
  .stop()
  .then(() => webRockets.start());

Listeners

You can add or remove listeners like this:

const ping = socket => socket.emit('pong');

new WebRockets()
  .listen('ping', ping)
  .unlisten('ping', ping);

Middleware

new WebRockets()
  // the same way you would do with socket.io
  .use((socket, next) => next());

Authentification by cookie

We support authentication via cookie if you also install web-rockets-cookie.

import identifyByCookie from 'web-rockets-cookie';

new WebRockets()
  .use(identifyByCookie(
    cookieName, // String - name of the cookie,
    true, // Boolean . true for secure cookies
    (cookie, socket, next) => { /* ... */ } // what to do
  ));