Currently, I am in the process of configuring a socket.io chat feature with an expressjs backend and sveltejs frontend.
I have established a custom namespace named 'chat' where a new room is generated upon a 'join' request.
My approach closely aligns with the official documentation.
Below is my code snippet:
Server:
const app = express();
const server = app.listen(3002);
log.info("Express server has started on port 3002");
const io = require("socket.io")(server, { path: "/api/express/socket.io" });
const chat = io.of("/chat");
chat.on("connection", socket => {
log.info("New User connected");
socket.on("join", room => {
log.info("New User joined room: " + room);
socket.join(room);
});
socket.on("chat message", data => {
log.info("'chat-message'-Event: ", data);
chat.in(data.room).emit("chat message", {
room: data.room,
msg: data.msg,
user: data.user
});
});
});
Client:
let chatSocket
onMount(async () => {
chatSocket = io('https://my.domain.com/chat', {
path: '/api/express/socket.io',
transports: ['websocket'],
})
chatSocket.on('connection', function(socket) {
socket.on('chat message', function(data) {
alert(data.msg)
})
})
chatSocket.emit('join', 'Chat-Room#' + id)
})
const Submit = async e => {
chatSocket.emit('chat message', {
room: 'Chat-Room#' + id,
msg: statusText,
user,
})
}
Upon checking the server console output, everything appears to be configured correctly.
All events are being triggered as expected and logged to console.
However, the clients are not receiving any 'chat message' events (even though they are sending them successfully).
Do you have any insights into what might be going wrong here?