79309606

Date: 2024-12-26 13:40:45
Score: 1.5
Natty:
Report link

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
    }
}
Reasons:
  • Blacklisted phrase (0.5): Thanks
  • Long answer (-1):
  • Has code block (-0.5):
  • User mentioned (1): @Mulan
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Akyno