Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

laravel websockets onsubscribe

// Add websocket routes "AppProvidersRouteServiceProvider.php"
public function map()
{
    $this->mapApiRoutes();

    $this->mapWebRoutes();
  
    $this->app->singleton('websockets.router', function () {
        return new Router();
    });
}

// Create WebSocket Router "AppWebSocketsRoute.php"
class Router extends BeyondCodeLaravelWebSocketsServerRouter
{
    public function echo()
    {
        $this->get('/app/{appKey}', WebSocketHandler::class);
        $this->post('/apps/{appId}/events', TriggerEventController::class);
        $this->get('/apps/{appId}/channels', FetchChannelsController::class);
        $this->get('/apps/{appId}/channels/{channelName}', FetchChannelController::class);
        $this->get('/apps/{appId}/channels/{channelName}/users', FetchUsersController::class);
    }
}

// Create WebSocket Router "AppWebSocketsWebSocketHandler.php"
class WebSocketHandler extends BeyondCodeLaravelWebSocketsWebSocketsWebSocketHandler
{
    public function onMessage(ConnectionInterface $connection, MessageInterface $message)
    {
        parent::onMessage($connection, $message);
        $message = json_decode($message->getPayload(), true);
        if ($message['event'] === 'pusher:subscribe') { 
      		// Your code here...
        }
    }
}
 
PREVIOUS NEXT
Tagged: #laravel #websockets #onsubscribe
ADD COMMENT
Topic
Name
4+1 =