Whether you are a beginner or an experienced Laravel user, Laravel Websockets is a great way to add WebSocket support to Laravel applications. With Laravel Websockets you can quickly get real-time functionality working in your Laravel projects. It is designed to be easy to use and provides a practical debug dashboard for fast and easy testing.
In this tutorial, we walk you through the most basic steps to get websockets running.
composer require beyondcode/laravel-websockets
php artisan vendor:publish
command and selecting the beyondcode/laravel-websockets
option.config/websockets.php
file and update the app_id
, app_key
, and app_secret
values with your own. These values will be used to authenticate the websockets server.php artisan websockets:serve
command. This will start a local websockets server that you can connect to from your Laravel application.WebSockets
facade to interact with the websockets server. For example, to send a message to a specific channel, you can use the WebSockets::to
method:WebSockets::to('my-channel')->send('Hello, world!');
WebSockets::on
method. This method takes a channel name and a callback function that will be executed when a message is received on that channel.WebSockets::on('my-channel', function ($message) {
// Do something with the incoming message...
});
Overall, using websockets with Laravel is a straightforward process. The Laravel Websockets package makes it easy to integrate websockets into your Laravel application, and provides a convenient facade for sending and receiving messages. With a little bit of setup and configuration, you can quickly get started with websockets in Laravel.