laravel websocket code example
Example 1: laravel websockets onmessage
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
$this->app->singleton('websockets.router', function () {
return new Router();
});
}
class Router extends \BeyondCode\LaravelWebSockets\Server\Router
{
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);
}
}
class WebSocketHandler extends \BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler
{
public function onMessage(ConnectionInterface $connection, MessageInterface $message)
{
parent::onMessage($connection, $message);
}
}
Example 2: laravel 8 websockets
you can watch the full video: https:
Example 3: laravel websockets onsubscribe
public function map()
{
$this->mapApiRoutes();
$this->mapWebRoutes();
$this->app->singleton('websockets.router', function () {
return new Router();
});
}
class Router extends \BeyondCode\LaravelWebSockets\Server\Router
{
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);
}
}
class WebSocketHandler extends \BeyondCode\LaravelWebSockets\WebSockets\WebSocketHandler
{
public function onMessage(ConnectionInterface $connection, MessageInterface $message)
{
parent::onMessage($connection, $message);
$message = json_decode($message->getPayload(), true);
if ($message['event'] === 'pusher:subscribe') {
}
}
}
Example 4: laravel websockets
Example 5: laravel websockets tutorial
php artisan websockets:serve