web-rockets

2.1.0 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i web-rockets

Weekly Downloads

0

Version

2.1.0

License

ISC

Last publish

Collaborators

  • francoisv