question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using subscriptions with express on the same port

See original GitHub issue

The current docs have a example on how to do this here of importance there is a note that says:

// ⚠️ Pay attention to the fact that we are calling `listen` on the http server variable, and not on `app`.
httpServer.listen(PORT, () => {
  console.log(`🚀 Server ready at http://localhost:${PORT}${server.graphqlPath}`)
  console.log(`🚀 Subscriptions ready at ws://localhost:${PORT}${server.subscriptionsPath}`)
})

Unfortunately using the httpServer to listen on a port disables all the express routing on that port.

I’ve been getting around this by doing something like:

httpServer.listen(PORT, () => {
  console.log(`🚀 Server ready at http://localhost:${PORT}${server.graphqlPath}`)
  console.log(`🚀 Subscriptions ready at ws://localhost:${PORT}${server.subscriptionsPath}`)
})

app.listen(differentPort, () => console.log(`listening on http://localhost:${port}`));

But this isn’t really ideal, is there a way to get the subscriptions and the express application to work on the same port?

Additional Information

I found what appears to be older documentation setting up the apollo-server to do this exact thing:

import express from 'express';
import bodyParser from 'body-parser';
import { graphqlExpress } from 'apollo-server-express';
import { createServer } from 'http';
import { execute, subscribe } from 'graphql';
import { PubSub } from 'graphql-subscriptions';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import { myGraphQLSchema } from './my-schema';

const PORT = 3000;
const app = express();

app.use('/graphql', bodyParser.json(), graphqlExpress({ schema: myGraphQLSchema }));

const pubsub = new PubSub();
const server = createServer(app);

server.listen(PORT, () => {
    new SubscriptionServer({
      execute,
      subscribe,
      schema: myGraphQLSchema,
    }, {
      server: server,
      path: '/subscriptions',
    });
});

However, the apollo-server-express package no longer exports a graphqlExpress module and instead only has ApolloServer, ServerRegistration, registerServer. So far I’ve only used ApolloServer with installSubscriptionHandlers to setup basic subscriptions and haven’t looked at the other methods. Is there a way to setup subscriptions on the same port as the express application or am I just chasing a dead end?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:5
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

45reactions
Elfayercommented, Feb 21, 2019

Solution here: https://www.apollographql.com/docs/apollo-server/features/subscriptions.html#middleware

const http = require('http');
const { ApolloServer } = require('apollo-server-express');
const express = require('express');

const PORT = 4000;
const app = express();
const server = new ApolloServer({ typeDefs, resolvers });

server.applyMiddleware({app})

const httpServer = http.createServer(app);
server.installSubscriptionHandlers(httpServer);

// ⚠️ Pay attention to the fact that we are calling `listen` on the http server variable, and not on `app`.
httpServer.listen(PORT, () => {
  console.log(`🚀 Server ready at http://localhost:${PORT}${server.graphqlPath}`)
  console.log(`🚀 Subscriptions ready at ws://localhost:${PORT}${server.subscriptionsPath}`)
})
5reactions
gforgecommented, Dec 2, 2018

As part of debugging a similar issue I wrote a minimal Express + Apollo server app with subscriptions, you can find it here: https://github.com/gforge/subscription_example It seems to work with shared port. Hope it helps!

Read more comments on GitHub >

github_iconTop Results From Across the Web

GraphQL subscriptions with Node.js and Express
Implement GraphQL subscriptions in Node, allowing you to generate notifications in real time via a pub/sub system.
Read more >
Apollo-server-express graphql-subscription and socket.io ...
Please I need a help! I'm working on a nodejs backend server which I have apollo-server-express and socket.io working perfectly.
Read more >
Subscriptions in Apollo Server - Apollo GraphQL Docs
We can pass this schema object to both the subscription server and ApolloServer . This way, we make sure that the same schema...
Read more >
GraphQL Subscriptions Setup in React and Express.js
Learn how to setup GraphQL subscriptions in React and Express.
Read more >
GraphQL: Server-side Subscriptions | by Yuri Nezdemkovski
Subscriptions allow realtime updates about data changes in a standard ... By default it runs on the 27017 port so we will use...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found