Subscriptions-transport-ws: Binding a SubscriptionServer to a pre-existing httpServer breaks existing web sockets

Created on 24 Jul 2017  路  5Comments  路  Source: apollographql/subscriptions-transport-ws

Hi All,

In my OP, I'm not going to go through all the pain required in compiling a standalone example of the problem; I will just give you a summary. I will report more data later, as needed.

We are using GraphQL subscriptions in our Meteor 1.5 app. Everything works accordingly the specifications; except that fact that I am forced to run the web socket for GraphQL subscriptions
on a separate web server, because if I bind it to the web server where the Meteor server is already running, it breaks Meteor's own websocket connection.

So, if I do this (example taken from here):

import { WebApp } from 'meteor/webapp';
import { execute, subscribe } from 'graphql';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import { myGraphQLSchema } from './my-schema';
new SubscriptionServer({
  schema: myGraphQLSchema,
  execute,
  subscribe,
}, {
  server: WebApp.httpServer,
  path: '/subscriptions',
});

... Then subscriptions will work properly, but Meteor's socket won't:

On the browser console, I get this:

WebSocket connection to 'ws://localhost:3000/sockjs/311/ov5_gsv3/websocket' failed: Connection closed before receiving a handshake response

However, if I run the web sockets for subscription on a different port, like this:

// Create WebSocket listener server                               
websocketServer = http.createServer(
    (_request, response) => {
        response.writeHead(404);
        response.end();
    });
// Bind it to port and start listening                            
websocketServer.listen(WS_PORT);

// Create the new subscription server                                 
new SubscriptionServer({
    schema,
    execute,
    subscribe,
}, {
    server: websocketServer,
    path: '/subscriptions',
});

... then both sockets work normally.

Do you have any idea what might be causing this?

The current versions I am using are:

    "graphql-subscriptions": "0.4.4",
    "subscriptions-transport-ws": "0.8.1",

Thanks.

Most helpful comment

alright so i've figured it out, here is an example of how to combine meteor socket with graphql's:
https://github.com/DxCx/meteor-graphql-rxjs/commit/216856856e00e3f533e4ce39badd37f38274a4b8

All 5 comments

I'm experiencing the same issue. Does someone have any idea on how to solve this?

As for me, I am still using a different port.

from a glance look is looks like it is because ws.Server (Under SubscriptionServer) will handle upgrade requests before meteor's sockjs.
then, when you set path: '/subscriptions', ws.Server will abortConnection if this is not the path given.

abortConnection will just socket.close().

So this issue should be really under ws repository.
and probably what we need is a flag tell it not to destroy sockets not for it as sock-js does run properly on the background.

alright so i've figured it out, here is an example of how to combine meteor socket with graphql's:
https://github.com/DxCx/meteor-graphql-rxjs/commit/216856856e00e3f533e4ce39badd37f38274a4b8

Is @DxCx 's solution the best practice solution (it seems more like a workaround), or should the problem be considered a bug that needs to be fixed by package authors?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

booboothefool picture booboothefool  路  4Comments

damour picture damour  路  3Comments

jedwards1211 picture jedwards1211  路  4Comments

nickofthyme picture nickofthyme  路  3Comments

leshane picture leshane  路  3Comments