flarum/framework · error · BadRequestException

Request must be limited to presence channels in order to…

Error message

Request must be limited to presence channels in order to fetch user_count

What it means

Validation guard in the realtime websocket FetchChannels controller. The 'info' query parameter (comma-separated channel attributes) may only request user_count for presence channels; the request named non-presence channels in its prefix filter, which cannot provide per-user counts, so the controller rejects it with a BadRequestException instead of returning incomplete data.

Solutions

  1. Only pass filter_by_prefix values that match presence channels (e.g. 'presence-') when requesting info=user_count.
  2. Drop user_count from the info parameter if non-presence channels must be fetched.
  3. If the client derives the prefix dynamically, filter the channel list to presence-* before sending the request.
  4. Wrap the call in try-catch on the client side and surface a clear message that user_count requires presence channels.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at extensions/realtime/src/Websocket/Connection/FetchChannels.php:30 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of flarum/framework@4b939f6853 (2026-09-15). Data as JSON: /api/errors/cb2230accef8adfe. Report an issue: GitHub.

Appendix: source

Thrown at extensions/realtime/src/Websocket/Connection/FetchChannels.php:30

use Flarum\Realtime\Websocket\Channel\Channel;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Psr\Http\Message\ServerRequestInterface;
use stdClass;
use Symfony\Component\HttpFoundation\Exception\BadRequestException;

class FetchChannels extends Controller
{
    public function __invoke(ServerRequestInterface $request): mixed
    {
        $attributes = [];
        $filterByPrefix = Arr::get($request->getQueryParams(), 'filter_by_prefix');

        if ($info = Arr::get($request->getQueryParams(), 'info')) {
            $attributes = explode(',', trim($info));

            if (in_array('user_count', $attributes) && ! Str::startsWith($filterByPrefix, 'presence-')) {
                throw new BadRequestException('Request must be limited to presence channels in order to fetch user_count');
            }
        }

        return $this->manager
            ->getChannels()
            ->then(function ($channels) use ($filterByPrefix, $attributes) {
                /** @phpstan-ignore-next-line */
                $channels = collect($channels)
                    ->mapWithKeys(function ($channel) {
                        $key = $channel instanceof Channel
                            ? $channel->getName()
                            : $channel;

                        return [
                            $key => new stdClass
                        ];
                    });

View on GitHub (pinned to 4b939f6853)