Repository navigation
fix(realtime): surface real Error on transport-level CHANNEL_ERROR - #2294
Merged
Merged
Conversation
@supabase/auth-js
@supabase/functions-js
@supabase/postgrest-js
@supabase/realtime-js
@supabase/storage-js
@supabase/supabase-js
commit: |
filipecabaco
previously approved these changes
Apr 28, 2026
mandarini
force-pushed
the
fix/realtime-channel-error-message
branch
from
April 28, 2026 10:00
525072c to
d126614
Compare
grdsdev
approved these changes
Apr 28, 2026
This was referenced May 11, 2026
This was referenced May 13, 2026
This was referenced May 13, 2026
This was referenced May 19, 2026
This was referenced Jul 29, 2026
This was referenced Aug 18, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
channel.subscribe((status, err) => ...)previously fired withstatus === 'CHANNEL_ERROR'anderr === undefinedfor 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 realErrorwith a meaningful message and the original event preserved oncause.What changed?
packages/core/realtime-js/src/lib/normalizeChannelError.tsthat mapsError | CloseEvent | Event | string | undefinedto a realError, formattingCloseEvent-shaped inputs as"socket closed: <code> (<reason>)"and attaching the original viacause.RealtimeChannel.subscribenow runs everyCHANNEL_ERRORexit through the normalizer:_onError) no longer castsundefinedtoError..receive('error', ...)) drops the oldJSON.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 oncause.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 thesubscribecallback receives a real Error with a useful message andcausefor CloseEvent payloads, Error payloads (heartbeat timeout), the defensiveundefinedfallback, and the server-reply path.npx nx test realtime-js, 346 tests).Why was this change needed?
The
err === undefinedsymptom 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 anerr, 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()callschannel.trigger(CHANNEL_EVENTS.error)with no payload, so the channelonErrorlisteners always receiveundefined. The corresponding upstream fix is in supabase/phoenix#29. Even with that, the publicsubscribecontract belongs in this layer, sorealtime-jsdoes the normalization defensively, which means consumers on an older@supabase/phoenixstill get a realError(with the generic"channel error: connection lost"message), and consumers on the new@supabase/phoenixget the rich"socket closed: 1006 (Network Failure)"message with theCloseEventoncause.Closes #1734
Screenshots/Examples
Before this PR,
errwasundefinedfor the same scenario.Breaking changes
The public type for the
errargument is alreadyError | undefined. This PR makes the actual runtime value match the documented type more often, never less. Consumers that only checkedstatus === 'CHANNEL_ERROR'are unaffected. Consumers that readerr?.messageorerr?.causeget 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"becomesAuthentication failed, Invalid API key). This is a fix for the existingJSON.stringifyof a string, not a contract change.Checklist
<type>(<scope>): <description>npx nx formatto ensure consistent code formattingAdditional notes
@supabase/phoenixpatch release, a follow up commit can bump the dep floor inpackages/core/realtime-js/package.jsonfrom^0.4.0to the new version. The current^0.4.0range will pull the patch automatically on fresh installs, so the bump is for clarity rather than necessity.undefinedbranch producesError("channel error: connection lost"), which is already an improvement overundefinedfor end users.errargument was already typed asError | undefinedin the public types.