Using AnyCable with Laravel
AnyCable can be used as a WebSocket server for Laravel applications using Laravel Echo and Laravel Broadcasting capabilities. Consider it a drop-in replacement for Laravel Reverb, or Pusher, or whatever you use today.
Why choosing AnyCable over Reverb et al?
AnyCable is a battle-proofed real-time server that's been in production at scale for many years. It comes with extensive features set (reliability, various protocols support, observability tools, etc.) and it's free to use.
You can use AnyCable server in a Pusher mode or natively using a custom broadcasting and Echo adapter. In the latter, you can benefit from such AnyCable features as streams history and resumeable sessions (see Reliable streams).
Pusher mode
Switching to AnyCable can be as simple as replacing the command to run a WebSocket server (if you use Reverb):
# before
$ REVERB_APP_ID=app-id \
REVERB_APP_KEY=app-key \
REVERB_APP_SECRET=app-secret \
php artisan reverb:start
# after
$ ANYCABLE_PUSHER_APP_ID=app-id \
ANYCABLE_PUSHER_APP_KEY=app-key \
ANYCABLE_SECRET=app-secret \
anycable-go
And that's it! AnyCable also runs on port 8080 by default, so no changes required.
To give AnyCable a quick try, consider using our free managed service AnyCable+.
To run AnyCable locally, you can use the anycable-laravel package that provides the following command:
php artisan anycable:server
When running with this command, AnyCable automatically recognizes the following Reverb environment variables and uses them: REVERB_APP_ID
, REVERB_APP_KEY
, REVERB_APP_SECRET
.
AnyCable mode
Check out our demo Laravel application to see the complete example: laravel-anycable-demo
To fully benefit from AnyCable features, we recommend switching to use our client library. We also provide an Echo adapter that provides a familiar interface while using AnyCable JS SDK under the hood.
First, install the anycable-laravel package and configure the broadcasting backend—you will need to authorize private and presence channels and publish events:
composer require anycable/laravel-broadcaster
Then, configure the application to use anycable
broadcasting driver. For that, add the AnyCable service provider to the bootstrap/providers.php
file:
<?php
return [
App\Providers\AppServiceProvider::class,
// ...
+ AnyCable\Laravel\Providers\AnyCableBroadcastServiceProvider::class,
];
Then, register the anycable
driver in your config/broadcasting.php
file:
'anycable' => [
'driver' => 'anycable',
],
Now, install the @anycable/echo
JS package and configure your Echo instance:
import Echo from "laravel-echo";
import { EchoCable } from "@anycable/echo";
window.Echo = new Echo({
broadcaster: EchoCable,
cableOptions: {
url: url: import.meta.env.VITE_WEBSOCKET_URL || 'ws://localhost:8080/cable',
},
// other configuration options such as auth, etc
});
Finally, install AnyCable server. We provide a convenient Artisan command that automatically downloads (when necessary) and runs the server:
php artisan anycable:server
You can specify AnyCable configuration in the .env
file:
ANYCABLE_SECRET=secret
ANYCABLE_PUBLIC=true
: This MUST be set to true to allow connection (however, we highly recommend looking at the JWT Authentication feature).ANYCABLE_PUBLIC_STREAMS=true
: Enables public channels—they're disabled by default.
You can also create an anycable.toml
configuration file to fine-tune your AnyCable server (see docs).
NOTE: The Artisan command automatically configures AnyCable broadcasting adapter to HTTP and enables the "broker" preset (streams history).
Alternatively, you can install AnyCable using other available options.
That's it! Run your Laravel application, launch AnyCable server, and you should see your Echo client connecting to it and receiving updates.
Benchmarks
You can find the benchmarks here: https://github.com/anycable/anycable-laravel/tree/master/benchmarks
tl;dr AnyCable shows slightly better performance and lesser memory usage during broadcast benchmarks compared to Reverb; however, AnyCable handles connection avalanches much better