3

I want to know is it possible access to ws instance from router controller level?

this is my entrypoint :

// app.js
// .... app configs
const server = createServer(app);

const websocket = new WebSocketServer({ httpServer: server });

websocket.on("request", (request) => {
  let connection = request.accept(null, request.origin);

  connection.on("message", (message) => {
    connection.send("message");
    console.log(message);
  });
  connection.on("close", () => console.log("client closed"));

  return connection

});

I did some modification here in request object for accessing ws instance in every request object:

app.use((req, res, next) => {
  req.ws = websocket;
  next();
});

so I want to know how can I publish messages with :

req.ws.send({message: "hi"}) 

I tried this trick with socket-io . but here I cannot access to ws.send() function.

ShayanJZ
  • 61
  • 4

0 Answers0