crossws icon indicating copy to clipboard operation
crossws copied to clipboard

Publish doesn't publish to current peer

Open rijkvanzanten opened this issue 3 months ago • 5 comments

Environment

[email protected]

[email protected]
  → h3@@1.15.4
    → [email protected]

Reproduction

Use peer.publish

Describe the bug

According to the documentation, peer.publish broadcasts a message to the subscribers of the given channel. However, I noticed that the peer that triggered the publish itself never received the message, even though it was subscribed to the channel.

Additional context

On closer look, it looks like this was implemented intentionally here:

https://github.com/h3js/crossws/blob/84cf6794f207bd5bc53be67bde68dd7e61e851e7/src/adapters/node.ts#L179

as it explicitly checks if the peer isn't the current peer before sending the message. Not entirely sure if this is a bug or a 'feature'; depending on that this is either a bug report or a request for better documentation. Happy to open a PR for either of those :)

Logs


rijkvanzanten avatar Sep 10 '25 00:09 rijkvanzanten

This isn't a bug but expected behavior. We should improve documentation to clarify it.

We could also introduce a new option like peer.publish(topic, message, { self: true }) to enable echo-back behavior.

(PR welcome for both)

pi0 avatar Sep 10 '25 10:09 pi0

expected behavior

intended* behavior 😁 My background in sockets is from socket.io back in the day which introduced the idea of broadcasting to rooms, but also set the standard that a broadcast to a room would send the message to all participants of this room. I therefore expected crossws' topic to behave similarly to that pre-established pattern 🙂

rijkvanzanten avatar Sep 10 '25 13:09 rijkvanzanten

@pi0 The code uses the nomenclature topic, but the documentation calls it channel. Any preference for one over the other? Lets make it consistent while we're at it 🙂

rijkvanzanten avatar Sep 10 '25 14:09 rijkvanzanten

Sorry for the spam, but had one more follow-up here. Does the self-exclusion in publishing work consistently across vendors at the moment? Noticed the peer !== this check in some adapters like node or SSE:

https://github.com/h3js/crossws/blob/84cf6794f207bd5bc53be67bde68dd7e61e851e7/src/adapters/node.ts#L179

https://github.com/h3js/crossws/blob/84cf6794f207bd5bc53be67bde68dd7e61e851e7/src/adapters/sse.ts#L147-L154

but not others like bun:

https://github.com/h3js/crossws/blob/84cf6794f207bd5bc53be67bde68dd7e61e851e7/src/adapters/bun.ts#L127-L131

rijkvanzanten avatar Sep 10 '25 14:09 rijkvanzanten

Native behavior of Bun pub/sub publish is to publish to all excluding self (docs), and I think the same for some others, so if we want to introduce self: true, we need an additional logic for them to do self-publish.

Let's first fix up the docs, We should clarify the current behavior. Also nice find about topic/channel, i think topic might make sense because we currently have peer.topics.

Another note: Crossws also has a new global publish introduced in v0.4 (release notes) which is still not documented and not available in Nitro but we should document that too!

pi0 avatar Sep 10 '25 16:09 pi0