skip to Main Content

I am very new to the laravel Event Broadcasting Concept ,i have some requirement without refreshing page i need to update the data in Angular site.i made a presence channel by using pusher.i called my broadcast in my queue class once it’s triggered i got an API message from the Pusher Debugger console with what data it’s triggered until this part fine.First of all i want to know weather server side implementation code and logic was correct if wrong means can you help to fix ?

broadcast(new Message('57585');
<?php

namespace AppEvents;

use Config;
use IlluminateBroadcastingChannel;
use IlluminateBroadcastingInteractsWithSockets;
use IlluminateBroadcastingPresenceChannel;
use IlluminateBroadcastingPrivateChannel;
use IlluminateContractsBroadcastingShouldBroadcast;
use IlluminateContractsBroadcastingShouldBroadcastNow;
use IlluminateContractsQueueShouldQueue;
use IlluminateFoundationEventsDispatchable;
use IlluminateQueueSerializesModels;
use IlluminateSupportFacadesArtisan;
use IlluminateSupportFacadesBroadcast;
use IlluminateSupportFacadesConfig as FacadesConfig;

class Message implements ShouldBroadcastNow
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public $message;

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




    public function broadcastOn()
    {
        return new PresenceChannel('test'.$this->message);
    }

   


}

channels.php

Broadcast::channel('test.{message}', function ($user, $message) {
        return true;
});

I am getting in Pusher console like my main doubt is weather this implementation is correct or not.if possible can you give some brief about concept ?

2

Answers


  1. I can able to see some problems with this code channel name is correctly concatinated with dot in channels.php but not where your’e returning presence channel so modify your code like in Event Class(Message.php)

    public function broadcastOn()
        {
            return new PresenceChannel('test.'.$this->message); 
                            or
            return new PresenceChannel('test'.'.'.$this->message);
        }
    

    And after this your’e using presence channel so you must authorize the user and return the user details if the user is valid otherwise null,simply can’t able to return true or false According to laravel docs.

    Broadcast::channel('test.{message}', function ($user, $message) {
            $user_valid = //write your authorization logic which will return boolean
            if($user_valid){
                return $user ; //if you don't want entire user object we can pass specific variables inside array
             }
    });
    

    For code cleaner you can write Authorization logic inside Channel Class or you can create in User model as per your wish.For more info Check docs

    Hope This will work ..!

    Login or Signup to reply.
  2. You need to be authenticated when you use presence channel because technically Presence channel is using private channel docs here

    Broadcast::channel('test.{message}', function ($user, $message) {
        if (auth()->check()){
            return $user;
         } 
    });
    

    And you have a bit of mistake in you broadcastOn method, it should be:

    public function broadcastOn()
    {
        return new PresenceChannel('test.'.$this->message);
    }
    

    I have created a free websocket server that uses pusher protocol v7 you might be interested. https://soket.uk

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search