Meteor/Apollo: SSL Access for HTTPS and WS?

安稳与你 提交于 2019-12-25 11:56:45

问题


I’m trying to get SSL working in Meteor for https and websockets. I’m getting this error:

(STDERR) Error: listen EACCES 0.0.0.0:443

Here's my setup.

For SSL access I have installed nourharidy/meteor-ssl. I can use any comparable package or approach that people have found useful!

As required by nourharidy/meteor-ssl, in server/main.js I have:

SSL('/path/to/private/server.key','/path/to/private/server.crt', 443);

Here's the rest of my setup:

My ROOT_URL environment variable is:

https://10.0.1.10:443 //I’m using my Mac’s ip address so that another Mac can access it

In imports/startup/server/index.js I have:

//SET UP APOLLO QUERY / MUTATIONS / PUBSUB
const USING_HTTPS = true;
const httpProtocol =  USING_HTTPS ? "https" : "http"; 
const localHostString = '10.0.1.10' //I’m using my Mac’s ip address so that another Mac can access it

const METEOR_PORT = 443;
const GRAPHQL_SUBSCRIPTION_PORT = 4000;
const subscriptionsEndpoint = `wss://${localHostString}:${GRAPHQL_SUBSCRIPTION_PORT}/subscriptions`;

const server = express();
server.use('*', cors({ origin: `${httpProtocol}://${localHostString}:${GRAPHQL_SUBSCRIPTION_PORT}` }));
server.use('/graphql', bodyParser.json(), graphqlExpress({
    schema
}));
server.use('/graphiql', graphiqlExpress({
    endpointURL: '/graphql',
    subscriptionsEndpoint: subscriptionsEndpoint
}));
// We wrap the express server so that we can attach the WebSocket for subscriptions
const ws = createServer(server);
ws.listen(GRAPHQL_SUBSCRIPTION_PORT, () => {
    console.log(`GraphQL Server is now running on ${httpProtocol}://${localHostString}:${GRAPHQL_SUBSCRIPTION_PORT}`);

    // Set up the WebSocket for handling GraphQL subscriptions
    new SubscriptionServer({
        execute,
        subscribe,
        schema
    }, {
        server: ws,
        path: '/subscriptions',
    });
});

What am I missing?

Thanks very much in advance to all for any info!


回答1:


The way Stack Overflow works is that you put some effort in, and get close to the problem, and we'll help you get through that last bit. Asking for help on how to use Google is what we call off-topic.

If you have done a Google search for Error: listen EACCES 0.0.0.0:443 would have yielded a bunch of results, the first of which is this:

Node.js EACCES error when listening on most ports

So I am willing to be helpful, but you need to help yourself too




回答2:


Solved it! It turns out my server setup was fine.

Server-side I was building the WSS url like so:

var websocketUri = Meteor.absoluteUrl('subscriptions').replace(/http/, 'ws').replace('3100', '3200');

The Meteor docs for absoluteUrl say:

The server reads from the ROOT_URL environment variable to determine where it is running.

In Webstorm I have ROOT_URL set to https://10.0.nn.nn:3100. But for some reason absoluteUrl was returning http://10.0.nn.nn:3000. I.e. it had the wrong port.

After correcting the port-- it’s working!

UPDATE: I thought I had it solved when I posted a few days ago, but I was still missing something.

I'm now using ngrok to provide SSL to my dev system for use with HTTPS and WSS.

Here are a few details.

  • I run Meteor on localhost:3000.
  • I assign the value of "http://localhost:3000" to the ROOT_URL environment variable.
  • I run two simultaneous ngrok connections, one for HTTPS and one for WSS:

./ngrok http 3000 ./ngrok http 3200

  • This gives me two ngrok urls, for example:

Forwarding https://9b785bd3.ngrok.io -> localhost:3000
Forwarding https://ec3d8027.ngrok.io -> localhost:3200

I access my app via the ngrok url, for example:

https://9b785bd3.ngrok.io

I build my HTTPS links and WSS links based on the ngrok addresses. For example:

const wssEndpoint = 'wss://ec3d8027.ngrok.io/subscriptions';

I hope this is helpful to others looking into this.



来源:https://stackoverflow.com/questions/46249101/meteor-apollo-ssl-access-for-https-and-ws

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!