Apollo-server: Apollo Server 2.0 RC, subscriptions and Websocket secure (WSS)

Created on 22 Jun 2018  路  7Comments  路  Source: apollographql/apollo-server

Hello all,

I'm trying to migrate one of my Apollo server from v1 to v2 and I'm unable to have subscriptions working with wss. I have followed the official documentation and read several articles on the Internet but nothing really clear explaining how to set up wss. Here my code:

`import express from "express";
import { createServer } from "https";
import { readFileSync } from "fs";
import { ApolloServer, gql } from "apollo-server-express";
import { resolvers } from "./data/resolvers";

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

const typeDefs = gql ${readFileSync("./data/schema.graphql", "utf-8")} ;
const server = new ApolloServer({ typeDefs, resolvers });

server.applyMiddleware({ app });

const sslOptions = {
key: readFileSync("./ssl/key.pem"),
cert: readFileSync("./ssl/cert.pem"),
};
const wss = createServer(sslOptions, app);
server.installSubscriptionHandlers(wss);

wss.listen(PORT, () =>
console.log(馃殌 Server ready at https://localhost:${PORT}${server.graphqlPath})
);
`
When lauching the Node.js server, I have no error message, just the successful one:

[email protected] start /usr/src/app
babel-node index.js

馃殌 Server ready at https://localhost:3000/graphql
Connected to MongoDB database.

But by testing, I can't access the server via wss and the playground is also unavailable via https 馃槥
Thanks a lot!

All 7 comments

Closed, issue was not related to Apollo... For info, the above code is totally functional if you want to set up wss! Sorry for the noise!

I'm glad you were able to get it to work @stephanerotureau. In case you run into any issue, here is a practical example of how it works on Apollo Server 2.0 and a client app

I found it confusing query and mutations can be done at root /, but subscriptions need to be done /graphql

I am setting up WSS with typescript, but I am getting the following error:

Argument of type 'import("https").Server' is not assignable to parameter of type 'import("http").Server'. Property 'maxHeadersCount' is missing in type 'Server'.

The error happens when i am calling installSubscriptionHandlers(server) where server is an instance of https.Server. Any idea?

For now, I am type casting the server to any.

@prabak
I've had same problem with you. The type signature of installSubscriptionHandlers should be changed like HttpServer | HttpsServer.

https://github.com/apollographql/apollo-server/blob/master/packages/apollo-server-core/src/ApolloServer.ts#L391

I'm glad you were able to get it to work @stephanerotureau. In case you run into any issue, here is a practical example of how it works on Apollo Server 2.0 and a client app

Not wss

How do you know if your websocket is now wss or still a ws?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

danilobuerger picture danilobuerger  路  3Comments

leinue picture leinue  路  3Comments

Magneticmagnum picture Magneticmagnum  路  3Comments

dupski picture dupski  路  3Comments

stevezau picture stevezau  路  3Comments