'Fastify onSend hook not being called on web socket route

I'm filtering the data returning from a route before being sent back to the client using onSend hook and works as expected

fastify.addHook("onSend", (request, reply, payload, next) => {
    const mask = request?.query?.["mask"];

   // ...

    const filtered = applyMask(object, mask);

    return next(null, JSON.stringify(filtered));
  });

  return next();
};

I also have web socket connections in my API that I would like behave the same before sending the message to the client but apparently, even if the in documentation of @fastify/websocket it states that "it will run any hooks that have been registered", it does not apply to onSend hook when sending back using connection

connection.socket.send(JSON.stringify(data));

From testing the hook is not called entirely.

Is this the correct behaviour? How can I achieve the same result that I have using HTTP methods?

Thanks



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source