Sunday 12 March 2017

Heroku with graphql subscription on node js app error

has anyone here have tried deploying a graphql application with subscription to heroku? I am having a hard time to make it work. I am receiving an error to the browser's console saying

    WebSocket connection to 'ws://myapplicationname.herokuapp.com/'
 failed: Error during WebSocket handshake: Unexpected response code: 404

here is what the heroku heroku logs --tail output

2017-03-13T02:25:19.763339+00:00 heroku[router]: at=info method=GET path="/" host=myapp.herokuapp.com request_id=7d25213f-b3ef-435a-87e6-95a38a283574 fwd="192.168.40.56,202.90.128.84" dyno=web.1 connect=3ms service=8ms status=404 bytes=488 protocol=http

this is my client side code that handle the graphql subscription:

// connect to web-socket for subscription
const wsClient = new SubscriptionClient(`ws://myapplicationname.herokuapp.com/`, {
    reconnect: true,
    connectionParams: {
        // Pass any arguments for initialization 
    }
});

and here is the server side

let server;

const app = express();

const port = process.env.PORT || 4000;

// Don't rate limit heroku
app.enable('trust proxy');

// FIXES CORS ERROR
const whitelist = [
  'http://localhost:8080',
  'http://myapp.herokuapp.com',
];
const corsOptions = {
  origin(origin, callback) {
    const originIsWhitelisted = whitelist.indexOf(origin) !== -1;
    callback(null, originIsWhitelisted);
  },
  credentials: true,
};
app.use(cors(corsOptions));

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use('/graphql', (...args) => graphqlMiddleware(...args));
app.use('/graphiql', (...args) => graphiqlMiddleware(...args));

server = http.createServer(app);

const subscriptionServerConfig = {
  server,
  path: '/',
};

let subscriptionServer = new SubscriptionServer({
  subscriptionManager,
}, subscriptionServerConfig);

server.listen(port, () => {
  console.log(`API is now running on port ${port}`);
});

server.on('close', () => {
  server = undefined;
});

when I test it to my local machine by changing the url to localhost:port there is no problem. Is there any additional configuration I need to make subscription work on heroku?



via thevinci

No comments:

Post a Comment