nowox
nowox

Reputation: 29116

Using Laravel Events without Pusher nor Redis?

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

Answers (3)

Kiyarash
Kiyarash

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

Client Side Installation

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

doydoy
doydoy

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:

  • Hosted solutions do not require you to implement your own infrastructure to manage the websocket connections. This means you don't need to worry about the uptime, security, provisioning or maintenance of the infrastructure, this is done for you.
  • Hosted solutions scale seamlessly. As your app user base grows and your connections grow, you no longer need to provision more infrastructure and load balance/route connections.
  • Hosted solutions such as Pusher have dedicated support teams to help during implementation/troubleshooting.
  • Hosted solutions often have round the clock server monitoring, ensuring the platform is available 24/7 without the need for you to respond to server alarms in the early hours.

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

There is a clone of pusher server available on laravel, have you checked it?

https://beyondco.de/docs/laravel-websockets/getting-started/introduction

  • You can use this on LAN.
  • This runs a php-socket server on some port like 5000
  • Just use Laravel Echo or Pusher SDK for mobile apps and connect it to your server on 5000 port.

You don't have to pay anyone, it runs clone of pusher server on your server.

Upvotes: 4

Related Questions