pusher / pusher-http-php

PHP library for interacting with the Pusher Channels HTTP API
https://pusher.com/docs/server_api_guide
1.42k stars 309 forks source link

Pusher Trigger Problem when i sent the message Broadcast->dispatch is not working #326

Closed mobarokhossen closed 2 years ago

mobarokhossen commented 2 years ago

Can please help what is the issue here? I config and setup completely. but Broadcast->dispatch is not working

local.ERROR: {"userId":3,"exception":"[object] (Pusher\ApiErrorException(code: 0): at C:\xampp\htdocs\fm\script\vendor\pusher\pusher-php-server\src\Pusher.php:533) [stacktrace]

0 C:\xampp\htdocs\fm\script\vendor

ahid\talk\src\Live\Webcast.php(55): Pusher\Pusher->trigger(Array, 'talk-send-messa...', Array)

Webcast.php

       class Webcast implements ShouldQueue
{
    use InteractsWithQueue, Queueable, SerializesModels;

    /*
   * Message Model Instance
   *
   * @var object
   * */
    protected $message;

    /*
     * Broadcast class instance
     *
     * @var object
     * */
    protected $broadcast;

    /**
     * Set message collections to the properties.
     */
    public function __construct($message)
    {
        $this->message = $message;
    }

    /*
     * Execute the job and broadcast to the pusher channels
     *
     * @param \Nahid\Talk\Live\Broadcast $broadcast
     * @return void
     */
    public function handle(Broadcast $broadcast)
    {
        $this->broadcast = $broadcast;
        $senderIdKey = $this->broadcast->getConfig('user.ownerKey') ? $this->broadcast->getConfig('user.ownerKey') : 'id';
        $toUser = ($this->message['sender'][ $senderIdKey] == $this->message['conversation']['user_one']) ? $this->message['conversation']['user_two'] : $this->message['conversation']['user_one'];

        $channelForUser = $this->broadcast->getConfig('broadcast.app_name').'-user-'.$toUser;
        $channelForConversation = $this->broadcast->getConfig('broadcast.app_name').'-conversation-'.$this->message['conversation_id'];

//        try {

            $this->broadcast->pusher->trigger([sha1($channelForUser), sha1($channelForConversation)], 'talk-send-message', $this->message);
//        } catch (ApiErrorException $e) {
//            throw new BroadcastException(
//                sprintf('Pusher error: %s.', $e->getMessage())
//            );
//        }
    }
}

Broadcast.php


class Broadcast
{
    use DispatchesJobs;

    /*
     * Constant for talk config prefix
     *
     * @const string
     * */
    const CONFIG_PATH = 'talk';

    /*
   * Set all configs from talk configurations
   *
   * @var array
   * */
    protected $config;

    /*
   * Pusher instance
   *
   * @var object
   * */
    public $pusher;

    /**
     * Connect pusher and get all credentials from config.
     *
     * @param \Illuminate\Contracts\Config\Repository $config
     */
    public function __construct(Repository $config)
    {
        $this->config = $config;
        $this->pusher = $this->connectPusher();
    }

    /**
     * Make pusher connection.
     *
     * @param array $options
     *
     * @return object | bool
     */
    protected function connectPusher($options = [])
    {
        if ($this->getConfig('broadcast.enable')) {
            $appId = $this->getConfig('broadcast.pusher.app_id');
            $appKey = $this->getConfig('broadcast.pusher.app_key');
            $appSecret = $this->getConfig('broadcast.pusher.app_secret');
            $appOptions = $this->getConfig('broadcast.pusher.options');

            $newOptions = array_merge($appOptions, $options);
            $pusher = new Pusher($appKey, $appSecret, $appId, $newOptions);

            return $pusher;
        }

        return false;
    }

    /**
     * Dispatch the job to the queue.
     *
     * @param \Nahid\Talk\Messages\Message $message
     */
    public function transmission(Message $message)
    {
        if (!$this->pusher) {
            return false;
        }

        $sender = $message->sender->toArray();
        $messageArray = $message->toArray();
        $messageArray['sender'] = $sender;
        $this->dispatch(new Webcast($messageArray));
    }

    /**
     * get specific config from talk configurations.
     *
     * @param  string
     *
     * @return string|array|int
     */
    public function getConfig($name)
    {
        return $this->config->get(self::CONFIG_PATH.'.'.$name);
    }
}
benjamin-tang-pusher commented 2 years ago

Are you using Laravel 9 here?

nausixkiz commented 2 years ago

I faced this problem too Laravel 9 maybe not working

benjamin-tang-pusher commented 2 years ago

Pusher Channels works fine in Laravel 9. If you are using Laravel, Instead of your $this->broadcast->pusher->trigger, can you use Laravel's broadcast(new someEvent(...))->toOthers() instead?

stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you'd like this issue to stay open please leave a comment indicating how this issue is affecting you. Thank you.