How do I use web socket inside a graphql mutation using socket.io and expressGraphQL?

How do I use web socket inside a graphql mutation using socket.io and expressGraphQL?


0

`I have been trying to emit an event to a specific user using socket.io when a specific mutation is called

I tried to use this on my main server file:

   const graphqlMiddleware = expressGraphQL((req, res) => ({
      schema,
      graphiql: __DEV__,
      rootValue: {
      request: req,
        response: res,
       },
       pretty: __DEV__,
   }));

      app.use("/graphql", graphqlMiddleware);

      let server = app.listen(socketPort);
      let socketio = require("socket.io")(server);
      connection(socketio);

and this on my connection.js file:

    let globalSocketIO;

    const __DEV__ = environment;

    const connection = (io) => {

    globalSocketIO = io;

    console.log('io', io);

    io.on('connection', async function (socket) {
        console.log('connected to socket')
        io.use(decodeJwt);

        });

   }

   export function getGlobalSocketIO() {
    return globalSocketIO;
   };

   export default connection;

and this on my mutation file:

   const globalSocketIO = getGlobalSocketIO();

   const testMutation = {
   type: BookingRequestType,

   async resolve({ request }) {
    globalSocketIO.emit('event-' + userId, { data: responseData }); 
   }}

but it returns this error "cannot read properties of undefined using (reading ’emit’)

`


Load 3 more related questions


Show fewer related questions

0



Leave a Reply

Your email address will not be published. Required fields are marked *