Skip to content

Subscribe resolver access to websocket #465

Closed Answered by enisdenjo
jmc420 asked this question in Q&A
Discussion options

You must be logged in to vote

If there was access to the web socket in the subscribe function (via a context?)

Exactly, this is the way to do it. You can find the socket in the context extra which you can pass to the GraphQL context:

import { WebSocketServer } from 'ws';
import { useServer } from 'graphql-ws/lib/use/ws';
import { schema } from './my-graphql-schema';

const server = new WebSocketServer({
  port: 4000,
  path: '/graphql',
});

useServer(
  {
    schema,
    // pass the socket to the GraphQL context
    context: (ctx) => ({ socket: ctx.extra.socket }),
  },
  server,
);

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@jmc420
Comment options

Answer selected by enisdenjo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants