Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(installSubscriptionHandlers): enable passing WebSocket.Server in…
…stead of HttpServer For people who need to have multiple WebSocket servers sharing a single HTTP(S) server, the `ws` docs recommend [creating multiple instances of `new WebSocket.Server({ noServer: true })`](https://github.com/websockets/ws#multiple-servers-sharing-a-single-https-server). This PR makes `installSubscriptionHandlers` accept a `WebSocket.Server` instance (in addition to accepting an http server) to support the multiple WebSocket servers use case. Example: ```js const graphQLWebSocket = new WebSocket.Server({ noServer: true, }) apolloServer.installSubscriptionHandlers(graphQLWebSocket) const symmetryWebSocket = new WebSocket.Server({ noServer: true, }) symmetryWebSocket.on('connection', socket => new SymmetryServer({socket})) /** * This is a workaround. When WebSocket.Server handles an upgrade request, * it will abort the handshake if the request path doesn't match -- meaning it * will break Meteor's DDP web socket. So we only want the WebSocket.Server * for GraphQL to receive the upgrade event if it's not for Meteor. */ httpServer.on('upgrade', (request: IncomingMessage, socket: net$Socket, head: any) => { if (request.url.startsWith(SYMMETRY_BROWSER_PATH)) { symmetryWebSocket.handleUpgrade(request, socket, head, (ws: WebSocket) => { symmetryWebSocket.emit('connection', ws) }) } else if (request.url.startsWith(GRAPHQL_PATH)) { graphQLWebSocket.handleUpgrade(request, socket, head, (ws: WebSocket) => { graphQLWebSocket.emit('connection', ws) }) } else { socket.destroy() } }) ```
- Loading branch information