|
| 1 | +# Using AnyCable with Laravel |
| 2 | + |
| 3 | +<p class="preview-badge-header"></p> |
| 4 | + |
| 5 | +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. |
| 6 | + |
| 7 | +Why choosing AnyCable over Reverb et al? |
| 8 | + |
| 9 | +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**. |
| 10 | + |
| 11 | +**NOTE:** Currently, this feature in the preview phase. There is some work in progress (see the limitations below). |
| 12 | + |
| 13 | +## Getting Started |
| 14 | + |
| 15 | +> Check out our demo Laravel application to see the complete example: [laravel-anycable-demo][] |
| 16 | +
|
| 17 | +First, install the [anycable-laravel][] library: |
| 18 | + |
| 19 | +```sh |
| 20 | +composer require anycable-laravel |
| 21 | +``` |
| 22 | + |
| 23 | +Then, configure the application to use `anycable` broadcasting driver. For that, add the AnyCable service provider to the `bootstrap/providers.php` file: |
| 24 | + |
| 25 | +```diff |
| 26 | + <?php |
| 27 | + |
| 28 | + return [ |
| 29 | + App\Providers\AppServiceProvider::class, |
| 30 | + // ... |
| 31 | ++ AnyCable\Laravel\Providers\AnyCableBroadcastServiceProvider::class, |
| 32 | + ]; |
| 33 | +``` |
| 34 | + |
| 35 | +Then, register the `anycable` driver in your `config/broadcasting.php` file: |
| 36 | + |
| 37 | +```php |
| 38 | +'anycable' => [ |
| 39 | + 'driver' => 'anycable', |
| 40 | +], |
| 41 | +``` |
| 42 | + |
| 43 | +AnyCable supports Pusher protocol, so your client-side code stays the same. For example: |
| 44 | + |
| 45 | +```js |
| 46 | +import Echo from "laravel-echo"; |
| 47 | + |
| 48 | +// We use Pusher protocol for now |
| 49 | +import Pusher from "pusher-js"; |
| 50 | +window.Pusher = Pusher; |
| 51 | + |
| 52 | +window.Echo = new Echo({ |
| 53 | + broadcaster: "reverb", // reverb or pusher would work |
| 54 | + key: import.meta.env.VITE_REVERB_APP_KEY, |
| 55 | + wsHost: import.meta.env.VITE_REVERB_HOST, |
| 56 | + wsPort: import.meta.env.VITE_REVERB_PORT ?? 80, |
| 57 | + wssPort: import.meta.env.VITE_REVERB_PORT ?? 443, |
| 58 | + forceTLS: (import.meta.env.VITE_REVERB_SCHEME ?? "https") === "https", |
| 59 | + enabledTransports: ["ws", "wss"], |
| 60 | +}); |
| 61 | +``` |
| 62 | +
|
| 63 | +Just make sure you point to to the AnyCable server (locally it runs on the same host and port as Reverb). |
| 64 | +
|
| 65 | +Finally, install AnyCable server. We provide a convenient Artisan command that automatically downloads (when necessary) and runs the server: |
| 66 | +
|
| 67 | +```sh |
| 68 | +php artisan anycable:server -- --pusher_app_key=my-app-key |
| 69 | +``` |
| 70 | +
|
| 71 | +The `--pusher_app_key` must be the same as the `VITE_REVERB_APP_KEY` for your client. |
| 72 | +To use public channel, you must also provide the `--public_streams` flag. You can also create an `anycable.toml` configuration file (see [docs](/anycable-go/configuration?id=configuration-files)). |
| 73 | +
|
| 74 | +Alternatively, you can install AnyCable using [other available options](/anycable-go/getting_started?id=installation). |
| 75 | +
|
| 76 | +That's it! Run your Laravel application, launch AnyCable server, and you should see your Echo client connecting to it and receiving updates. |
| 77 | +
|
| 78 | +## Limitations |
| 79 | +
|
| 80 | +- Presence channels are yet to be implemented. |
| 81 | +
|
| 82 | +- Only HTTP broadcasting adapter for AnyCable is supported for now. |
| 83 | +
|
| 84 | +[anycable-laravel]: https://github.com/anycable/anycable-laravel |
| 85 | +[laravel-anycable-demo]: https://github.com/anycable/laravel-anycable-demo |
0 commit comments