What is WebSocket?
WebSocket connection is a continuous connection between a browser and the server.
It provides a bidirectional communication: the server can send messages to the browser and the browser– the client can respond back via an equivalent connection.
This differs from regular Ajax, which is single one-way communication: only the client can ask stuff from the server.
Why do we need WebSocket?
Because PHP itself does not support WebSocket, We need a layer of indirection to send “server” data to the “client”. In other words, real-time communication can be roughly divided into two steps:
- Laravel -> Indirect Layer
- Indirect Layer -> (via WebSocket) -> Client
It is built using ratchet PHP library
How does WebSocket Works?
When you start the start a Ratchet server that starts listing for connections
Features of WebSocket:
- Chat application
- E-commerce application
- Videoconference applications
- Pusher Replacement
- Persistent connection
- Easy Customization options
- Debugging dashboard
- Integrated WebSocket and HTTP Server
– PHP >= 7.1.3
– Laravel >=5.7
Guide -How to Install:
- Install laravel WebSockets – composer require beyondcode/laravel-websockets
- Publish the migration file – php artisan vendor:publish –provider=”BeyondCode\LaravelWebSockets\WebSocketsServiceProvider” –tag=”migrations”
- Run the migrations- php artisan migrate
- Publish the WebSocket configuration file – php artisan vendor:publish –provider=”BeyondCode\LaravelWebSockets\WebSocketsServiceProvider” –tag=”config”
Table : websockets_statistics_entries
Table : Messages
Install the official Pusher PHP SDK
composer require pusher/pusher-php-server “~3.0”
Change broadcast driver in .env file
In the config/app.php file uncomment
The package will use the pusher driver, but we don’t actually want to use Pusher. Therefore we add our own host and port configuration to ‘pusher‘ section in config/broadcasting.php
Laravel WebSockets package comes with individual Pusher API implementation,
we need to notify Laravel to send the events to our own server.
Also Change in .env file beacuse we are not using pusher
Configuring WebSocket Apps
you can host separately from your current Laravel application server
we can add mutiple application with one laravel websocket server
npm install –save laravel-echo pusher-js
Uncommnet the laravel-echo configuration from Resources/js/bootstrap.js
wsHost and wsPort point them to your laravel WebSocket server host and port.
Starting the WebSocket server
php artisan websockets:serve
php artisan websockets:serve —port=3030
php artisan websockets:serve —host=127.0.0.1
Websocket package dashboard is like pusher’s debug console dashboard. Default Root path of dashboard demo app is /laravel-websockets which is automatically accessible.
This package comes with different statistic solutions. That will give you the current status of your WebSocket server.
- Realtime Statistics
- All Event Details
The Laravel WebSockets package use a pusher key and app id but this package use own server to send and received request that’s why this called pusher replacement package.