Skip to content
LaravelBlog

laravel/reverb: Unencodable broadcast payloads now fail fast under Redis scaling

Malformed payloads published to Redis now throw JsonException where they were created, and the subscriber logs and skips messages it cannot handle.

LaravelBlogBot
LaravelBlogBot , 4 mins read

Contributed by pushpak1300

If you run Reverb with horizontal scaling (reverb:start --scaling), every event is published to Redis and fanned out to each subscribed server. Up to now, a payload that PHP could not JSON-encode — one containing INF or NAN, for example — was silently published as an empty string, and the subscriber then failed to decode it. This change makes encoding failures loud and points the error at the code that caused it, while making the Redis subscriber resilient to messages it cannot handle.

What changed

Two related fixes landed in the Redis pub/sub provider.

RedisPublishClient::publish() now encodes with JSON_THROW_ON_ERROR before doing anything else:

$message = json_encode($payload, JSON_THROW_ON_ERROR);

If the payload cannot be encoded, a JsonException is thrown immediately — before anything is sent to Redis, and before the payload is queued while a connection is still being established. The error surfaces in the process that built the payload, which is exactly where it can be fixed.

On the subscriber side, the message handler now wraps the incoming payload in a try/catch. When handling a message throws, Reverb logs Failed to handle message from Redis: ... and carries on with the next message instead of letting the exception break out of the event loop.

Why it matters

Previously the failure chain was completely silent on the publishing side. json_encode(['data' => INF]) returns false, that false went to the Redis client as the message body, and subscribers received an empty string they could not decode. The event never arrived, and nothing in the logs pointed at the event that caused it.

Worse, the decode failure happened inside the subscriber's message handler on every subscribed server. An unhandled exception there could interrupt the processing of subsequent messages, meaning a single malformed broadcast could degrade event delivery across your whole scaled deployment until each server was restarted.

With this change, the failure is attributed to the request or job that broadcast the bad payload, and the subscriber degrades gracefully: one bad message is logged and skipped, and everything after it is processed as usual.

What this looks like in practice

Consider an event whose payload depends on sampled system statistics:

class MetricsUpdated implements ShouldBroadcast
{
    public function __construct(private array $sample) {}

    public function broadcastWith(): array
    {
        return [
            'load' => $this->sample['load'] ?? 0, // INF if /proc could not be read
            'rpm'  => $this->sample['rpm'] ?? 0,
        ];
    }
}

Before this change, with scaling enabled, broadcast(new MetricsUpdated($sample)) appeared to succeed while subscribers received an empty string. After this change, the broadcast throws immediately:

JsonException

Inf and NaN cannot be JSON encoded

If you would rather skip than fail the request, catch it at the call site:

try {
    broadcast(new MetricsUpdated($sample));
} catch (JsonException $e) {
    Log::warning('Skipped broadcast with unencodable payload.', [
        'error' => $e->getMessage(),
    ]);
}

The better fix is usually at the source: coerce NAN/INF to null or a finite number, and make sure strings are valid UTF-8 before they reach broadcast().

Upgrade impact

  • There are no configuration, API, or contract changes — nothing to migrate in your application.
  • Valid payloads are encoded and published exactly as before. The new tests confirm an unencodable payload never reaches Redis (the recorded client calls stay empty) and that the subscriber keeps handling messages after one fails.
  • If your application currently broadcasts payloads containing INF, NAN, or invalid UTF-8, those broadcasts will now throw JsonException instead of "succeeding" with an empty message. These were always bugs — they just used to be invisible.
  • Rolling deploys also benefit: subscribers running the new code will log and continue if they receive a malformed message from a server still running the old code.

Takeaways

  • Encoding errors now throw JsonException where the payload is created, thanks to JSON_THROW_ON_ERROR, instead of publishing an empty string to Redis.
  • The Redis subscriber logs malformed messages and continues, so a single bad message can no longer stall event delivery on a server.
  • Valid payloads behave identically to v1.13.0 — no action is needed for well-formed events.
  • Audit broadcasts that include computed values; anything PHP cannot JSON-encode will now surface at broadcast time.

Sources

Related Articles