Skip to content

fix(postgres-emitter): include the namespace in published messages - #5601

Open
ricardoasmarques wants to merge 1 commit into
socketio:mainfrom
ricardoasmarques:fix/postgres-emitter-nsp
Open

ricardoasmarques wants to merge 1 commit into
socketio:mainfrom
ricardoasmarques:fix/postgres-emitter-nsp

Conversation

@ricardoasmarques

Copy link
Copy Markdown

The kind of change this PR does introduce

  • a bug fix
  • a new feature
  • an update to the documentation
  • a code change that improves performance
  • other

Current behavior

With @socket.io/postgres-emitter@0.1.1 and @socket.io/postgres-adapter@0.4.0 or later, the servers never receive anything the emitter publishes.

Since 0.4.0, the Postgres adapter extends ClusterAdapter from socket.io-adapter, which drops messages whose top-level nsp doesn't match:

if (message.nsp !== this.nsp.name) {
  return debug("[%s] ignore message from another namespace (%s)", this.uid, message.nsp);
}

0.5.0 also routes incoming messages by msg.nsp (namespaces.get(msg.nsp)) before reaching that check.

The emitter sets nsp only on the inner packet (data.packet.nsp), never on the message itself. So every broadcast, socketsJoin / socketsLeave, disconnectSockets and serverSideEmit is dropped, with this debug log on each server:

socket.io-adapter [<uid>] ignore message from another namespace (undefined)

The emitter's tests didn't catch it because the root package.json pins @socket.io/postgres-adapter to ^0.1.0, which never checked the namespace.

New behavior

publish() sets document.nsp = this.emitter.nsp next to document.uid. Every message goes through publish(), so this covers both the direct pg_notify path and the attachment path (payload over payloadThreshold, or with binary data).

The dev dependency moves to @socket.io/postgres-adapter@^0.5.0, so the existing tests now run against a current adapter. Locally with Postgres 14 (as in CI):

  • adapter 0.5.0 without the fix: the broadcast tests time out
  • adapter 0.5.0 with the fix: 12 passing

Other information (e.g. related issues)

I couldn't find an existing issue. We hit this in production after moving an emit-only worker from the full adapter to the emitter: the worker's events stopped reaching clients without any error.

Since version 0.4.0, `@socket.io/postgres-adapter` extends the
`ClusterAdapter` class, which ignores any message whose top-level `nsp`
does not match its namespace. The emitter only set `nsp` on the inner
packet, so every message it published was dropped ("ignore message from
another namespace (undefined)").

The tests now run against `@socket.io/postgres-adapter@0.5.0` instead of
`0.1.x`, which did not check the namespace and hid the issue.

Co-authored-by: Cursor <cursoragent@cursor.com>
@ricardoasmarques
ricardoasmarques marked this pull request as ready for review November 12, 2026 15:59
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant