Skip to content
Laravel Backend Software Architecture

How I Made Laravel Receive Browser Messages With WebSockets and Redis

Raheel Shan

Raheel Shan

November 25th, 2026 · 15 min read

How I Made Laravel Receive Browser Messages With WebSockets and Redis

In the previous article, I moved from the old WebSocket setup to Laravel Reverb. But before Reverb, I had already found a way to make Laravel handle communication coming from the browser. It was not as simple as SignalR. I had to connect three things together:

Laravel → Redis → Laravel subscriber

The WebSocket server was already using Redis for communication. I used that same Redis communication path to let a separate Laravel process listen for client events.

This gave me two different types of communication:

  • Client → client
  • Client → server

But there was an important limitation.

Client → server could save something in the database.

It did not automatically send that updated data back to the other clients. That distinction is important.

The client-to-client part

With Laravel Echo and laravel-echo-server, a browser can send a client event using whisper().

For example:

const channel = Echo.private(`trip.${tripId}`);

channel.whisper('location', {
    latitude,
    longitude
});

Another browser listening on the same channel can receive it:

channel.listenForWhisper('location', (data) => {
    updateDriverLocation(data);
});

The important thing here is that Laravel itself does not receive this event. The WebSocket server receives the client event and sends it to the other connected clients.

So the flow is:

Browser A
    ↓
WebSocket
    ↓
laravel-echo-server
    ↓
Browser B

Laravel is not in the middle. For things such as typing indicators, temporary cursor positions, or live driver locations, this can be useful. The data does not necessarily need to be stored.

But I needed Laravel to receive the data

In one of my applications, I had a different requirement. The browser was sending real-time information, but I needed Laravel to process it.

For example, a driver could send location information.

I did not just want another browser to see that location. I wanted Laravel to receive it and perform application logic. That meant the communication needed to look more like this:

Browser
    ↓
WebSocket
    ↓
Redis
    ↓
Laravel subscriber
    ↓
Database

This is where Redis became useful.

Redis was the bridge

The WebSocket server was already using Redis as part of its communication mechanism.

Instead of trying to make Laravel receive the WebSocket event directly, I created a long-running Laravel Artisan command that subscribed to the relevant Redis channels.

The command looked like this:

class RedisSubscriber extends Command
{
    protected $signature = 'redis:subscribe';

    protected $description =
        'This command is defined to listen to the communication between two clients';

    public function handle()
    {
        ini_set('default_socket_timeout', -1);

        Redis::psubscribe(
            ['*driver.*', '*trip.*'],
            function ($data, $channel) {

                $parts = explode('-', $channel);

                $channelName =
                    explode('.', $parts[1])[0];

                if ($channelName == 'driver') {

                    $driver =
                        new \App\Subscribers\DriverSubscriber($data);

                    $driver->handle();

                } else if ($channelName == 'trip') {

                    $trip =
                        new \App\Subscribers\TripSubscriber($data);

                    $trip->handle();
                }
            }
        );
    }
}

The important part is not the command itself.

It is this:

Redis::psubscribe(
    ['*driver.*', '*trip.*'],
    ...
);

The Laravel process stays alive and listens for messages published on those Redis channels. When a message arrives, the subscriber checks which type of message it received.

A driver message goes to DriverSubscriber.

A trip message goes to TripSubscriber.

The subscriber then contains the application-specific logic.

Why use separate subscriber classes?

I could have put all the processing inside the Redis callback. I did not want to do that. The Redis command should mainly be responsible for listening. The actual application logic belongs somewhere else.

For example:

$driver = new \App\Subscribers\DriverSubscriber($data);

$driver->handle();

or:

$trip = new \App\Subscribers\TripSubscriber($data);

$trip->handle();

This gives me a simple separation:

RedisSubscriber
    ↓
Identify message
    ↓
DriverSubscriber / TripSubscriber
    ↓
Application logic
    ↓
Database

The Redis subscriber does not need to know how a driver location is processed. It only needs to know where that message should go.

Starting the subscriber

This command is not a normal request. It is a long-running process. I start it with:

php artisan redis:subscribe

The command does not finish after handling one message. It stays alive and waits for the next Redis message. That is why this part needs to be treated differently from a normal Laravel Artisan command.

In production, I would run it under Supervisor so that the process is restarted if it stops. The architecture now has another long-running process to maintain. That was one of the trade-offs of this approach.

Client-to-server communication

Now we can put the complete flow together. Suppose a driver sends a location update. The browser sends the event:

channel.whisper('location', {
    latitude: 24.8607,
    longitude: 67.0011
});

The WebSocket server receives the client event. Redis carries the communication.

Our Laravel subscriber is listening for the relevant Redis channel. The subscriber receives the data and passes it to DriverSubscriber. DriverSubscriber can then validate the data, find the driver, update the location, create history, or perform whatever application logic is required.

The important point is that Laravel is now receiving something that originated from a browser through the WebSocket/Redis path.

That is the part I was missing when I first looked at whisper().

But saving the data does not update the clients

This is where it is easy to misunderstand the architecture. Suppose the driver sends:

Driver location = 24.8607, 67.0011

Laravel receives it. Laravel saves it. The database now contains the new location. That does not automatically mean the other browsers know about it. The flow so far is:

Client
   ↓
WebSocket
   ↓
Redis
   ↓
Laravel subscriber
   ↓
Database

There is no arrow going back to the browsers. If I want the other clients to receive the updated location, I need another broadcast. So the complete flow becomes:

Client A
   ↓
WebSocket
   ↓
Redis
   ↓
Laravel subscriber
   ↓
Application logic
   ↓
Database
   ↓
Laravel broadcast
   ↓
WebSocket
   ↓
Client B

This is an important distinction.

Receiving data and broadcasting data are two separate operations.

Client-to-client and client-to-server are not the same thing

At this point I had two different communication paths.

Client → client

Browser A
    ↓
WebSocket
    ↓
laravel-echo-server
    ↓
Browser B

Laravel does not need to participate. This is useful for temporary real-time events.

Client → server

Browser
    ↓
WebSocket
    ↓
Redis
    ↓
Laravel subscriber
    ↓
Database

Now Laravel can process and store the data. But the other clients do not automatically receive the result. For that, Laravel needs to broadcast another event.

Why not just use HTTP?

At this point someone might ask:

Why not simply send the data with POST?

That is a completely reasonable question. For many applications, HTTP is the better choice.

For example:

await axios.post(`/trips/${tripId}/location`, {
    latitude,
    longitude
});

Laravel receives a normal request.

Authentication, validation and database operations all happen through the normal Laravel lifecycle.

There is nothing wrong with this approach.

I used the Redis/WebSocket approach when I wanted the communication to stay inside the real-time connection and avoid turning every real-time event into a separate HTTP request.

But that came with a cost.

I now had to maintain:

  • A WebSocket server
  • Redis
  • A Laravel subscriber process
  • Supervisor
  • The normal Laravel application
  • The queue worker for broadcasts

The architecture worked. It was just not particularly small.

The subscriber can become a routing layer

The first version of my subscriber was simple. It checked the channel name:

if ($channelName == 'driver') {

    $driver =
        new \App\Subscribers\DriverSubscriber($data);

    $driver->handle();

} else if ($channelName == 'trip') {

    $trip =
        new \App\Subscribers\TripSubscriber($data);

    $trip->handle();
}

But the structure also gave me another idea.

Instead of putting all Redis handling into one command, I could configure different subscriber classes for different message types.

The commented code in my command was already moving in this direction.

The Redis command becomes responsible for:

  1. Listening to Redis.
  2. Identifying the channel.
  3. Finding the appropriate subscriber.
  4. Passing the data to it.

The subscriber becomes responsible for the actual application logic.

That is a much cleaner separation than putting database operations directly inside the Redis callback.

What this approach actually solved

This architecture solved the problem from the previous articles.

I could now have:

Client → server

without converting every message into an HTTP request.

I could also continue using:

Client → client

for events that did not need Laravel.

And if Laravel needed to notify the other clients after processing the message, it could broadcast another event.

So technically, I had two-way communication. But there was an important catch. I had built the missing server-side communication myself using Redis and a long-running Laravel process.

Laravel was not providing the whole thing as one integrated WebSocket communication model. That distinction becomes important when comparing this approach with newer Laravel WebSocket solutions.

Where this approach left me

This setup worked in production. It was not theoretical. A browser could send real-time data. Redis could carry it. A Laravel process could receive it. The application could save it. And Laravel could broadcast another event to the clients. But I now had another process that had to stay alive. If redis:subscribe stopped running, client-to-server communication stopped. The main Laravel application could still be working.

The website could still be loading. The API could still be responding. Only the real-time subscriber would be dead.

That was another version of the same operational problem I had already seen with the old WebSocket stack. The architecture worked. The maintenance was the problem. And this is exactly where Laravel Reverb becomes interesting.

Previous: Laravel Reverb Cut My WebSocket Stack From 7 Parts to 5

Next: A hub that gives Laravel the client call SignalR had, so the browser talks to your PHP over the same connection.

Comment "REVERB" if you have built a Redis subscriber like this.

Found this useful?

Support the work that goes into these posts.

Support
Comments


Comment created and will be displayed once approved.
Built with Brandidos