Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: global publish #61

Merged
merged 2 commits into from
Aug 6, 2024
Merged

feat: global publish #61

merged 2 commits into from
Aug 6, 2024

Conversation

pi0
Copy link
Member

@pi0 pi0 commented Aug 6, 2024

Resolves #29

Based on new feature in #60 with peers being accessible globally, we can pick the first one to publish to all other peers.

Note: There seems to be a limitation with cloudflare that is not possible for it. In most cases, i would recommend to directly use peer itself to broadcast as it runs within the right context.

Cloudflare error:

Cannot perform I/O on behalf of a different request. I/O objects (such as streams, request/response bodies, and others) created in the context of one request handler cannot be accessed from a different request's handler. This is a limitation of Cloudflare Workers which allows us to improve overall performance. (I/O type: Native)

@pi0 pi0 self-assigned this Aug 6, 2024
Copy link

cloudflare-workers-and-pages bot commented Aug 6, 2024

Deploying crossws with  Cloudflare Pages  Cloudflare Pages

Latest commit: 1ba7908
Status: ✅  Deploy successful!
Preview URL: https://82ee1094.crossws.pages.dev
Branch Preview URL: https://feat-adapter-publish.crossws.pages.dev

View logs

@pi0 pi0 mentioned this pull request Aug 6, 2024
8 tasks
@pi0 pi0 merged commit 31f759f into main Aug 6, 2024
3 checks passed
@pi0 pi0 deleted the feat/adapter-publish branch August 7, 2024 09:28
@IlyaSemenov
Copy link

@pi0 Can you please publish this to npm? The last release was half a year ago.

publish(topic: string, message: any, options) {
const firstPeer = peers.values().next().value as Peer;
if (firstPeer) {
firstPeer.send(message, options);
Copy link

@rossinek rossinek Oct 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @pi0, thank you for this feature 🥇
I was about to use it although I think there is a bug here. You are publishing to firstPeer without checking whether it is subscribed to a topic.

I don't think there is currently an easy way to see which topics a peer is subscribed to without checking the protected property _topics, but temporary solution for me was to just do:

if (firstPeer['_topics'].has(topic)) firstPeer.send(message, options);

BTW. I wish Nitro would expose these utils somewhere (I couldn't find any way to access them from Nuxt)

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.

publish without peer
3 participants