开发者

Multichannel depending on the path with nodejs and redis

开发者 https://www.devze.com 2023-03-16 05:09 出处:网络
i try create a multi channel depending on the path with nodejs in the same port. For exemple (my port is 8080) :

i try create a multi channel depending on the path with nodejs in the same port. For exemple (my port is 8080) :

wwww.exemple.com:8080/channel/1

wwww.exemple.com:8080/channel/2

wwww.exemple.com:8080/channel/3

wwww.exemple.com:8080/channel/4

Each url path channel correspo开发者_如何学编程nds to a channel redis

For exemple (channel-X) :

var channel = 'channel-X'
getRedis.subscribe(channel);

but I do not know how to link the channel and repeat the url.

here is my code to the current time.

socket.on('connection', function(client) {
const getRedis = redis.createClient();
const sendRedis = redis.createClient();
getRedis.subscribe('channel-1');

getRedis.on("message", function(channel, message) {
    client.send(message);
});


client.on('message', function(msg) {
    sendRedis.publish('channel-1',msg);
});


client.on('disconnect', function() {
    getRedis.quit();
    sendRedis.quit();
});

});

I am a bit in the fog all proposals will be welcome:)


You cannot link your channel name to your socket.io. Because socket.io client only carry latest user infor only.

Solution 1: You can pass the channel name from client side.

//Client side


var socket = new io.Socket();

socket.on('connect', function() {

socket.send({ChannelName:'channel-X',Message:'HI !!'});
});




//Server side

socket.on('connection', function(client) {

const getRedis = redis.createClient();


getRedis.on("message", function(channel, message) {
    client.send(message);
});


client.on('message', function(msg) {
    //Assume the input msg is JSON structure {ChannelName:'channel-X',Message:'HI !!'}
    var data = JSON.parse(msg);
     getRedis.subscribe(data.ChannelName);

});

});

Solution 2:

Use session store to store/get the channel name by socket session id.

Problem : I not understand what you need "url to managed subscriptions to channels repeat"

What is channels repeat ?

0

精彩评论

暂无评论...
验证码 换一张
取 消