使用Redis作为PubSub在Socket.io上

时间:2022-08-22 16:31:49

I am creating a chat application which enable users to do private and group chats. Planning to use following technologies for this app:-

我正在创建一个聊天应用程序,它允许用户进行私人聊天和群组聊天。计划为这个应用使用以下技术:-。

NodeJs + Socket.io + Redis + CouchDB(To store message history) + AngularJS

NodeJs +套接字。io + Redis + CouchDB(用于存储消息历史)+ AngularJS

Per my initial research using Redis as a PubSub service is better approach over using Socket.io as pub-sub .Reason of this is if different users are connected to different server instances, then using socket in this scenario will create problem as message send by user 1 will not pass on to user 2(user 1 connected to server 1 and user 2 connected to server 2).

根据我最初的研究,使用Redis作为PubSub服务比使用Socket更好。io作为sub .原因是如果不同的用户连接到不同的服务器实例,那么在这个场景中使用套接字会产生问题,因为用户1发送的消息不会传递给用户2(用户1连接到服务器1,用户2连接到服务器2)。

But if we use Redis, then per my understanding we have to create new channels to enable private chats. And their is a limit to 10k channels in Redis.

但是如果我们使用Redis,那么根据我的理解,我们必须创建新的通道来启用私有聊天。在Redis,他们的频道限制为10k。

My Doubts are

我的疑问是

  1. Do I need to create new channel every time to enable private chat between two users?
  2. 我是否需要每次都创建一个新通道来启用两个用户之间的私人聊天?
  3. If I need to create separate channels, then is there actually a limit of 10K channels?
  4. 如果我需要创建单独的通道,那么实际上是否有10K通道的限制?
  5. I need a working example of using Redis as pub/sub with socket.io to enable private chats.
  6. 我需要一个使用Redis作为pub/sub的工作示例。io启用私人聊天。

Regards, Vikram

问候,维克拉姆

3 个解决方案

#1


15  

After reading below articles/blog post, using Redis for pub/sub over socket.io pub/sub will help in scalability and better performance.

在阅读下面的文章/博客文章后,使用Redis作为pub/sub over socket。io pub/sub将有助于提高可扩展性和更好的性能。

https://github.com/sayar/RedisMVA/blob/master/module6_redis_pubsub/README.md

https://github.com/sayar/RedisMVA/blob/master/module6_redis_pubsub/README.md

https://github.com/rajaraodv/redispubsub

https://github.com/rajaraodv/redispubsub

Further I am able to create a quick POC on private chat using redis . Here is the code:-

此外,我还可以使用redis在私人聊天上创建一个快速POC。这是代码:-

var app = require('http').createServer(handler);
app.listen(8088);
var io = require('socket.io').listen(app);
var redis = require('redis');
var redis2 = require('socket.io-redis');
io.adapter(redis2({ host: 'localhost', port: 6379 }));
var fs = require('fs');

function handler(req,res){
    fs.readFile(__dirname + '/index.html', function(err,data){
        if(err){
            res.writeHead(500);
            return res.end('Error loading index.html');
        }
        res.writeHead(200);
        console.log("Listening on port 8088");
        res.end(data);
    });
}

var store = redis.createClient();   
var pub = redis.createClient();
var sub = redis.createClient();
sub.on("message", function (channel, data) {
        data = JSON.parse(data);
        console.log("Inside Redis_Sub: data from channel " + channel + ": " + (data.sendType));
        if (parseInt("sendToSelf".localeCompare(data.sendType)) === 0) {
             io.emit(data.method, data.data);
        }else if (parseInt("sendToAllConnectedClients".localeCompare(data.sendType)) === 0) {
             io.sockets.emit(data.method, data.data);
        }else if (parseInt("sendToAllClientsInRoom".localeCompare(data.sendType)) === 0) {
            io.sockets.in(channel).emit(data.method, data.data);
        }       

    });

io.sockets.on('connection', function (socket) {

    sub.on("subscribe", function(channel, count) {
        console.log("Subscribed to " + channel + ". Now subscribed to " + count + " channel(s).");
    });

    socket.on("setUsername", function (data) {
        console.log("Got 'setUsername' from client, " + JSON.stringify(data));
        var reply = JSON.stringify({
                method: 'message',
                sendType: 'sendToSelf',
                data: "You are now online"
            });     
    });

    socket.on("createRoom", function (data) {
        console.log("Got 'createRoom' from client , " + JSON.stringify(data));
        sub.subscribe(data.room);
        socket.join(data.room);     

        var reply = JSON.stringify({
                method: 'message', 
                sendType: 'sendToSelf',
                data: "Share this room name with others to Join:" + data.room
            });
        pub.publish(data.room,reply);


    });
    socket.on("joinRooom", function (data) {
        console.log("Got 'joinRooom' from client , " + JSON.stringify(data));
        sub.subscribe(data.room);
        socket.join(data.room);     

    });
    socket.on("sendMessage", function (data) {
        console.log("Got 'sendMessage' from client , " + JSON.stringify(data));
        var reply = JSON.stringify({
                method: 'message', 
                sendType: 'sendToAllClientsInRoom',
                data: data.user + ":" + data.msg 
            });
        pub.publish(data.room,reply);

    });

    socket.on('disconnect', function () {
        sub.quit();
        pub.publish("chatting","User is disconnected :" + socket.id);
    });

  });

HTML Code

HTML代码

<html>
<head>
    <title>Socket and Redis in Node.js</title>
    <script src="/socket.io/socket.io.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
</head>
<body>
<div id="username">
    <input type="text" name="usernameTxt" /> 
    <input type="button" name="setUsername" value="Set Username" />
</div>
<div id="createroom" style="display:none;">>
    <input type="text" name="roomNameTxt" /> 
    <input type="button" name="setRooomName" value="Set Room Name" />
    <input type="button" name="joinRooomName" value="Join" />
</div>
<div id="sendChat" style="display:none;">
    <input type="text" name="chatTxt" /> 
    <input type="button" name="sendBtn" value="Send" />
</div>
<br />
<div id="content"></div>
<script>    
    $(document).ready(function() {
        var username = "anonymous";
        var roomname = "anonymous";
        $('input[name=setUsername]').click(function(){
            if($('input[name=usernameTxt]').val() != ""){
                username = $('input[name=usernameTxt]').val();
                //var msg = {type:'setUsername',user:username};
                socket.emit('setUsername',{user:username});
            }
            $('#username').slideUp("slow",function(){
                $('#createroom').slideDown("slow");
            });
        });
        $('input[name=setRooomName]').click(function(){
            if($('input[name=roomNameTxt]').val() != ""){
                roomname = $('input[name=roomNameTxt]').val();
                socket.emit('createRoom',{user:username,room:roomname});
            }
            $('#createroom').slideUp("slow",function(){
                $('#sendChat').slideDown("slow");
            });
        });
        $('input[name=joinRooomName]').click(function(){
            if($('input[name=roomNameTxt]').val() != ""){
                roomname = $('input[name=roomNameTxt]').val();
                socket.emit('joinRooom',{room:roomname});
            }
            $('#createroom').slideUp("slow",function(){
                $('#sendChat').slideDown("slow");
            });
        });

        var socket = new io.connect('http://localhost:8088');
        var content = $('#content');

        socket.on('connect', function() {
            console.log("Connected");
        });

        socket.on('message', function(message){
            //alert('received msg=' + message);
            content.append(message + '<br />');
        }) ;

        socket.on('disconnect', function() {
            console.log('disconnected');
            content.html("<b>Disconnected!</b>");
        });

        $("input[name=sendBtn]").click(function(){
            var msg = {user:username,room:roomname,msg:$("input[name=chatTxt]").val()}
            socket.emit('sendMessage',msg);
            $("input[name=chatTxt]").val("");
        });
    });
</script>
</body>
</html>

#2


3  

That's all code basic redis pub/sub.

这是基本的redis pub/sub代码。

var redis = require("redis");

var pub = redis.createClient();
var sub = redis.createClient();

sub.on("subscribe", function(channel, count) {
    console.log("Subscribed to " + channel + ". Now subscribed to " + count + " channel(s).");
});

sub.on("message", function(channel, message) {
    console.log("Message from channel " + channel + ": " + message);
});

sub.subscribe("tungns");

setInterval(function() {
    var no = Math.floor(Math.random() * 100);
    pub.publish('tungns', 'Generated Chat random no ' + no);
}, 5000);

#3


1  

If you need to get your feet wet and create a simple chat application, then the following development stack works very nicely together:

如果您需要弄湿您的脚并创建一个简单的聊天应用程序,那么下面的开发堆栈可以很好地结合在一起:

  • Express.js (Node.js)
  • 表达。js(node . js)
  • Redis
  • 复述,
  • Socket.IO
  • socket . io

The application consists of a chat room that users can join and start up a conversation. Socket.IO is responsible for emitting events when the chatter count/messages are updated and these are refreshed in the UI using jQuery.

该应用程序由一个聊天室组成,用户可以加入该聊天室并开始对话。套接字。IO负责在聊天计数/消息更新并使用jQuery在UI中刷新时发出事件。

For the full article and the source code, check out the following link: https://scalegrid.io/blog/using-redis-with-node-js-and-socket-io/

关于完整的文章和源代码,请查看以下链接:https://scalegrid.io/blog/using-redis-with-node- jandsocket -io/

#1


15  

After reading below articles/blog post, using Redis for pub/sub over socket.io pub/sub will help in scalability and better performance.

在阅读下面的文章/博客文章后,使用Redis作为pub/sub over socket。io pub/sub将有助于提高可扩展性和更好的性能。

https://github.com/sayar/RedisMVA/blob/master/module6_redis_pubsub/README.md

https://github.com/sayar/RedisMVA/blob/master/module6_redis_pubsub/README.md

https://github.com/rajaraodv/redispubsub

https://github.com/rajaraodv/redispubsub

Further I am able to create a quick POC on private chat using redis . Here is the code:-

此外,我还可以使用redis在私人聊天上创建一个快速POC。这是代码:-

var app = require('http').createServer(handler);
app.listen(8088);
var io = require('socket.io').listen(app);
var redis = require('redis');
var redis2 = require('socket.io-redis');
io.adapter(redis2({ host: 'localhost', port: 6379 }));
var fs = require('fs');

function handler(req,res){
    fs.readFile(__dirname + '/index.html', function(err,data){
        if(err){
            res.writeHead(500);
            return res.end('Error loading index.html');
        }
        res.writeHead(200);
        console.log("Listening on port 8088");
        res.end(data);
    });
}

var store = redis.createClient();   
var pub = redis.createClient();
var sub = redis.createClient();
sub.on("message", function (channel, data) {
        data = JSON.parse(data);
        console.log("Inside Redis_Sub: data from channel " + channel + ": " + (data.sendType));
        if (parseInt("sendToSelf".localeCompare(data.sendType)) === 0) {
             io.emit(data.method, data.data);
        }else if (parseInt("sendToAllConnectedClients".localeCompare(data.sendType)) === 0) {
             io.sockets.emit(data.method, data.data);
        }else if (parseInt("sendToAllClientsInRoom".localeCompare(data.sendType)) === 0) {
            io.sockets.in(channel).emit(data.method, data.data);
        }       

    });

io.sockets.on('connection', function (socket) {

    sub.on("subscribe", function(channel, count) {
        console.log("Subscribed to " + channel + ". Now subscribed to " + count + " channel(s).");
    });

    socket.on("setUsername", function (data) {
        console.log("Got 'setUsername' from client, " + JSON.stringify(data));
        var reply = JSON.stringify({
                method: 'message',
                sendType: 'sendToSelf',
                data: "You are now online"
            });     
    });

    socket.on("createRoom", function (data) {
        console.log("Got 'createRoom' from client , " + JSON.stringify(data));
        sub.subscribe(data.room);
        socket.join(data.room);     

        var reply = JSON.stringify({
                method: 'message', 
                sendType: 'sendToSelf',
                data: "Share this room name with others to Join:" + data.room
            });
        pub.publish(data.room,reply);


    });
    socket.on("joinRooom", function (data) {
        console.log("Got 'joinRooom' from client , " + JSON.stringify(data));
        sub.subscribe(data.room);
        socket.join(data.room);     

    });
    socket.on("sendMessage", function (data) {
        console.log("Got 'sendMessage' from client , " + JSON.stringify(data));
        var reply = JSON.stringify({
                method: 'message', 
                sendType: 'sendToAllClientsInRoom',
                data: data.user + ":" + data.msg 
            });
        pub.publish(data.room,reply);

    });

    socket.on('disconnect', function () {
        sub.quit();
        pub.publish("chatting","User is disconnected :" + socket.id);
    });

  });

HTML Code

HTML代码

<html>
<head>
    <title>Socket and Redis in Node.js</title>
    <script src="/socket.io/socket.io.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js"></script>
</head>
<body>
<div id="username">
    <input type="text" name="usernameTxt" /> 
    <input type="button" name="setUsername" value="Set Username" />
</div>
<div id="createroom" style="display:none;">>
    <input type="text" name="roomNameTxt" /> 
    <input type="button" name="setRooomName" value="Set Room Name" />
    <input type="button" name="joinRooomName" value="Join" />
</div>
<div id="sendChat" style="display:none;">
    <input type="text" name="chatTxt" /> 
    <input type="button" name="sendBtn" value="Send" />
</div>
<br />
<div id="content"></div>
<script>    
    $(document).ready(function() {
        var username = "anonymous";
        var roomname = "anonymous";
        $('input[name=setUsername]').click(function(){
            if($('input[name=usernameTxt]').val() != ""){
                username = $('input[name=usernameTxt]').val();
                //var msg = {type:'setUsername',user:username};
                socket.emit('setUsername',{user:username});
            }
            $('#username').slideUp("slow",function(){
                $('#createroom').slideDown("slow");
            });
        });
        $('input[name=setRooomName]').click(function(){
            if($('input[name=roomNameTxt]').val() != ""){
                roomname = $('input[name=roomNameTxt]').val();
                socket.emit('createRoom',{user:username,room:roomname});
            }
            $('#createroom').slideUp("slow",function(){
                $('#sendChat').slideDown("slow");
            });
        });
        $('input[name=joinRooomName]').click(function(){
            if($('input[name=roomNameTxt]').val() != ""){
                roomname = $('input[name=roomNameTxt]').val();
                socket.emit('joinRooom',{room:roomname});
            }
            $('#createroom').slideUp("slow",function(){
                $('#sendChat').slideDown("slow");
            });
        });

        var socket = new io.connect('http://localhost:8088');
        var content = $('#content');

        socket.on('connect', function() {
            console.log("Connected");
        });

        socket.on('message', function(message){
            //alert('received msg=' + message);
            content.append(message + '<br />');
        }) ;

        socket.on('disconnect', function() {
            console.log('disconnected');
            content.html("<b>Disconnected!</b>");
        });

        $("input[name=sendBtn]").click(function(){
            var msg = {user:username,room:roomname,msg:$("input[name=chatTxt]").val()}
            socket.emit('sendMessage',msg);
            $("input[name=chatTxt]").val("");
        });
    });
</script>
</body>
</html>

#2


3  

That's all code basic redis pub/sub.

这是基本的redis pub/sub代码。

var redis = require("redis");

var pub = redis.createClient();
var sub = redis.createClient();

sub.on("subscribe", function(channel, count) {
    console.log("Subscribed to " + channel + ". Now subscribed to " + count + " channel(s).");
});

sub.on("message", function(channel, message) {
    console.log("Message from channel " + channel + ": " + message);
});

sub.subscribe("tungns");

setInterval(function() {
    var no = Math.floor(Math.random() * 100);
    pub.publish('tungns', 'Generated Chat random no ' + no);
}, 5000);

#3


1  

If you need to get your feet wet and create a simple chat application, then the following development stack works very nicely together:

如果您需要弄湿您的脚并创建一个简单的聊天应用程序,那么下面的开发堆栈可以很好地结合在一起:

  • Express.js (Node.js)
  • 表达。js(node . js)
  • Redis
  • 复述,
  • Socket.IO
  • socket . io

The application consists of a chat room that users can join and start up a conversation. Socket.IO is responsible for emitting events when the chatter count/messages are updated and these are refreshed in the UI using jQuery.

该应用程序由一个聊天室组成,用户可以加入该聊天室并开始对话。套接字。IO负责在聊天计数/消息更新并使用jQuery在UI中刷新时发出事件。

For the full article and the source code, check out the following link: https://scalegrid.io/blog/using-redis-with-node-js-and-socket-io/

关于完整的文章和源代码,请查看以下链接:https://scalegrid.io/blog/using-redis-with-node- jandsocket -io/