2.1.0 • Published 7 years ago

web-rockets v2.1.0

Weekly downloads
1
License
ISC
Repository
-
Last release
7 years ago

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
  ));
2.1.0

7 years ago

1.0.7

8 years ago

1.0.6

8 years ago

1.0.4

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago