Start/stop and customize a socket.io server in a few lines. Useful for prototyping.
npm install web-rockets
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.
import http from 'http';
import express from 'express';
const httpServer = http.createServer(express());
httpServer.listen(() => new WebRockets(httpServer));
import HTTPServer from 'express-emitter';
const http = new HTTPServer()
.on('listening', () => new WebRockets(http.server));
const webRockets = new WebRockets()
.stop()
.then(() => webRockets.start());
You can add or remove listeners like this:
const ping = socket => socket.emit('pong');
new WebRockets()
.listen('ping', ping)
.unlisten('ping', ping);
new WebRockets()
// the same way you would do with socket.io
.use((socket, next) => next());
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
));