Laravel Real-Time Events on Windows (Local Server)

This article explains how to set up Laravel real-time events on Windows using a local development environment. We will use:

  • Windows + WSL (Ubuntu)
  • Redis as the broadcast queue
  • Laravel Broadcasting
  • Laravel Echo Server + Socket.IO

By the end, you will be able to broadcast events from Laravel and listen to them in real time on localhost.


1. Prerequisites

  • Windows 10/11
  • Administrator access
  • PHP >= 8.1
  • Composer
  • Node.js & npm
  • A Laravel project (existing or new)

2. Install Ubuntu on Windows using WSL

2.1 Enable WSL

Open PowerShell as Administrator and run:

wsl --install

This installs WSL and Ubuntu by default. Restart your system if prompted.

Check installation:

wsl -l -v

2.2 Open Ubuntu (WSL)

From Start Menu, open Ubuntu and set your Linux username and password.


3. Install & Run Redis on Ubuntu (WSL)

Redis will be used as the broadcasting driver.

3.1 Update packages

sudo apt update && sudo apt upgrade -y

3.2 Install Redis

sudo apt install redis-server -y

3.3 Start Redis Service

sudo service redis-server start

Verify Redis is running:

redis-cli ping

Expected output:

PONG
Redis will be available on 127.0.0.1:6379

4. Configure Laravel Broadcasting

4.1 Install Redis PHP Extension

Inside WSL:

sudo apt install php-redis -y

Restart PHP (if using PHP-FPM or Apache).

4.2 Enable Broadcasting in Laravel

In your Laravel project directory:

php artisan install:broadcasting

This enables broadcasting configuration.

4.3 Update .env

BROADCAST_CONNECTION=redis
QUEUE_CONNECTION=redis
CACHE_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PORT=6379

4.4 Update config/broadcasting.php

Make sure Redis is enabled:

'redis' => [
    'driver' => 'redis',
    'connection' => 'default',
],

5. Enable Laravel Broadcast Service Provider

Open config/app.php and ensure:

App\Providers\BroadcastServiceProvider::class,

is uncommented.


6. Install Laravel Echo Server & Socket.IO

Laravel Echo Server listens for broadcast events and pushes them via WebSockets.

6.1 Install Laravel Echo Server Globally

npm install -g laravel-echo-server

6.2 Initialize Echo Server

From your Laravel root:

laravel-echo-server init

Example answers:

? Do you want to run this server in development mode? Yes
? Which port would you like to serve on? 6001
? Which database would you like to use to store presence channel members? redis
? Enter the host of your Laravel authentication server: http://localhost
? Will you be serving on http or https? http
? Do you want to generate a client ID/Key? Yes

This creates laravel-echo-server.json.

6.3 Example laravel-echo-server.json

{
  "authHost": "http://localhost",
  "authEndpoint": "/broadcasting/auth",
  "clients": [
    {
      "appId": "local",
      "key": "localkey"
    }
  ],
  "database": "redis",
  "databaseConfig": {
    "redis": {
      "host": "127.0.0.1",
      "port": 6379
    }
  },
  "devMode": true,
  "host": null,
  "port": 6001,
  "protocol": "http",
  "socketio": {},
  "secureOptions": 67108864
}

7. Start Laravel Echo Server

laravel-echo-server start

You should see:

✔ Running at http://localhost:6001
✔ Listening for events...

8. Configure Frontend (Laravel Echo)

8.1 Install Echo & Socket.IO Client

npm install laravel-echo socket.io-client

8.2 Configure resources/js/bootstrap.js

import Echo from 'laravel-echo';
import io from 'socket.io-client';

window.io = io;

window.Echo = new Echo({
    broadcaster: 'socket.io',
    host: window.location.hostname + ':6001'
});

Run build:

npm run dev

9. Create a Simple Broadcast Event

9.1 Create Event

php artisan make:event MessageSent

9.2 Update Event Class

app/Events/MessageSent.php:

use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class MessageSent implements ShouldBroadcast
{
    use Dispatchable, InteractsWithSockets, SerializesModels;

    public string $message;

    public function __construct($message)
    {
        $this->message = $message;
    }

    public function broadcastOn()
    {
        return new Channel('chat');
    }
}

10. Dispatch Event from Controller

use App\Events\MessageSent;

Route::get('/send', function () {
    broadcast(new MessageSent('Hello from Laravel'));
    return 'Event Sent';
});

11. Listen to Event in JavaScript

window.Echo.channel('chat')
    .listen('MessageSent', (e) => {
        console.log(e.message);
    });

Open browser console and visit:

http://localhost/send

You should see the message instantly.


12. Notes: Enable & Edit Laravel Broadcasting

Important Broadcasting Notes

  • Always implement ShouldBroadcast
  • Channels are defined in routes/channels.php
  • Public channels require no authentication
  • Private/Presence channels require auth

Example Channel Definition

Broadcast::channel('chat', function () {
    return true;
});

Queue Requirement

Broadcasting uses queues. Ensure worker is running:

php artisan queue:work

13. Common Issues

Issue Solution
Events not firing Check Redis running
Echo not connecting Check port 6001 and Echo Server status
Auth error Verify authHost and authentication routes
No output Run queue worker with php artisan queue:work

Conclusion

Using WSL + Redis + Laravel Echo Server, you can easily run real-time Laravel events on Windows with a local setup. This approach closely matches production environments and provides fast, reliable WebSocket communication.

Happy Coding 🚀