Apollo GraphQL: How to Set Up Secure Websockets?

2.6k views Asked by At

I'm setting up my dev system to use https, and Chrome is complaining about my websocket not begin secure:

VM4965:161 Mixed Content: The page at 'https://mywebsite.io/' was loaded over HTTPS, but attempted to connect to the insecure WebSocket endpoint 'ws://mywebsite.io:4000/subscriptions'. This request has been blocked; this endpoint must be available over WSS.

Here's my current server-side setup for WS, based on the Apollo docs:

const localHostString = 'mywebsite.io'; 
const pubsub = new PubSub();

// additional context you use for your resolvers, if any
const context = {connectors: connectors};

//SET UP APOLLO QUERY / MUTATIONS / PUBSUB
//start a graphql server with Express handling a possible Meteor current user
createApolloServer({
    schema,
    context
});

const METEOR_PORT = 3000;
const GRAPHQL_PORT = 4000;
const server = express();

server.use('*', cors({ origin: `https://${localHostString}:${METEOR_PORT}` }));

server.use('/graphql', bodyParser.json(), graphqlExpress({
    schema,
    context
}));

server.use('/graphiql', graphiqlExpress({
    endpointURL: '/graphql',
    subscriptionsEndpoint: `ws://${localHostString}:${GRAPHQL_PORT}/subscriptions`
}));

// Wrap the Express server
const ws = createServer(server);
ws.listen(GRAPHQL_PORT, () => {
    console.log(`GraphQL Server is now running on http://${localHostString}:${GRAPHQL_PORT}`);
    console.log(`GraphiQL available at http://${localHostString}:${GRAPHQL_PORT}/graphiql`);
    // Set up the WebSocket for handling GraphQL subscriptions
    new SubscriptionServer({
        execute,
        subscribe,
        schema
    }, {
        server: ws,
        path: '/subscriptions',
    });
});

How can I update this so as to use WSS rather than WS websockets?

Thanks in advance to all for any info.

1

There are 1 answers

0
Mike Martin On BEST ANSWER

It looks like you're doing

subscriptionsEndpoint: `ws://${localHostString}:${GRAPHQL_PORT}/subscriptions

Maybe instead, change ws to wss. ie:

subscriptionsEndpoint: `wss://${localHostString}:${GRAPHQL_PORT}/subscriptions