Skip to content

Instantly share code, notes, and snippets.

@emir-ekin-ors
Created December 6, 2023 09:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emir-ekin-ors/79e670eb6ea970af38c476a8087c19ea to your computer and use it in GitHub Desktop.
Save emir-ekin-ors/79e670eb6ea970af38c476a8087c19ea to your computer and use it in GitHub Desktop.
Websockets implementation
Websockets
https://www.youtube.com/watch?v=pIGy7-7gGXI
https://beyondco.de/docs/laravel-websockets/getting-started/installation
composer require beyondcode/laravel-websockets
If there is a error, try this:
composer require beyondcode/laravel-websockets --with-all-dependencies
php artisan vendor:publish --provider="BeyondCode\LaravelWebSockets\WebSocketsServiceProvider" --tag="migrations"
php artisan vendor:publish --provider="BeyondCode\LaravelWebSockets\WebSocketsServiceProvider" --tag="config"
php artisan migrate
Modify .env file:
* Pusher app id, key, secret and cluster values are random, doesn’t matter
BROADCAST_DRIVER=pusher
PUSHER_APP_ID=74590823740923
PUSHER_APP_KEY=APSDHFNLSDSLNVSQ
PUSHER_APP_SECRET=VNDSMFHGREIUIEROGHFDNS
PUSHER_HOST=http://localhost
PUSHER_PORT=6001
PUSHER_SCHEME=http
PUSHER_APP_CLUSTER=mt1
MIX_PUSHER_APP_KEY="${PUSHER_APP_KEY}"
MIX_PUSHER_APP_CLUSTER="${PUSHER_APP_CLUSTER}"
Modify config/websockets.php:
By default, it should be like this
'apps' => [
[
'id' => env('PUSHER_APP_ID'),
'name' => env('APP_NAME'),
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'path' => env('PUSHER_APP_PATH'),
'capacity' => null,
'enable_client_messages' => false,
'enable_statistics' => true,
],
],
'path' => 'laravel-websockets',
Run:
composer require pusher/pusher-php-server -W
Modify config/broadcasting.php:
'connections' => [
'pusher' => [
'driver' => 'pusher',
'key' => env('PUSHER_APP_KEY'),
'secret' => env('PUSHER_APP_SECRET'),
'app_id' => env('PUSHER_APP_ID'),
'options' => [
'cluster' => env('PUSHER_APP_CLUSTER'),
'host' => env('PUSHER_HOST') ?: 'api-'.env('PUSHER_APP_CLUSTER', 'mt1').'.pusher.com',
'port' => env('PUSHER_PORT', 443),
'scheme' => env('PUSHER_SCHEME', 'https'),
//'encrypted' => true,
//'useTLS' => env('PUSHER_SCHEME', 'https') === 'https',
],
'client_options' => [
// Guzzle client options: https://docs.guzzlephp.org/en/stable/request-options.html
],
],
Run:
php artisan make:Event
ex: php artisan make:Event NewMessage
Modify app/Evenets/NewMessage:
<?php
namespace App\Events;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;
class NewMessage implements ShouldBroadcast
{
use Dispatchable, InteractsWithSockets, SerializesModels;
public $message;
public function __construct($message)
{
$this->message = $message;
}
public function broadcastOn(): array
{
return [
new PrivateChannel('home'),
];
}
}
Run:
php artisan websockets:serve
php artisan serve
Dashboard link: http://localhost:8000/laravel-websockets
We are done. To test if it works, write this into the project terminal:
php artisan tinker
Write this to tinker editor and see if is there any new event in the dashboard:
event(new App\Events\NewMessage("Hello World"))
There should be a event in dashboard
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment