Reputation: 29116
I am surprised that I need third-party services such as Pusher or Redis to have a bidirectional communication from my server to my clients through WebSockets.
What are the advantages of Pusher over Redis or simply a socker.io
server aside from nginx
? I see many disadvantages:
From my understanding, they are only two possible solutions with Laravel:
Is there a third alternative?
Upvotes: 2
Views: 8196
Reputation: 2588
It's posting years after your question.
But Nowadays, We can use the power of amazing Laravel Reverb
php artisan install:broadcasting
The install:broadcasting
Artisan command will run the reverb:install
command, which will install Reverb with a sensible set of default configuration options.
Then You may define these credentials using the following environment variables:
REVERB_APP_ID=my-app-id
REVERB_APP_KEY=my-app-key
REVERB_APP_SECRET=my-app-secret
To start reverb, you can easily use artisan command like queu or other stuff:
php artisan reverb:start
If you plan to broadcast your events using Pusher Channels, you should install the Pusher Channels PHP SDK using the Composer package manager:
composer require pusher/pusher-php-server
Next, you should configure your Pusher Channels credentials in the config/broadcasting.php
configuration file.
Typically, you should configure your Pusher Channels credentials in your application's .env file:
PUSHER_APP_ID="your-pusher-app-id"
PUSHER_APP_KEY="your-pusher-key"
PUSHER_APP_SECRET="your-pusher-secret"
PUSHER_HOST=
PUSHER_PORT=443
PUSHER_SCHEME="https"
PUSHER_APP_CLUSTER="mt1"
Then, set the BROADCAST_CONNECTION environment variable to pusher in your application's .env file:
BROADCAST_CONNECTION=pusher
You can use Laravel Echo
and pusher-js
for client side. Laravel Echo is a JavaScript library that makes it painless to subscribe to channels and listen for events broadcast by your server-side broadcasting driver.
npm install --save-dev laravel-echo pusher-js
As documentation said:
A great place to do this is at the bottom of the resources/js/bootstrap.js file that is included with the Laravel framework. By default, an example Echo configuration is already included in this file - you simply need to uncomment it and update the broadcaster configuration option to reverb:
import Echo from 'laravel-echo';
import Pusher from 'pusher-js';
window.Pusher = Pusher;
window.Echo = new Echo({
broadcaster: 'reverb',
key: import.meta.env.VITE_REVERB_APP_KEY,
wsHost: import.meta.env.VITE_REVERB_HOST,
wsPort: import.meta.env.VITE_REVERB_PORT,
wssPort: import.meta.env.VITE_REVERB_PORT,
forceTLS: (import.meta.env.VITE_REVERB_SCHEME ?? 'https') === 'https',
enabledTransports: ['ws', 'wss'],
});
Next, you should compile your application's assets:
npm run build
Upvotes: 0
Reputation: 4091
The benefits of using a third party solution are different per use case and per person. However, broadly speaking there are a couple of benefits that haven't been mentioned here that are worth highlighting:
A lot has been said about build vs buy over the years, and there are many resources that discuss the merits of both (in fact Pusher has a resource for this). Ultimately this is not a decision that can be made for you, you will need to assess your application requirements and then look at what best fits your use case.
Upvotes: 2
Reputation: 692
There is a clone of pusher server available on laravel, have you checked it?
https://beyondco.de/docs/laravel-websockets/getting-started/introduction
You don't have to pay anyone, it runs clone of pusher server on your server.
Upvotes: 4