jeudi 24 mars 2016

How to broadcast message using Redis and Nodejs with laravel

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

ar 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="http://ift.tt/1Abvuox"></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>



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire