lundi 30 avril 2018

Authorization for laravel passport through socket.io for broadcasting channels

I'm using laravel 5.3 + passport for authorization, Laravel is my back-end API which is restfull.

front-end is written in angular-js which communicate with API with rest requests.

For Real-time notifications I've used laravel broadcasting events + redis, and socket.io for socket server and socket client in angular-js.

I want to authorize these events and I've done it far as I could :

BroadcastServiceProvider :

 public function boot()
    {
        Broadcast::routes(['middleware' => ['api']]);

        /*
         * Authenticate the user's personal channel...
         */
        Broadcast::channel('App.User.*', function ($user, $userId) {
            return (int) $user->id === (int) $userId;
        });

        Broadcast::channel('notifs.*', function ($user, $userId) {
            return $user->id === (int) $userId;
        });
    }

This is my socket.js code which runs my socket server :

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.psubscribe('*', function(err, count) {});

redis.on('pmessage', function(subscribed, channel, message) {
    console.log(channel);
    message = JSON.parse(message);
    io.emit(channel + ':' + message.event, message.data);
});

http.listen(3000, function () {
    console.log('Listening on Port 3000');
});

redis.on("error", function (err) {
    console.log(err);
});

The problem is I don't know how to authenticate this broadcasting events in socket server and also how to authorize user in angular-js (SPA) to listen to these events.

I'd appreciate any help.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire