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

ws.publish() should not echo back to self #2709

Closed
jakeg opened this issue Apr 21, 2023 · 3 comments
Closed

ws.publish() should not echo back to self #2709

jakeg opened this issue Apr 21, 2023 · 3 comments
Labels
bug Something isn't working

Comments

@jakeg
Copy link
Contributor

jakeg commented Apr 21, 2023

What version of Bun is running?

0.5.9

What platform is your computer?

WSL (Linux 4.19.104-microsoft-standard x86_64 x86_64)

What steps can reproduce the bug?

Sample code:

const server = Bun.serve({
  port: 3001,
  fetch (req) {
    server.upgrade(req)
    return
  },
  websocket: {
    open (ws) {
      ws.subscribe('some-topic')
      ws.publish('some-topic', 'Message published by ws.publish()...')
      server.publish('some-topic', 'Message published by server.publish()...')
    }
  }
})

new WebSocket('ws://localhost:3001').onmessage = console.log

What is the expected behavior?

ws.publish('some-topic') should be sent to all other websockets connected to that topic, but not the sending websocket itself

What do you see instead?

It sends to all websockets in the topic, including self.

Additional information

A few months ago I asked about websocket pub/sub and whether or not published messages get echo'd back to the sender. I think @Jarred-Sumner found that they did, but shouldn't, and that you were working on a fix. The current docs say "...and .publish() messages to all other subscribers to that topic" which suggests they shouldn't as well. Just testing it now, looks like they do currently echo back to the sender though. Is there a workaround or fix in progress?

@jakeg jakeg added the bug Something isn't working label Apr 21, 2023
@Jarred-Sumner
Copy link
Collaborator

Check out the publishToSelf option in the weboscket object handler. It's not enabled by default because it is a subtle breaking change and we want to avoid those. We could make this change in v0.6 though.

https://github.com/oven-sh/bun/blob/main/packages/bun-types/bun.d.ts#L1433-L1446

@jakeg
Copy link
Contributor Author

jakeg commented Apr 21, 2023

Much appreciated. I think this would be worth adding to v0.6, or adding mention of this to the docs https://bun.sh/docs/api/websockets if not.

@jakeg jakeg closed this as completed Apr 21, 2023
@jakeg jakeg reopened this Apr 21, 2023
@Jarred-Sumner
Copy link
Collaborator

This was fixed in v0.6.3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants