0

I am using BROADCAST_DRIVER=redis to trigger laravel 5.2 event. Once i run the following services on command prompt :

  1. In the first tab, run node socket.js

    You should see "Listening on Port 3000"

  2. In the second tab run redis-server --port 3001

After it Open up two browser windows side by side and in the first one hit the URL: "http://your-project-name.app/fire"

And in the second: "http://your-project-name.app/test"

Keep refreshing the first window and you should see the second page's content updated.

But i do not want to refreshing the page , i just want to trigger the broadcast event in background and also do not want to run the services "node socket.js and redis-server --port 3004".

I have installed node,redis,express ioredis socket.io and created the event.

My socket code:

var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var Redis = require('ioredis');
var redis = new Redis();
redis.subscribe('test-channel', function(err, count) { });
redis.on('message', function(channel, message) { 
  console.log('Message Recieved: ' + message);
  message = JSON.parse(message);
  io.emit(channel + ':' + message.event, message.data);
});

http.listen(3004, function(){
  console.log('Listening on Port 3004');
});
1
  • my socket.js code is var app = require('express')(); var http = require('http').Server(app); var io = require('socket.io')(http); var Redis = require('ioredis'); var redis = new Redis(); redis.subscribe('test-channel', function(err, count) { }); redis.on('message', function(channel, message) { console.log('Message Recieved: ' + message); message = JSON.parse(message); io.emit(channel + ':' + message.event, message.data); }); http.listen(3004, function(){ console.log('Listening on Port 3004'); }); Commented Jun 3, 2016 at 10:25

1 Answer 1

1

You need to use Redis Pub/Sub

These Redis commands allow you to listen for messages on a given "channel". You may publish messages to the channel from another application, or even using another programming language, allowing easy communication between applications / processes.

First, let's setup a listener on a channel via Redis using the subscribe method. We will place this method call within an Artisan command since calling the subscribe method begins a long-running process:

    <?php

    namespace App\Console\Commands;

    use Redis;
    use Illuminate\Console\Command;

    class RedisSubscribe extends Command
    {
        /**
         * The name and signature of the console command.
         *
         * @var string
         */
        protected $signature = 'redis:subscribe';

        /**
         * The console command description.
         *
         * @var string
         */
        protected $description = 'Subscribe to a Redis channel';

        /**
         * Execute the console command.
         *
         * @return mixed
         */
        public function handle()
        {
            Redis::subscribe(['test-channel'], function($message) {
                echo $message;
            });
        }
    }

Go to terminal, in your root folder launch the command

 php artisan redis:subscribe

Now, we may publish messages to the channel using the publish method:

 Redis::publish('test-channel', json_encode(['foo' => 'bar']));

This method doesn't use nodejs.

Sign up to request clarification or add additional context in comments.

5 Comments

for receiving end i am using the following code:: <script src="cdn.socket.io/socket.io-1.3.5.js"></script> <script src="ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"> var socket = io('localhost:3004'); socket.on("test-channel:App\\Events\\EventName", function (message) { $('#power').text(parseInt($('#power').text()) + parseInt(message.data.power)); }); </script> And also i have event code in laravel 5.2, What next i need to do, Can you please give me some direction? Eagerly waiting for your valuable response!!
My problem is to trigger the event in background automatically in specific time interval. I have everything working but when i trigger it manually .
Pub/sub is that you need, forgot nodejs. In laravel.com/docs/5.2/redis#pubsub you have the example. First create a command to listen and after send message to channel with Redis::publish()
If you want to trigger it in background, you must don't use nodejs, because is only to trigger events in your frontend, not in background. Change for the method I showed you
I will implement your solution in future and let you know , Thanks for your assistance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.