私はこのスニペットを見ました:
サーバー上
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 });
});
});
クライアント上
$(".action").click(function() {
socket.emit('publish', 'game.#{gameid}.action.' + $(this).data('action'),
JSON.stringify({ nick: "#{nick}", ts: Date.now() })
);
なぜだろう?Socket.IO には独自のブロードキャスト メカニズムがありませんか? Socket.IO ではなく Redis の Pub-Sub を選ぶ理由 このようにすることはできません:
io.sockets.on('connection', function(socket) {
socket.on('action', function(channel, data) {
socket.broadcast.to(channel).emit(data)
});
});
また、Redis を使用する理由があるとすれば、その利点は何でしょうか? 持続性?