Laravel Websocket without Pusher: A Comprehensive Guide

Introduction

Websockets are a powerful technology that enables real-time communication between a client and server. Laravel, one of the most popular PHP frameworks, has built-in support for Websockets through the Laravel Websockets package. However, this package relies on Pusher, a third-party service that can be expensive for some developers. In this article, we will explore how to use Laravel Websockets without Pusher.

What are Websockets?

Websockets are a communication protocol that enables two-way communication between a client and server in real-time. With Websockets, the server can push data to the client without the client having to request it. This is in contrast to traditional HTTP requests, where the client requests data from the server and the server responds.

Websockets are ideal for applications that require real-time updates, such as chat applications, real-time games, and collaborative tools. They are also useful for applications that require frequent updates without the need for the client to constantly poll the server.

What is Laravel Websockets?

Laravel Websockets is a package that provides Websockets support for Laravel applications. It is built on top of the Ratchet library, which is a PHP implementation of the Websockets protocol.

Laravel Websockets provides an easy-to-use API for managing Websockets connections, broadcasting events, and managing channels. It also includes a dashboard for monitoring Websockets connections and events.

Why Use Laravel Websockets without Pusher?

Pusher is a popular third-party service that provides Websockets support for applications. However, it can be expensive for some developers, especially if they are building an application that requires a large number of connections or events.

Laravel Websockets provides an alternative to Pusher that is free and open source. By using Laravel Websockets without Pusher, developers can save money and have more control over their application.

Setting Up Laravel Websockets without Pusher

Setting up Laravel Websockets without Pusher requires a few steps:

  1. Install the Laravel Websockets package
  2. Configure the broadcasting driver
  3. Start the Laravel Websockets server

Install the Laravel Websockets Package

The first step is to install the Laravel Websockets package using Composer:

composer require beyondcode/laravel-websockets

This will download and install the package and its dependencies.

Configure the Broadcasting Driver

The next step is to configure the broadcasting driver. Laravel Websockets supports several broadcasting drivers, including Redis, Pusher, and Log. For this tutorial, we will use the Redis driver.

To configure the Redis driver, add the following lines to your .env file:

BROADCAST_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

These lines tell Laravel to use the Redis driver and to connect to a Redis server running on the local machine. If your Redis server is running on a different machine or port, adjust these values accordingly.

Start the Laravel Websockets Server

The final step is to start the Laravel Websockets server. To do this, run the following command:

php artisan websocket:serve

This will start the Websockets server and listen for incoming connections on port 6001. If you need to change the port, you can do so by passing the –port option to the command.

Using Laravel Websockets without Pusher

Once you have set up Laravel Websockets without Pusher, you can start using it in your Laravel application. Here are some steps to get started:

  1. Create a new channel
  2. Listen for events on the channel
  3. Broadcast events on the channel

Create a New Channel

To create a new channel, use the following code:

use BeyondCode\LaravelWebSockets\Facades\WebSocketsRouter;
use Illuminate\Support\Facades\Auth;
WebSocketsRouter::channel(‘my-channel’, function ($user, $data) {
return Auth::check() && $user->id === $data[‘user_id’];
});

This code creates a new channel named ‘my-channel’. The channel function takes a closure that is called when a user subscribes to the channel. The closure returns true or false, depending on whether the user is authorized to subscribe to the channel.

In this example, we are checking if the user is authenticated and if their user ID matches the user ID passed in the $data array. You can modify this code to suit your application’s needs.

Listen for Events on the Channel

To listen for events on the channel, use the following code:

use BeyondCode\LaravelWebSockets\Facades\WebSocketsRouter;
WebSocketsRouter::channel(‘my-channel’, function ($user, $data) {
$user->notify(new MyNotification());
});

This code listens for events on the ‘my-channel’ channel. When a new notification is created, the notify method is called on the authenticated user.

Broadcast Events on the Channel

To broadcast events on the channel, use the following code:

use BeyondCode\LaravelWebSockets\Facades\WebSocketsRouter;
WebSocketsRouter::broadcastToChannel(‘my-channel’, new MyEvent());

This code broadcasts a new event of type MyEvent to the ‘my-channel’ channel. When the event is received by the server, it is broadcast to all subscribers of the channel.

FAQ

What is Pusher?

Pusher is a third-party service that provides Websockets support for applications. It is a popular choice for developers who want to add real-time communication to their applications.

Why use Laravel Websockets without Pusher?

Using Laravel Websockets without Pusher can be more cost-effective for developers who do not want to pay for a third-party service. It also gives developers more control over their application.

What broadcasting drivers does Laravel Websockets support?

Laravel Websockets supports several broadcasting drivers, including Redis, Pusher, and Log.

How do I configure the broadcasting driver for Laravel Websockets?

To configure the broadcasting driver, add the appropriate values to your .env file. For example, to use the Redis driver, add the following lines:

BROADCAST_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379

How do I start the Laravel Websockets server?

To start the Laravel Websockets server, run the following command:

php artisan websocket:serve

This will start the server and listen for incoming connections on port 6001.

How do I create a new channel in Laravel Websockets?

To create a new channel, use the following code:

WebSocketsRouter::channel(‘my-channel’, function ($user, $data) {
// Code to authorize the user to subscribe to the channel
});

This code creates a new channel named ‘my-channel’. The closure passed to the channel function is called when a user subscribes to the channel.

How do I listen for events on a channel in Laravel Websockets?

To listen for events on a channel, use the following code:

WebSocketsRouter::channel(‘my-channel’, function ($user, $data) {
$user->notify(new MyNotification());
});

This code listens for events on the ‘my-channel’ channel. When a new notification is created, the notify method is called on the authenticated user.

How do I broadcast events on a channel in Laravel Websockets?

To broadcast events on a channel, use the following code:

WebSocketsRouter::broadcastToChannel(‘my-channel’, new MyEvent());

This code broadcasts a new event of type MyEvent to the ‘my-channel’ channel. When the event is received by the server, it is broadcast to all subscribers of the channel.