PHPackages                             upscale/swoole-dispatch - 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. [Queues &amp; Workers](/categories/queues)
4. /
5. upscale/swoole-dispatch

ActiveLibrary[Queues &amp; Workers](/categories/queues)

upscale/swoole-dispatch
=======================

Request dispatch strategies for Swoole web-server

2.3.0(7y ago)3934Apache-2.0PHPPHP &gt;=5.4

Since Mar 3Pushed 7y ago3 watchersCompare

[ Source](https://github.com/upscalesoftware/swoole-dispatch)[ Packagist](https://packagist.org/packages/upscale/swoole-dispatch)[ Docs](https://github.com/upscalesoftware/swoole-dispatch)[ RSS](/packages/upscale-swoole-dispatch/feed)WikiDiscussions master Synced 2d ago

READMEChangelogDependenciesVersions (8)Used By (0)

Request Dispatch for Swoole
===========================

[](#request-dispatch-for-swoole)

This is a collection of request [dispatch strategies](https://www.swoole.co.uk/docs/modules/swoole-server/configuration#dispatch_func) for [Swoole](https://www.swoole.co.uk/) web-server that compliment the built-in [dispatch modes](https://www.swoole.co.uk/docs/modules/swoole-server/configuration#dispatch_mode). Sticky session dispatch strategy also known as the session affinity is the cornerstone of this library. Other strategies are provided as a fallback for guest requests without the session context. They mimic the native dispatch modes that are by design mutually exclusive to the custom dispatch function.

**Strategies:**

- Fixed Client
    - Dispatch requests to workers by Client ID
- Round Robin
    - Dispatch requests to workers in circular order
- Sticky Session
    - Dispatch requests to workers by Session ID
    - Session ID in query string
    - Session ID in cookies
- URL Path
    - Dispatch requests to workers by URL path
    - Query string excluded or included
    - Custom URL path pattern
    - HTTP method filters

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

[](#installation)

The library is to be installed via [Composer](https://getcomposer.org/) as a dependency:

```
composer require upscale/swoole-dispatch
```

Dispatch Strategies
-------------------

[](#dispatch-strategies)

### Fixed Client

[](#fixed-client)

Dispatch requests to workers according to client connection ID equivalent to the built-in [fixed dispatch mode](https://www.swoole.co.uk/docs/modules/swoole-server/configuration#dispatch_mode).

Register the dispatcher:

```
require 'vendor/autoload.php';

$server = new \Swoole\Http\Server('127.0.0.1', 8080);
$server->set([
    'dispatch_func' => new \Upscale\Swoole\Dispatch\FixedClient(),
]);
$server->on('request', function ($request, $response) use ($server) {
    $response->header('Content-Type', 'text/plain');
    $response->end("Served by worker {$server->worker_id}\n");
});
$server->start();
```

Send some test requests:

- Different connections are distributed between all workers: ```
    curl -s 'http://127.0.0.1:8080/?[1-4]' -H 'Connection: close'

    Served by worker 0
    Served by worker 1
    Served by worker 2
    Served by worker 3
    ```
- Every connection is dispatched to a dedicated worker: ```
    curl -s 'http://127.0.0.1:8080/?[1-4]'

    Served by worker 1
    Served by worker 1
    Served by worker 1
    Served by worker 1
    ```

### Round Robin

[](#round-robin)

Dispatch requests to workers in circular order equivalent to the built-in [polling dispatch mode](https://www.swoole.co.uk/docs/modules/swoole-server/configuration#dispatch_mode).

Register the dispatcher:

```
$server->set([
    'dispatch_func' => new \Upscale\Swoole\Dispatch\RoundRobin(),
]);
```

Send some test requests:

- Requests of all connections are distributed between all workers: ```
    curl -s 'http://127.0.0.1:8080/?[1-4]' -H 'Connection: close'
    curl -s 'http://127.0.0.1:8080/?[1-4]'

    Served by worker 0
    Served by worker 1
    Served by worker 2
    Served by worker 3
    ```

### Sticky Session

[](#sticky-session)

Dispatch requests to workers according to session ID for sticky session also known as session affinity. All requests belonging to a session will be dispatched to a dedicated worker process. Session ID is recognized in a query string and cookie headers in that order of priority.

This strategy is complimentary to the session locking and can compensate for the lack of thereof. It prevents race conditions in workers competing for an exclusive lock of the same session ID. Workers only pick up requests of their respective sessions as well as guest requests without the session context.

Dispatch of guest requests will be delegated to a specified fallback strategy of choice.

Register the sticky session dispatcher with fallback to the Round-Robin for guests:

```
$server->set([
    'dispatch_func' => new \Upscale\Swoole\Dispatch\StickySession(
        new \Upscale\Swoole\Dispatch\RoundRobin()
    ),
]);
```

Send some test requests with and without the session context:

- Guest requests are delegated to the fallback strategy Round-Robin: ```
    curl -s 'http://127.0.0.1:8080/?[1-4]'

    Served by worker 0
    Served by worker 1
    Served by worker 2
    Served by worker 3
    ```
- Session requests are dispatched to a dedicated worker: ```
    curl -s 'http://127.0.0.1:8080/?PHPSESSID=ExampleSessionIdentifier11&[1-4]'
    curl -s 'http://127.0.0.1:8080/?[1-4]' -H 'Cookie: PHPSESSID=ExampleSessionIdentifier11'

    Served by worker 2
    Served by worker 2
    Served by worker 2
    Served by worker 2
    ```

### URL Path

[](#url-path)

Dispatch requests to workers according to a URL path of a HTTP request. Configure whether to use a query string in addition to the path or not (the default). Custom URL path filter can be provided via the PCRE regular expression syntax. The dispatch can also be limited to a given HTTP methods (`HEAD` and `GET` by default).

Register the URL path dispatcher with fallback to the Round-Robin:

```
$server->set([
    'dispatch_func' => new \Upscale\Swoole\Dispatch\UrlPath(
        new \Upscale\Swoole\Dispatch\RoundRobin()
    ),
]);
```

Send some test requests:

- Requests are dispatched to a dedicated worker by URL path: ```
    curl -s 'http://127.0.0.1:8080/page1.html'
    curl -s 'http://127.0.0.1:8080/page2.html'
    curl -s 'http://127.0.0.1:8080/page3.html'
    curl -s 'http://127.0.0.1:8080/page3.html?ignored=any'

    Served by worker 2
    Served by worker 0
    Served by worker 1
    Served by worker 1
    ```

Contributing
------------

[](#contributing)

Pull Requests with fixes and improvements are welcome!

License
-------

[](#license)

Copyright © Upscale Software. All rights reserved.

Licensed under the [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0).

###  Health Score

29

—

LowBetter than 59% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity17

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity60

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~3 days

Total

7

Last Release

2609d ago

Major Versions

1.0.0 → 2.0.02019-03-03

PHP version history (2 changes)1.0.0PHP &gt;=7.1

2.1.0PHP &gt;=5.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/9ff1791f1bd72221702e39e356c25b108de26dea1f5b56416f7957e8fa43ea92?d=identicon)[upscalesoftware](/maintainers/upscalesoftware)

---

Top Contributors

[![sshymko](https://avatars.githubusercontent.com/u/1231423?v=4)](https://github.com/sshymko "sshymko (31 commits)")

---

Tags

requestclientConnectionswoolesessiondispatcherdispatchworkerstickyround-robincircularfixedaffinity

### Embed Badge

![Health badge](/badges/upscale-swoole-dispatch/health.svg)

```
[![Health](https://phpackages.com/badges/upscale-swoole-dispatch/health.svg)](https://phpackages.com/packages/upscale-swoole-dispatch)
```

###  Alternatives

[basis-company/nats

nats jetstream client for php

201354.3k19](/packages/basis-company-nats)[qxsch/worker-pool

Runs tasks in a parallel processing workerpool.

108325.7k1](/packages/qxsch-worker-pool)[jmikola/wildcard-event-dispatcher-bundle

Enhances the Symfony2 event dispatcher with support for wildcard patterns inspired by AMQP topic exchanges.

35138.8k1](/packages/jmikola-wildcard-event-dispatcher-bundle)[sobirjonovs/laravel-rabbit

Easy tool for working with RabbitMQ

111.6k](/packages/sobirjonovs-laravel-rabbit)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
