package
socket.io-proxy2 ================
npm install socket.io-proxy2GitHub-flavored package documentation.
Across all versions.
Download trend unavailable.
Runtime metadata.
Estimated unpacked size across 7 packages.
378.7 kB
Includes dependencies.
A socket.io proxy based on http-proxy to handle multiple socket.io servers.
$ npm install socket.io-proxy2const Proxy = require('socket.io-proxy2');
const redis = require('redis');
const redisClient = redis.createClient(6379, 'localhost');
const proxy = new Proxy({
redisClient: redisClient,
hosts: [
'localhost:3000',
'localhost:3001',
'localhost:3002'
]
});
const port = process.env['PORT'] || 4000;
proxy.listen(port, () => {
console.log(`proxy listening on port ${port}`);
});Options:
The polling transport requires that every request is directed to the same backend for the duration of the session. There are several ways to meet this requirement:
only use the websocket transport, but in that case you should rather consider using something like robust-websocket for the client and ws for the server, as one of the main features of Socket.IO is to provide a fallback when WebSocket connection is not possible.
use sticky-session, either IP-based (NGINX example) or cookie-based (HAProxy example, )
On first request (the socket has no id yet), the request is proxied to a random available backend. The id is sent as part of the response, which the proxy reads to save the association in Redis (key: <keyPrefix><socket id> with an expiry of <keyExpiry> seconds).
For the next requests or the WebSocket upgrade, the association is retrieved from Redis and the request is proxied to the right backend.
The package uses debug. In order to see all the debug output, you can run the proxy with the environment variable DEBUG including the desired scope:
$ DEBUG=socket.io-proxy* node indexuse Fedor Indutny's sticky-session package in a cluster environment
or try this package: