PHPackages                             levskiy0/long-polling - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. levskiy0/long-polling

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

levskiy0/long-polling
=====================

Laravel Long-Polling system with Go service integration

v1.1.5(7mo ago)0298↓21.4%MITPHPPHP &gt;=8.1

Since Nov 14Pushed 7mo agoCompare

[ Source](https://github.com/levskiy0/laravel-long-polling)[ Packagist](https://packagist.org/packages/levskiy0/long-polling)[ RSS](/packages/levskiy0-long-polling/feed)WikiDiscussions main Synced today

READMEChangelog (5)Dependencies (7)Versions (6)Used By (0)

Laravel Long-Polling Package
============================

[](#laravel-long-polling-package)

Laravel package that provides queue-backed long-polling primitives for Laravel apps while delegating delivery to the [Go Long-Polling Service](https://github.com/levskiy0/go-laravel-long-polling). Use it to keep Redis, database persistence, and API contracts in sync between PHP and Go runtimes.

[![preview.gif](assets/preview.gif)](assets/preview.gif)

Features
--------

[](#features)

- **Queue-Based Broadcasting** - events are persisted and dispatched through Laravel queues for reliable delivery.
- **Redis Fan-Out** - each broadcast notifies the Go service via Redis Pub/Sub so connected clients update instantly.

Requirements
------------

[](#requirements)

- PHP 8.1+
- Laravel 10/11/12 (or components via Illuminate packages)
- Redis server for Pub/Sub
- Queue worker configured for the broadcast queue

Installation
------------

[](#installation)

1. **Install the package**```
    composer require levskiy0/long-polling:^1.1.5
    ```
2. **Publish configuration**```
    php artisan vendor:publish --tag=long-polling-config
    ```
3. **Configure environment** - add keys to `.env`: ```
    LONGPOLLING_DRIVER=redis
    LONGPOLLING_GO_SERVICE_URL=http://localhost:8085
    LONGPOLLING_ACCESS_SECRET=shared_secret_between_laravel_and_go
    LONGPOLLING_BROADCAST_QUEUE=broadcast
    LONGPOLLING_REDIS_CONNECTION=default
    LONGPOLLING_REDIS_CHANNEL=longpoll:events
    ```
4. **Run migrations**```
    php artisan migrate
    ```
5. **Start a queue worker**```
    php artisan queue:work --queue=broadcast
    ```

Configuration
-------------

[](#configuration)

`config/long-polling.php` exposes defaults for the driver, Go-service URL, shared secret, queue, and Redis connection. Update the array below to match your infrastructure:

```
return [
    'driver' => env('LONGPOLLING_DRIVER', 'redis'),
    'go_service_url' => env('LONGPOLLING_GO_SERVICE_URL', 'http://localhost:8085'),
    'access_secret' => env('LONGPOLLING_ACCESS_SECRET', 'shared_secret_between_laravel_and_go'),
    'broadcast_queue' => env('LONGPOLLING_BROADCAST_QUEUE', 'broadcast'),
    'redis' => [
        'connection' => env('LONGPOLLING_REDIS_CONNECTION', 'default'),
        'channel' => env('LONGPOLLING_REDIS_CHANNEL', 'longpoll:events'),
    ],
];
```

Usage
-----

[](#usage)

### Broadcasting Events

[](#broadcasting-events)

```
use Levskiy0\LongPolling\Facades\LongPolling;

LongPolling::broadcast('user-123', [
    'type' => 'notification',
    'message' => 'You have a new message',
    'data' => [
        'sender' => 'John Doe',
        'timestamp' => now()->toISOString(),
    ],
]);
```

### Client Integration

[](#client-integration)

```
async function poll(offset = 0) {
  const tokenResponse = await fetch('/api/longpolling/token', {
    method: 'POST',
    body: JSON.stringify({ channel_id: 'user-123' }),
    headers: { 'Content-Type': 'application/json' }
  });
  const { token } = await tokenResponse.json();

  while (true) {
    const response = await fetch(
      `http://localhost:8085/getUpdates?token=${token}&offset=${offset}`
    );
    const { events } = await response.json();

    events.forEach(event => {
      // Update UI with each event
    });

    offset = events.length ? events[events.length - 1].id : offset;
  }
}

poll();
```

Example: Real-Time Chat
-----------------------

[](#example-real-time-chat)

### Controller

[](#controller)

```
