Reputation: 980
I have two clients, Client 1 sends data to our Node.js & Express back-end, and the back-end emits an event that only Client 2 can receive it. After, Client 2 receives the data and displays it.
Below are the codes for Client 1:
var socket = io();
socket.emit("match_package", {user_uid, match_uid, room_id, user_data});
Node.js Express server:
var tech = io.of("/");
tech.on("connection", (socket) => {
socket.on("match_package", (data) => {
var match_uid = data.match_uid;
var room_id = data.room_id;
socket.emit(match_uid, room_id);
socket.emit("1", "hello");
}
}
Client 2:
var socket = io();
socket.on('connect', () => {
socket.on(user_uid, (data) => {
console.log(data);
})
socket.emit("1", (data) => {
console.log(data);
})
})
The match_uid
from the back-end is the same as the user_uid
in Client 2 (verified). That is how that event is only visible to Client 2. But somehow on Client 2, the console.log
are not displaying any data. I can guarantee that the data are received fine in the back-end. But somehow Client 2 does not receive it.
I even emitted a dummy event "1" from the back-end. That is not received by Client 2 either.
What did I do wrong here?
Thanks a lot for your help and time.
Upvotes: 0
Views: 1621
Reputation: 195
You need to store all the socket.ids in array, then you can get that socket.id with the receiver name whenever you are sending message.
Here you can find what you need
Upvotes: 1