Action responses

There are 2 ways to respond to the execution of a action

Using the response class:

socket.action("message", (req, res) => {
  res.send([
    { id: 1, username: "admin" },
    { id: 2, username: "demo" },
  ]);
});

Using return:

socket.action("register", (req, res) => {
  return [
    { id: 1, username: "admin" },
    { id: 2, username: "demo" },
  ];
});

The response class provides more functionality such as, for example, changing the response status, adding custom headers, enabling cors, and so on

You can also combine the use of response with return:

socket.action("message", (req, res) => {
  res.status(201);
  res.cors();
  res.header("x-custom-header", "my-header");

  return [
    { id: 1, username: "admin" },
    { id: 2, username: "demo" },
  ];
});

Last updated

Was this helpful?