mardi 1 mars 2016

Socket.io, PHP, Node, and Redis Chat rooms

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.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire