Skip to content

fix(realtime): surface real Error on transport-level CHANNEL_ERROR - #2294

Merged
mandarini merged 3 commits into
developfrom
fix/realtime-channel-error-message
Apr 28, 2026
Merged

mandarini merged 3 commits into
developfrom
fix/realtime-channel-error-message

Conversation

@mandarini

@mandarini mandarini commented Apr 28, 2026 •

Copy link
Copy Markdown
Contributor

Description

channel.subscribe((status, err) => ...) previously fired with status === 'CHANNEL_ERROR' and err === undefined for any transport-level failure (socket close, heartbeat timeout, network unreachable, invalid JWT closing the socket). Consumers had no way to tell what went wrong. This PR makes the second argument always a real Error with a meaningful message and the original event preserved on cause.

What changed?

  • New helper packages/core/realtime-js/src/lib/normalizeChannelError.ts that maps Error | CloseEvent | Event | string | undefined to a real Error, formatting CloseEvent-shaped inputs as "socket closed: <code> (<reason>)" and attaching the original via cause.
  • RealtimeChannel.subscribe now runs every CHANNEL_ERROR exit through the normalizer:
    • Transport path (_onError) no longer casts undefined to Error.
    • Server-reply path (.receive('error', ...)) drops the old JSON.stringify(Object.values(error).join(', ')) wrapping, which produced messages with literal quote characters around them. The joined message is used directly and the raw payload is preserved on cause.
  • Tests:
    • test/normalizeChannelError.test.ts: 9 unit tests covering all input shapes (Error, Error subclass, string, CloseEvent with and without reason, empty reason, generic Event, undefined, null).
    • test/RealtimeChannel.errors.test.ts: 4 integration tests asserting the subscribe callback receives a real Error with a useful message and cause for CloseEvent payloads, Error payloads (heartbeat timeout), the defensive undefined fallback, and the server-reply path.
  • Full realtime-js suite passes locally (npx nx test realtime-js, 346 tests).

Why was this change needed?

The err === undefined symptom has been confirmed by multiple reporters in different environments (WSL networking, unreachable self-hosted instances, invalid JWT) and acknowledged by maintainers in the linked issue. Without an err, applications cannot distinguish a heartbeat timeout from a network failure from an auth rejection, which makes Realtime issues painful to debug.

Root cause is two layers deep. In @supabase/phoenix, Socket.triggerChanError() calls channel.trigger(CHANNEL_EVENTS.error) with no payload, so the channel onError listeners always receive undefined. The corresponding upstream fix is in supabase/phoenix#29. Even with that, the public subscribe contract belongs in this layer, so realtime-js does the normalization defensively, which means consumers on an older @supabase/phoenix still get a real Error (with the generic "channel error: connection lost" message), and consumers on the new @supabase/phoenix get the rich "socket closed: 1006 (Network Failure)" message with the CloseEvent on cause.

Closes #1734

Screenshots/Examples

const channel = supabase.channel('room1')
channel.subscribe((status, err) => {
  if (status === 'CHANNEL_ERROR') {
    console.error(err.message)        // 'socket closed: 1006 (Network Failure)'
    console.error(err.cause)          // the original CloseEvent
  }
})

Before this PR, err was undefined for the same scenario.

Breaking changes

  • This PR contains no breaking changes

The public type for the err argument is already Error | undefined. This PR makes the actual runtime value match the documented type more often, never less. Consumers that only checked status === 'CHANNEL_ERROR' are unaffected. Consumers that read err?.message or err?.cause get a useful value where they previously got nothing.

The server-reply error path's message format changes slightly: it no longer wraps the joined string in literal double quotes ("Authentication failed, Invalid API key" becomes Authentication failed, Invalid API key). This is a fix for the existing JSON.stringify of a string, not a contract change.

Checklist

  • I have read the Contributing Guidelines
  • My PR title follows the conventional commit format: <type>(<scope>): <description>
  • I have run npx nx format to ensure consistent code formatting
  • I have added tests for new functionality (if applicable)
  • I have updated documentation (if applicable)

Additional notes

  • Pairs with supabase/phoenix#29. Once that ships in a new @supabase/phoenix patch release, a follow up commit can bump the dep floor in packages/core/realtime-js/package.json from ^0.4.0 to the new version. The current ^0.4.0 range will pull the patch automatically on fresh installs, so the bump is for clarity rather than necessity.
  • Safe to ship without waiting on phoenix: the normalizer's undefined branch produces Error("channel error: connection lost"), which is already an improvement over undefined for end users.
  • No documentation changes here. The err argument was already typed as Error | undefined in the public types.

@mandarini
mandarini requested review from a team as code owners April 28, 2026 08:32
@github-actions github-actions Bot added the realtime-js Related to the realtime-js library. label Apr 28, 2026
@pkg-pr-new

pkg-pr-new Bot commented Apr 28, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

@supabase/auth-js

npm i https://pkg.pr.new/@supabase/auth-js@2294

@supabase/functions-js

npm i https://pkg.pr.new/@supabase/functions-js@2294

@supabase/postgrest-js

npm i https://pkg.pr.new/@supabase/postgrest-js@2294

@supabase/realtime-js

npm i https://pkg.pr.new/@supabase/realtime-js@2294

@supabase/storage-js

npm i https://pkg.pr.new/@supabase/storage-js@2294

@supabase/supabase-js

npm i https://pkg.pr.new/@supabase/supabase-js@2294

commit: 2cb050a

@mandarini mandarini self-assigned this Apr 28, 2026
filipecabaco
filipecabaco previously approved these changes Apr 28, 2026
@mandarini
mandarini force-pushed the fix/realtime-channel-error-message branch from 525072c to d126614 Compare April 28, 2026 10:00
@mandarini
mandarini requested review from a team as code owners April 28, 2026 10:00
@mandarini
mandarini merged commit 6795cbc into develop Apr 28, 2026
35 of 38 checks passed
@mandarini
mandarini deleted the fix/realtime-channel-error-message branch April 28, 2026 10:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

realtime-js Related to the realtime-js library.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Realtime - Unable to communicate with host when running client within WSL

3 participants