Skip to content

Instantly share code, notes, and snippets.

@dchymko
Last active September 12, 2019 17:17
Show Gist options
  • Save dchymko/da2a89dc3d5f3caf1826d58376bbc896 to your computer and use it in GitHub Desktop.
Save dchymko/da2a89dc3d5f3caf1826d58376bbc896 to your computer and use it in GitHub Desktop.
// Server
var socketio = require('socket.io');
var SocketIORedis = require('socket.io-redis');
const redis = require('redis');
var io1 = socketio.listen(8321);
//io1.adapter( SocketIORedis( { host: 'localhost', port: 6379 } ) );
const pub = redis.createClient(6379, 'localhost', { auth_pass: "" });
const sub = redis.createClient(6379, 'localhost', { auth_pass: "" });
io1.adapter(SocketIORedis({ pubClient: pub, subClient: sub }));
io1.on('connection', function(socket1) {
socket1.on('data', function(msg1) {
console.log(msg1);
});
socket1.on('cancel', function(msg1) {
console.log('Cance', msg1);
});
});
// Mirror
var ioIn = socketio.listen(8123);
//ioIn.adapter( SocketIORedis( { host: 'localhost', port: 6379 } ) );
const pubIn = redis.createClient(6379, 'localhost', { auth_pass: "" });
const subIn = redis.createClient(6379, 'localhost', { auth_pass: "" });
ioIn.adapter(SocketIORedis({ pubClient: pubIn, subClient: subIn }));
var ioOut = require('socket.io-client');
var socketOut = ioOut.connect('http://localhost:8321');
ioIn.on('connection', function(socketIn) {
socketIn.on('cancel', function(msg) {
socketOut.emit('data', msg);
});
});
// Client
var io2 = require('socket.io-client');
var socket2 = io2.connect('http://localhost:8123');
io1.of('/').adapter.clients((err, clients) => {
console.log('clients:', clients); // an array containing all connected socket ids
});
io1.of('/').adapter.allRooms((err, rooms) => {
console.log('rooms', rooms); // an array containing all rooms (accross every node)
});
setInterval( function () {
//is received
socket2.emit('cancel', "Cancel 1");
},1000 );
setInterval( function () {
//Is not recieved
io1.emit('cancel', 'Cancel 2');
io1.of('/').adapter.emit('cancel', '3');
},1000 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment