I saw this snippet:
On Server
io.sockets.on('connection', function(socket) {
const subscribe = redis.createClient();
const publish = redis.createClient();
socket.on('publish', function(channel, data) {
publish.publish(channel, data);
});
socket.on('psubscribe', function(channel) {
subscribe.psubscribe(channel);
});
subscribe.on("pmessage", function(pattern, channel, message) {
socket.emit('message', { channel: channel, data: message });
});
});
On Client
$(".action").click(function() {
socket.emit('publish', 'game.#{gameid}.action.' + $(this).data('action'),
JSON.stringify({ nick: "#{nick}", ts: Date.now() })
);
And I'm wondering why? Doesn't Socket.IO have its own broadcast mechanism? Why choose Redis' Pub-Sub over Socket.IO? Can't we just do like this:
io.sockets.on('connection', function(socket) {
socket.on('action', function(channel, data) {
socket.broadcast.to(channel).emit(data)
});
});
And if there is a reason to use Redis, what would be the benefit? Persistence?