Authorization for laravel passport through socket.io for broadcasting channels

Posted 1 year ago by anonymox

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

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' => ['auth:api']]);
   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 these broadcasting events in socket server and also how to authorize the user in angular.js (SPA) to listen to these events.

I'd appreciate any help.

Please sign in or create an account to participate in this conversation.