@ciscospaces/redis-adapter
TypeScript icon, indicating that this package has built-in type declarations

1.0.1 • Public • Published

Socket.IO Redis adapter

The @ciscospaces/redis-adapter package allows unicast packets between multiple Socket.IO servers.

Diagram of Socket.IO packets forwarded through Redis

Installation

npm install @ciscospaces/redis-adapter

Usage

With the redis package

import { createClient } from "redis";
import { Server } from "socket.io";
import { createAdapter } from "@ciscospaces/redis-adapter";

const pubClient = createClient({ url: "redis://localhost:6379" });
const subClient = pubClient.duplicate();
const runningNodeInstanceId = EC2InstanceId;
const externalNamespace = '/api/v1/namespace';

await Promise.all([
  pubClient.connect(),
  subClient.connect()
]);


/**
 * Example: 
 * instanceRoutingFullNameSpace = 'socket.io#/instanceid-i-0ee1956361f484f09#room:123#
 * instanceRoutingNameSpace = 'socket.io#/instanceid-i-0ee1956361f484f09#
 * 
 * 
 */
function instanceBasedMessageRouteFunc (instanceRoutingFullNameSpace, instanceRoutingNameSpace) {
  let overrideChannel;
  if (instanceRoutingFullNameSpace.indexOf(cachedRoutingNamespaceVar) !== -1) {
    overrideChannel = instanceRoutingFullNameSpace.replace(runningNodeInstanceId, externalNamespace);
  }
  return overrideChannel;
}
const io = new Server({
  adapter: createAdapter(pubClient, subClient, { instanceBasedMessageDelivery: instanceBasedMessageRouteFunc})
});

io.of(runningNodeInstanceId);

io.of(externalNamespace).on('connection', (socket) => {
  console.log('connected to instance namespace');
  socket.on('message', (data) => {
    console.log('message received', data);
  });
})

io.listen(3000);

License

MIT

Readme

Keywords

Package Sidebar

Install

npm i @ciscospaces/redis-adapter

Weekly Downloads

1

Version

1.0.1

License

MIT

Unpacked Size

50.7 kB

Total Files

9

Last publish

Collaborators

  • asgiri218