You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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".
// added toString() to template literalconstroomName=`${userId}_${recipientId}`.toString();socket.join(roomName);
Suddenly, this works perfectly and correctly emits to the specific room using:
// works with added toString() to template literalio.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.
The text was updated successfully, but these errors were encountered:
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.
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.
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:
Later on, when I emit back to the client I do this:
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:
Upon changing the variable definition to:
Suddenly, this works perfectly and correctly emits to the specific room using:
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.
The text was updated successfully, but these errors were encountered: