https://github.com/co2-git/web-rockets
Start/stop a socket.io server in a few lines. Useful for prototyping.
https://github.com/co2-git/web-rockets
Last synced: 11 months ago
JSON representation
Start/stop a socket.io server in a few lines. Useful for prototyping.
- Host: GitHub
- URL: https://github.com/co2-git/web-rockets
- Owner: co2-git
- Created: 2016-02-08T19:31:16.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-11-14T03:18:44.000Z (over 9 years ago)
- Last Synced: 2025-03-27T11:44:44.909Z (about 1 year ago)
- Language: JavaScript
- Size: 18.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
web-rockets
===
Start/stop and customize a socket.io server in a few lines. Useful for prototyping.
# Install
```bash
npm install web-rockets
```
# Usage
```js
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
```js
import http from 'http';
import express from 'express';
const httpServer = http.createServer(express());
httpServer.listen(() => new WebRockets(httpServer));
```
# Express emitter
```js
import HTTPServer from 'express-emitter';
const http = new HTTPServer()
.on('listening', () => new WebRockets(http.server));
```
# Stop and restart
```js
const webRockets = new WebRockets()
.stop()
.then(() => webRockets.start());
```
# Listeners
You can add or remove listeners like this:
```js
const ping = socket => socket.emit('pong');
new WebRockets()
.listen('ping', ping)
.unlisten('ping', ping);
```
# Middleware
```js
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`.
```js
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
));
```