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

Make room name string argument compatible with ES6 template literals #5010

Closed
petersonprojects opened this issue Apr 25, 2024 · 4 comments
Closed

Comments

@petersonprojects
Copy link

socket.io v4.7.2
socket.io-client v4.7.2

The Problem

I wasn't sure if this was a bug or intended behavior, but basically, this is what I mean by my title.

I was getting a userId and recipientId from my emitter on the client-side like so:

// received on the backend
const roomName = `${userId}_${recipientId}`;

Later on, when I emit back to the client I do this:

io.to(roomName).emit('new_message', updatedMessages);

However, I noticed that because of the use of a template literal to define roomName, it was not properly emitting to the room and it was not throwing an error either for the room not "existing".

Just for clarity, I joined the room the same way:

const roomName = `${userId}_${recipientId}`;

socket.join(roomName); 

Upon changing the variable definition to:

// added toString() to template literal
const roomName = `${userId}_${recipientId}`.toString();

socket.join(roomName); 

Suddenly, this works perfectly and correctly emits to the specific room using:

// works with added toString() to template literal
io.to(roomName).emit('new_message', updatedMessages);

Describe the solution you'd like

This was very frustrating to deal with because after looking at the documentation, the io.to() function expects a string, which a template literal is. ES6 has been around for a long time and it would be nice if this function could support passing template literals as the room name argument I think it would save people a big headache if they are interpolating their room's name this way.

Obviously, there are other ways to do this, and the toString() method doesn't even need to be used, nor do custom room names need to be created, but this would be a "nice to have" and could maybe save many people future headache/frustration with this.

I think at the very least, it should throw some error if there is an issue with the format of the room argument.

P.S.
Please let me know if more code context is needed to understand what I'm talking bout. I figured this was enough context since the issue is fairly straightforward.

@petersonprojects petersonprojects added the enhancement New feature or request label Apr 25, 2024
@darrachequesne
Copy link
Member

Hi! That's weird, there should be nothing specific about template literals.

I was not able to reproduce the issue: https://github.com/socketio/socket.io-fiddle/tree/issues/socket.io/5010

Could you please check?

@darrachequesne darrachequesne added needs investigation and removed enhancement New feature or request labels Apr 26, 2024
@petersonprojects
Copy link
Author

From what I observed, it will still emit the event, but it seemed to ignore the room name and instead emits it to ALL rooms if the socket is part of multiple rooms.
Using your js fiddle I was not able to recreate it.
I am starting to think it has something to do with my client being written in TypeScript, and that maybe the values were not being sent from the client as strings as I expected.

Thanks so much for taking a look.

@darrachequesne
Copy link
Member

I'm afraid that without a way to reproduce, we won't be able to help you..

Please keep us updated if you find the culprit!

@petersonprojects
Copy link
Author

I figured out that I was accidentally creating duplicate listeners on my client side (React) so when I broadcasted to a room it was being received several times on an emit. When I added the toString to the template literal, I also added some logic that I didn't realize fixed the listeners as well. Template literals 100% working fine.

@petersonprojects petersonprojects closed this as not planned Won't fix, can't repro, duplicate, stale May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants