Skip to content

Instantly share code, notes, and snippets.

@liorkesos
Created May 7, 2018 15:44
Show Gist options
  • Save liorkesos/5ba67198addf179c036c9515ab992ad9 to your computer and use it in GitHub Desktop.
Save liorkesos/5ba67198addf179c036c9515ab992ad9 to your computer and use it in GitHub Desktop.
var redis = require('redis');
var app = require('http').createServer();
io = require('socket.io').listen(app);
app.listen(8080);
io.sockets.on('connection', function (socket) {
const subscribe = redis.createClient(undefined, "redis.demos.cloud.linnovate.net")
const pub = redis.createClient(undefined, "redis.demos.cloud.linnovate.net")
/**
* Connect to redis channel and I trigger init event
*/
socket.on("subscribe", function (channel) {
subscribe.subscribe(channel);
console.log('channel' + channel);
socket.emit("init", "woww");
});
/**
* Goodbye redis pub/sub!
*/
socket.on('unsubscribe', function (obj) {
subscribe.unsubscribe(obj.channel);
});
/*
* Redis use subcribe on for send new message in a channel
*/
subscribe.on("message", function (channel, data) {
/*
* Socket.io use send function for trigger an event into default channel 'message'
*/
socket.send(data);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment