jeudi 28 septembre 2017

Laravel event broadcasting with socketio and redis is not working in laravel 5.4?

I am trying to show realtime message notification count when user logged in to the website. For this I am using socketio, redis and laravel event. But it is not working. In laravel log I am getting a message. In this message count is showing but socket is null. I think I miss something. My gitbash showing channel subscribed and user is connected.

Gitbash

vagrant@homestead:~/Code/cabinapi$ node socket.js
    listening on *:3000
    Redis: message-channel subscribed
    a user connected

Laravel log

[2017-09-28 07:21:57] local.INFO: Broadcasting [App\Events\MessageEvent] on channels [message-channel] with payload:
{
    "count": 9,
    "socket": null
}

Controller

public function privateMessageAPICount($id)
{
        $count = PrivateMessage::where('receiver_id', new \MongoDB\BSON\ObjectID($id))
            ->where('read', 0)
            ->count();

        if ($count > 0){
            event(new MessageEvent($count));
        }
}

routes->web.php

Route::get('/message/count/{id}', 'Cabinowner\DashboardController@privateMessageAPICount');

.env

APP_URL=http://cabinapi.app
BROADCAST_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

MessageEvent.php

<?php

namespace App\Events;

use Illuminate\Broadcasting\Channel;
use Illuminate\Queue\SerializesModels;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;

class MessageEvent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public $count;
    public function __construct($count)
    {
        $this->count = $count;
    }

    public function broadcastOn()
    {
        return ['message-channel'];
    }
}

views->owner->layouts->app.blade.php

<body>
<span class="label label-success msgSpan"><span class="msgCountRemove">{!! $miscellaneous->privateMessageCount() !!}</span></span>

<script src=""></script>
<script src="http://ift.tt/2xyMXyo"></script>
    <script>
        var socket = io(':3000');
        socket.on('message-channel:App\\Events\\MessageEvent', function(data){
            $('.msgCountRemove').remove();
            $('.msgSpan').append('<span class="msgCountRemove">'+data.count+'</span>');
        });
    </script>

</body>

config->app.php

'Redis' => Illuminate\Support\Facades\Redis::class,

socket.js

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('message-channel', function () {
    console.log('Redis: message-channel subscribed');
});
redis.on('message', function(channel, message) {
    console.log('Redis: Message on ' + channel + ' received!');
    console.log(message);
    message = JSON.parse(message);
    io.emit(channel + ':' + message.event, message.data);
});

io.on('connection', function(socket){
    console.log('a user connected');
    socket.on('disconnect', function(){
        console.log('user disconnected');
    });
});
http.listen(3000, function(){
    console.log('listening on *:3000');
});



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire