Skip to content

Best way to add an event listener to save resources #3771

Answered by darrachequesne
shunz19 asked this question in Q&A
Discussion options

You must be logged in to vote

Here's a common application structure:

index.js

const httpServer = require("http").createServer();
const io = require("socket.io")(httpServer);

const { createOrder, readOrder } = require("./orderHandler")(io);
const { updatePassword } = require("./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);

orderHandler.js

module.exports = (io) => {
  const createOrder = function (payload) {
    const socket = this; // hence the 'function' above, as an arrow function will not work
    // ...
  };

  const readOrder = function (

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@shunz19
Comment options

@darrachequesne
Comment options

Answer selected by shunz19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants