I'm using react frontend with express backend.
React socket code
import io from "socket.io-client";
function ReactComponent() {
const socket = io("http://localhost:5000");
socket.on('connection', () => {
console.log("connection")
});
useEffect(() => {
socket.emit('new-user', "hello");
}, []);
return(<p>Test text</p>);
}
export default ReactComponent;
Express socket code
const express = require('express');
const app = express();
const http = require('http').Server(app);
const io = require('socket.io')(http);
io.on('connection', (socket) => {
console.log("New client connected");
socket.on('new-user', (room) => {
socket.join(room);
socket.to(room).broadcast.emit('user-connected', "thx for info");
})
});
From looking at the documentation and other examples, this should work and if it does, express should print out "New client connected" yet that doesn't happen.