1

I am using Redis+laravel+nodejs to broadcast message . My Code is as follow

In controller

function postReassignlead(Request $request){
    $data = Input::get();
    $leadId = $data['leadid'];
    $assignedtoid = $data['assignedtoid'];
    $assignedbyid = $data['assignedbyid'];

    $message = "A lead :".$leadId," has been assigned to YOU by ".$assignedbyid."";

    $redis = LRedis::connection();
    $redis->publish('message', $message);

}

and in my server.js

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
var redis = require('redis');

server.listen(8890);

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

  console.log("new client connected");
  var redisClient = redis.createClient();
  redisClient.subscribe('message');

  redisClient.on("message", function(channel, message) {
    console.log("mew message in queue "+ message + "channel");
    socket.emit(channel, message);
    //res.flush();
  });

  socket.on('disconnect', function() {
    redisClient.quit();
  });

});

then on home.php page

    <script src="//code.jquery.com/jquery-migrate-1.2.1.min.js"></script>
    <script src="https://cdn.socket.io/socket.io-1.3.4.js"></script>

    <div class="container">
        <div class="row">
            <div class="col-lg-8 col-lg-offset-2">
              <div id="messages"></div>
            </div>
        </div>
    </div>
    <script>
        var socket = io.connect('http://localhost:8890');
        socket.on('message', function (data) {
            $( "#messages" ).append( "<p>"+data+"</p>" );
        });
    </script>

Here the message is being broadcasted to all the user who are logged in , But I want to change this to only one user

something I can broad to particular user id

Like

    $redis = LRedis::connection();
    $redis->publish('message_1990', $message);

so in my home.php

I can have the code something like this

 <script>
        var socket = io.connect('http://localhost:8890');
        socket.on('message_1990', function (data) {
            $( "#message_1990" ).append( "<p>"+data+"</p>" );
        });
 </script>
Tirth Patel
  • 5,443
  • 3
  • 27
  • 39
Vikram Anand Bhushan
  • 4,836
  • 15
  • 68
  • 130

1 Answers1

0

Doing some research on Redis for my own project and came across your question..

From what I understand, the first parameter of the publish and subscribe methods is the "channel" that is being used for communication. So in your case you would need only update 'message' to 'message_1990' on both the sending and receiving ends.

Redis - Laravel

This would mean that your Laravel app would know which channel to publish the messages to, which may not always be easily determined, or maybe the message is a team broadcast message.

For flexibility you could pass the message in JSON form, then on the client simply unpackage to determine delivery/display. This way the message is delivered to everyone and the client can decide what to do with it.

Pass PHP array to Javascript

So within the Laravel controller:

$message = "A lead :".$leadId," has been assigned to YOU by ".$assignedbyid."";

$redis = LRedis::connection();

// PHP array to convert to JSON, add other fields as necessary
$msgPacket = ["rx"=>"bsmith","msgtype"=>"leadGen","message"=>$message];

// send as a JSON data packet
$redis->publish('message', json_encode($msgPacket) );

On the client side unpackage the JSON data to determine what to do with it:

<script>
    socket.on('message', function (data) {
        obj = JSON.parse(data);

        // receive team and personal messages
        if( (obj.msgtype == "leadGen") || (obj.rx == "bsmith") ) {
            $( "#message" ).append( "<p>"+data+"</p>" );
        }
        // else ignore the message

    });
</script>

Here I've used JSON.parse() but it is not always necessary and the data is also accessible through the data variable, as in data.msgtype

Community
  • 1
  • 1
RobinM
  • 141
  • 2
  • 6
  • hehe thanks for the reply I applied similar approach instead of the Json data I sent one single string with separated with `_` and in the front end I just slit it into JS array and checked it the `ID` of the message is same as `Auth::user->ID` Then the message is appended to its popup :) . But here every user is listening to the message socket , is there a way every user listen to there own socket so that when we are publishing it goes only to that particular user ?? – Vikram Anand Bhushan Apr 04 '16 at 09:34