I'm trying to integrate socket.io
in my app, I will use it as a way to pass commands and real-time updates from server to client. I am able to connect both client and server and I'm also able to put the client in a room in which I will be emiting my events. However, the client doesn't seem to be catching events.
CLIENT-SIDE:
Connects client when window loads:
const socket = io("/");
socketConnect = function(){
socket.on("connect", () => {
console.log(socket.id);
})
};
window.onload = socketConnect();
Client clicks an upload button and form gets submitted, then client will wait for a confirmation from server:
(NOTE: This is a different .js file from the above code.)
submitForms = function(){
socket.on("formOK", () => {
console.log("form was sent");
socket.disconnect();
})
document.getElementById("form1").submit();
}
SERVER-SIDE:
Gets connection and puts client in a room:
//initiates socket.io
io.on("connection", (socket) =>{
clientID = socket.id;
roomID = `ROOM:${clientID}`;
//connects client to room where messages will be exchanged (room name = "ROOM:clientId")
socket.join(roomID);
});
Server handles form and confirms success to client:
app.post("/uploads/kit-info", (req,res) => {
//... handles post request
//sends success confirmation to client
io.to(roomID).emit("formOK");
});
The problem:
Everything works fine but I am unable to get the "formOK"
confirmation on the client-side. I've run tests and I confirm that the client is indeed in the room, so io.to(roomID).emit("formOK");
should be working, right?
I am not sure what is going wrong.