- VolkomenJuist.nl - http://www.volkomenjuist.nl/blog -

Laravel 4 and NodeJs/Redis pub/sub realtime notifications

Currently I am building an application where we can fill in live scores and I needed something to update all my visitors whenever a score has been updated by one of the admins.

Whenever an admin updates the score via the Laravel 4 backend I fire an event and publish it to Redis. I’ve setup a simple NodeJS server which listens to Redis for incoming changes. NodeJS will redirect the message to all Socket.IO clients.


1. Open config//database.php
2. Configure:

'redis' => array(

		'cluster' => false, //publish won't work when set to true

		'default' => array(
			'host'     => '',
			'port'     => 6379,
			'database' => 0,


3. Create app\events folder and update composer.json

"autoload": {
	"classmap": [

4. Create UpdateScoreEventHandler.php in the events folder:

class UpdateScoreEventHandler {

    CONST EVENT = 'score.update';
    CONST CHANNEL = 'score.update';

    public function handle($data)
        $redis = Redis::connection();
        $redis->publish(self::CHANNEL, $data);

5.Create listeners.php (next to routes.php/filters.php):

Event::listen(UpdateScoreEventHandler::EVENT, 'UpdateScoreEventHandler');

6. Add to start/global.php:

require app_path().'/listeners.php';

7. Fire event in controller (update score) :

Event::fire(UpdateScoreEventHandler::EVENT, array($updatedMatch));

8. Run composer update (class loading)


1. Download NodeJS via the website (http://nodejs.org/ [1])
2. Create a nodejs folder in Laravel’s root.
3. /nodejs/npm install socket.io express redis
4. Create nodejs server (e.g. server.js) and store it /nodejs

var express =   require('express'),
    http =      require('http'),
    server =    http.createServer(app);

var app = express();

const redis =   require('redis');
const io =      require('socket.io');
const client =  redis.createClient();

server.listen(3000, 'localhost');

io.listen(server).on('connection', function(client) {
    const redisClient = redis.createClient()

    redisClient.on("message", function(channel, message) {
        //Channel is e.g 'score.update'
        client.emit(channel, message);

    client.on('disconnect', function() {


1. Include Socket.io client in your page (https://github.com/LearnBoost/socket.io/releases [2])
2. Add code below to receive messages when a score has been updated.

<script type="text/javascript">// <![CDATA[
            var socket = io.connect('');

            //socket.on('connect', function(data){
            //    socket.emit('subscribe', {channel:'score.update'});

            socket.on('score.update', function (data) {
                //Do something with data
                console.log('Score updated: ', data);

// ]]></script>

5. Start the server

/nodejs/node server.js


1. Download Redis (http://redis.io/download [3])
2. Go to extacted folder
3. Go to src folder
4. Start the server


Optional – Monitor Redis (requests)

1. ./redis-cli monitor

Buffer [4]