Skip to content

feat!: namespaced pub/sub peers#162

Merged
pi0 merged 7 commits intomainfrom
feat/isolate-pubsub
May 20, 2025
Merged

feat!: namespaced pub/sub peers#162
pi0 merged 7 commits intomainfrom
feat/isolate-pubsub

Conversation

@pi0
Copy link
Copy Markdown
Member

@pi0 pi0 commented May 20, 2025

In the current version of crossws, there is one peers global set per server. Publishing to the same "topic", published to all peers of the same topic.

While it works in simple servers with a single set of hooks, when using dynamic hooks (using resolver), it makes sense that each peer only subscribe and publish to relevant peers and topics don't overlap.

This PR segments global peers into "namespaces". By default, the request "pathname" is used for namespace, so peers connected to /_ws/room1 and /_ws/room2 are isolated from each other (BREAKING CHANGE).

It is possible to have customized namespace logic based on request:

  • Using adapter option getNamespace: (req: Request) => string)
  • Using upgrade(req) hook by returning { namespace }

Global .publish(topic, message) still publishes to all peers, but can specify a namespace using .publish(topic, message, { namespace })

@codecov
Copy link
Copy Markdown

codecov Bot commented May 20, 2025

Codecov Report

Attention: Patch coverage is 90.27778% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/hooks.ts 60.00% 4 Missing ⚠️
src/adapter.ts 90.32% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages Bot commented May 20, 2025

Deploying h3-crossws with  Cloudflare Pages  Cloudflare Pages

Latest commit: 023ea1b
Status: ✅  Deploy successful!
Preview URL: https://cd0588b6.h3-crossws.pages.dev
Branch Preview URL: https://feat-isolate-pubsub.h3-crossws.pages.dev

View logs

@pi0 pi0 marked this pull request as ready for review May 20, 2025 21:20
@pi0 pi0 merged commit c33d867 into main May 20, 2025
4 checks passed
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