Thanks to @Mulan and because I come back here every 4 months, here the complete code in ECMAScript :
index.js
import {createServer} from "http";
import {Server} from "socket.io";
import orderHandler from "../src/handlers/orderHandler.js"
import userHandler from "../src/handlers/userHandler.js"
const httpServer = createServer();
const io = new Server(httpServer);
const { createOrder, readOrder } = orderHandler(io)
const { updatePassword } = userHandler(io)
const onConnection = (socket) => {
socket.on("order:create", createOrder);
socket.on("order:read", readOrder);
socket.on("user:update-password", updatePassword);
}
io.on("connection", onConnection);
httpServer.listen(3000);
console.log("Server started on port 3000");
orderHandler.js
export default (io) => {
const createOrder = function (payload) {
const socket = this; // hence the 'function' above, as an arrow function will not work
// ...
};
const readOrder = function (orderId, callback) {
// ...
};
return {
createOrder,
readOrder
}
}