Skip to content

onSend hook not being called on web socket route #203

Open
@michele-tonizzo

Description

@michele-tonizzo

Hello,

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugConfirmed bugdocumentationImprovements or additions to documentationgood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions