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

websocket not publishing to topic after connection close. #6691

Closed
RohitKaushal7 opened this issue Oct 24, 2023 · 2 comments · Fixed by #6739
Closed

websocket not publishing to topic after connection close. #6691

RohitKaushal7 opened this issue Oct 24, 2023 · 2 comments · Fixed by #6739
Labels
bug Something isn't working bun.js Something to do with a Bun-specific API

Comments

@RohitKaushal7
Copy link
Contributor

RohitKaushal7 commented Oct 24, 2023

What version of Bun is running?

1.0.7

What platform is your computer?

Linux 5.10.102.1-microsoft-standard-WSL2 x86_64 x86_64

What steps can reproduce the bug?

just create a simple websocket chat application as described in the documentation.
https://bun.sh/docs/api/websockets#pub-sub

What is the expected behavior?

close(ws) {
      const msg = `${ws.data.username} has left the chat`;
      ws.unsubscribe("the-group-chat");
      ws.publish("the-group-chat", msg);  // 🔴 this message should get published to the topic and other subscribers should get this
}

What do you see instead?

The message doesn't get published. 😕
I ran some tests and also checked the return value of the publish method.
seems like it returns a number

  • (3+ message length) : when it successfully publishes the message and
  • 0: if the message is not published.

Message is not published if there is only one user subscribing to the topic and it is the sending user himself, which is OK.
but I tried with 2 and 3 users connected, and that publish line always returned 0, and the message in the close callback never gets published, so no one got to know that some user disconnected from chat.

Additional information

No response

@RohitKaushal7 RohitKaushal7 added the bug Something isn't working label Oct 24, 2023
@RohitKaushal7 RohitKaushal7 changed the title websocket not publishing to topic after connection close. websocket not publishing to topic after connection close. Oct 24, 2023
@Electroid Electroid added the bun.js Something to do with a Bun-specific API label Oct 24, 2023
@Jarred-Sumner
Copy link
Collaborator

You want server.publish instead of ws.publish

@RohitKaushal7
Copy link
Contributor Author

@Jarred-Sumner Thanks, that worked. 🙂

But need to fix this in the docs here https://bun.sh/docs/api/websockets#pub-sub to clarify that closed ws cannot publish. Also, is it required to call ws.unsubscribe('topic') in the closed callback, does it not automatically get unsubscribed if the connection is closed?

You can close this issue if you want.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working bun.js Something to do with a Bun-specific API
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants