Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

laravel websockets onmessage

// 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);
      	// Your code here...
    }
}
 
PREVIOUS NEXT
Tagged: #laravel #websockets #onmessage
ADD COMMENT
Topic
Name
7+9 =