# Connecting to AWS IoT Core over WebSockets

**URL:** https://discourse.nodered.org/t/connecting-to-aws-iot-core-over-websockets/84741
**Category:** Developing Nodes
**Tags:** websocket
**Created:** [19 January 2024 18:36 UTC](https://discourse.nodered.org/t/connecting-to-aws-iot-core-over-websockets/84741 "2024-01-19T18:36:14Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![FrankAr](https://avatars.discourse-cdn.com/v4/letter/f/278dde/32.png) [@FrankAr](https://discourse.nodered.org/u/FrankAr)
#### Post date: [19 January 2024 18:36 UTC](https://discourse.nodered.org/t/connecting-to-aws-iot-core-over-websockets/84741/1 "2024-01-19T18:36:14Z")

</div>

Hi there,  
I am trying to connect to AWS IoT Core using node red. I am behind a corporate proxy with a lot of restrictions concerning port availability etc, and can therefore not use the node-red-contrib-aws-iot-hub as no mqtt connections are allowed. My end goal is to subscribe to a topic and receive value updates in node-red.

I have decided to write my own node-red node, however I am having problems with the proxy configuration. The node works just fine when I test it on a raspberry pi outside the corporate network, but when deployed in the network I get ECONNREFUSED error with errno -111. I tried multiple ways of setting up the proxy, but each approach ended up in the same error.

Does someone maybe have experience with deploying the AWS node behind a proxy? Or any suggestion on how I can establish a connection would be very helpful and appreciated.

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [19 January 2024 18:47 UTC](https://discourse.nodered.org/t/connecting-to-aws-iot-core-over-websockets/84741/2 "2024-01-19T18:47:17Z")

</div>

Welcome to the forums @FrankAr

If I may, we won't help you circumvent a corporate firewall, it's not in our nature.

I know nothing about AWS, but someone here may know about its supporting NPM modules.  
But I stress, I hope the help someone here may provide is not a guide on how to "dodge" the security setup in your corporate environment.

---

<div class="post-metadata">

### Author: ![FrankAr](https://avatars.discourse-cdn.com/v4/letter/f/278dde/32.png) [@FrankAr](https://discourse.nodered.org/u/FrankAr)
#### Post date: [19 January 2024 18:58 UTC](https://discourse.nodered.org/t/connecting-to-aws-iot-core-over-websockets/84741/3 "2024-01-19T18:58:48Z")

</div>

Hi Marcus,  
I agree, dodging the firewall should not be discussed here, that is also in no way my goal here.  
I am not trying to dodge the firewall, rather to connect to AWS while operating within its limits. The firewall blocks MQTT connections categorically, which is why I am looking at connecting via WebSockets which are open. My problem is, that the existing node uses MQTT making it unusable for me. And my own solution using WebSockets can not find the server when behind proxy, even though I (as far as I can tell) configured it the right way in my node.

Just to clarify, I am not aiming to dodge the firewall, the firewall is just a reason one solution does not work. The semantics of the firewall are not to block AWS connections, therefore a connection using websockets is just to stay within the perimeter.

---

<div class="post-metadata">

### Author: ![marcus-j-davies](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/marcus-j-davies/32/103435_2.png) [@marcus-j-davies](https://discourse.nodered.org/u/marcus-j-davies)
#### Post date: [19 January 2024 19:10 UTC](https://discourse.nodered.org/t/connecting-to-aws-iot-core-over-websockets/84741/4 "2024-01-19T19:10:04Z")

</div>

Ok...

So I believe, the **ws** npm package can use an **agent**  
an agent in the node runtime, is the client "user-agent" if you will - I don't really know where it sits 🤔

An agent can specify a proxy server, below is one example.

> **[https-proxy-agent](https://www.npmjs.com/package/https-proxy-agent)**
>
> An HTTP(s) proxy \`http.Agent\` implementation for HTTPS. Latest version: 7.0.2, last published: 5 months ago. Start using https-proxy-agent in your project by running \`npm i https-proxy-agent\`. There are 3004 other projects in the npm registry using...

example:

```auto
const agent = new HttpsProxyAgent(options);
const socket = new WebSocket(endpoint, { agent: agent });`

```

I hope this helps

---

<div class="post-metadata">

### Author: ![GogoVega](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/gogovega/32/71313_2.png) [@GogoVega](https://discourse.nodered.org/u/GogoVega)
#### Post date: [19 January 2024 20:39 UTC](https://discourse.nodered.org/t/connecting-to-aws-iot-core-over-websockets/84741/5 "2024-01-19T20:39:58Z")

</div>

Check your AWS rules to accept the IP address  
PS: I'm not an expert, so I can't help you more

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [19 January 2024 21:06 UTC](https://discourse.nodered.org/t/connecting-to-aws-iot-core-over-websockets/84741/6 "2024-01-19T21:06:55Z")

</div>

> [@FrankAr](#):
>
> which is why I am looking at connecting via WebSockets which are open

Don't be totally sure of that, have you confirmed it?

Also, I hope you are using TLS secured connections? You really MUST do this (with the possible exception of having a private network connection to the inside of your AWS setup - but even then I wouldn't recommend it).

> [@FrankAr](#):
>
> firewall blocks MQTT connections categorically

Does the MQTT broker support websocket connections and is it configured?

---

<div class="post-metadata">

### Author: ![FrankAr](https://avatars.discourse-cdn.com/v4/letter/f/278dde/32.png) [@FrankAr](https://discourse.nodered.org/u/FrankAr)
#### Post date: [11 February 2024 23:33 UTC](https://discourse.nodered.org/t/connecting-to-aws-iot-core-over-websockets/84741/7 "2024-02-11T23:33:00Z")

</div>

Hi everyone,  
Firstly, thank you for your suggestions and help.  
I have managed to establish a connection, my problem was with setting the proxy settings the right way. I was setting just the websocket agent, which for some reason was not enough and I had to set global agent as well.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [11 April 2024 23:33 UTC](https://discourse.nodered.org/t/connecting-to-aws-iot-core-over-websockets/84741/8 "2024-04-11T23:33:08Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
