1
votes

I created a basic chat room based on many tutorials with PHP, socket.io, redis, and node. I created a .js that acts as a server to create the web sockets to allow browsers to get the broadcasts and update automatically. Question I have is how to I modify what I have to allow there to be multiple rooms/channels? In the tutorial, it sets the server to subscribe to 'test-channel' but I need to the users to subscribe to different rooms based on who they are supposed to meetup with. Any advice, code hints, or pointing to other tutorials is highly appreciated.

My .js server that node runs:

var server = require('http').Server();
var io = require('socket.io')(server);
var Redis = require('ioredis');
var redis = new Redis();

redis.subscribe('test-channel');

redis.on('message',function(channel, message){
    message = JSON.parse(message);
    io.emit(channel + ':' + message.event_type, message.data);
});

server.listen(3000);

My php file script that just displays the message for now:

<script>
    var socket = io('http://localhost:3000');
    socket.on('test-channel:CampaignJobCompleted', function (data) {
        console.log(data);
        $( "#messages" ).append( "<p>"+data.message+"</p>" );
    });
</script>

My Laravel controller that broadcasts the data:

$data = [
    'event_type' => 'CampaignJobCompleted',
    'data' => [
        'campaign_name' => 'Sample Campaign',
        'message' => 'I am sending a message'
    ]
];

\LRedis::publish('test-channel',json_encode($data));

The room/channel is predecided when the user asks for a meetup with another person so I would want to inject the channel name before they enter the room.

Thanks for any help anyone can offer.

1

1 Answers

0
votes

I think you want to make the different chatroom for particulars users like io.in('chatroom').emit(data) ? If so you need to get the user id first that is socket id and insert the id into specific chatroom you can add the user id and delete the id via javascript slice function hope this would help? Main theme would be on some event you would like to collect the user information like id and saving all user id in empty array then on any event make id and let them enter to that named chatroom ......