# MQTT, Public URL and React

**URL:** <https://forums.balena.io/t/mqtt-public-url-and-react/370100>\
**Category:** Project help\
**Created:** [March 12, 2024, 10:45am UTC](https://forums.balena.io/t/mqtt-public-url-and-react/370100 "2024-03-12T10:45:32Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jamiear](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@jamiear](https://forums.balena.io/u/jamiear)\
**Post date:** [March 12, 2024, 10:45am UTC](https://forums.balena.io/t/mqtt-public-url-and-react/370100/1 "2024-03-12T10:45:32Z")

</div>

Hi all,  
I am facing a problem that I am not sure how to solve.  
I want to add remote configuration for my apps. I am doing this using React and Typescript where I open a webpage over port 80.  
One of the catches is that I do not have access to the local file system this way. I therefore would like to publish this config.json over MQTT.  
I have a mosquitto container configured to listen socket on port 9001, but when I try connect my mqtt client to the URL wss:localhost:9001 I get the following errors.  
bundle.js:125597 WebSocket connection to ‘wss://localhost:9001/’ failed:  
WebSocket connection to ‘wss://f2d2fe3110830500d6d35e536afb4ecf.balena-devices.com:80/ws’ failed:  
I’m really hoping there is a simple solution to this problem, but yeah. Any advice would be greatly appreciated

---

<div class="post-metadata">

**Author:** ![mpous](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mpous/32/4742_2.png) [@mpous](https://forums.balena.io/u/mpous)\
**Post date:** [March 12, 2024, 11:28am UTC](https://forums.balena.io/t/mqtt-public-url-and-react/370100/2 "2024-03-12T11:28:00Z")

</div>

Hello @jamiear first of all welcome to the balena community!

Could you please share more details of the docker-compose and see how we can help you more?

Thanks!

---

<div class="post-metadata">

**Author:** ![jamiear](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@jamiear](https://forums.balena.io/u/jamiear)\
**Post date:** [March 12, 2024, 11:46am UTC](https://forums.balena.io/t/mqtt-public-url-and-react/370100/3 "2024-03-12T11:46:42Z")

</div>

Hey @mpous and thanks for the speedy reply!

My docker-compose looks like this

```auto
version: '2'
volumes:
  dbus:
  minimalx:
services:
  configuration_manager:
    build: ./configuration_manager
    network_mode: "host"
    volumes:
      - 'minimalx:/minimalx'
    ports:
      - 80:80
      - 3000:3000
      - 1883:1883
  mqtt:
    build: ./mqtt
    network_mode: "host"
    ports:
      - 1883:1883
      - 9001:9001

```

and the mqtt app is basically just eclipse-mosquitto:latest with some config that looks like this

```auto
persistence false

listener 9001
protocol websockets

listener 1883
protocol mqtt

```

which allows the broker to listen to websocket 9001 (as far as I understand)

The flow that I would like is as follows:  
Open a react based webpage via the public URL, edit/create a config JSON, publish this config JSON via MQTT, preferably just using the local broker.

---

<div class="post-metadata">

**Author:** ![mpous](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mpous/32/4742_2.png) [@mpous](https://forums.balena.io/u/mpous)\
**Post date:** [March 12, 2024, 12:05pm UTC](https://forums.balena.io/t/mqtt-public-url-and-react/370100/4 "2024-03-12T12:05:25Z")

</div>

@jamiear instead of using `localhost` did you try `mqtt` or the local ip address of the device?

---

<div class="post-metadata">

**Author:** ![jamiear](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@jamiear](https://forums.balena.io/u/jamiear)\
**Post date:** [March 12, 2024, 1:05pm UTC](https://forums.balena.io/t/mqtt-public-url-and-react/370100/5 "2024-03-12T13:05:41Z")

</div>

Yeah just tried both of those and I still get the same:

```auto
WebSocket connection to 'wss://f2d2fe3110830500d6d35e536afb4ecf.balena-devices.com:80/ws' failed: 

mqtt.esm.js:5 WebSocket connection to 'wss://192.168.128.48:9001/' failed: 

```

(for the case of device IP)

---

<div class="post-metadata">

**Author:** ![mpous](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mpous/32/4742_2.png) [@mpous](https://forums.balena.io/u/mpous)\
**Post date:** [March 12, 2024, 1:16pm UTC](https://forums.balena.io/t/mqtt-public-url-and-react/370100/6 "2024-03-12T13:16:55Z")

</div>

Hello @jamiear apologizes to do not mention that but the Public URL only give access to the port 80 or 8080!

You can read more here [Communicate outside the container - Balena Documentation](https://docs.balena.io/learn/develop/runtime/#public-device-urls)

---

<div class="post-metadata">

**Author:** ![jamiear](https://avatars.discourse-cdn.com/v4/letter/j/73ab20/32.png) [@jamiear](https://forums.balena.io/u/jamiear)\
**Post date:** [March 12, 2024, 1:19pm UTC](https://forums.balena.io/t/mqtt-public-url-and-react/370100/7 "2024-03-12T13:19:43Z")

</div>

Okay… are you saying that I should specify the websocket port as 8080? I am already opening the webpage on port 80 in this case and I can view it via the public URL link on the dashboard.  
The issue I am having is with communicating data from the webpage to the device

---

<div class="post-metadata">

**Author:** ![mpous](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/mpous/32/4742_2.png) [@mpous](https://forums.balena.io/u/mpous)\
**Post date:** [March 14, 2024, 9:54am UTC](https://forums.balena.io/t/mqtt-public-url-and-react/370100/8 "2024-03-14T09:54:04Z")

</div>

@jamiear if you want to publicly use the Public device URL for the websockets, yes you might need to use the port `8080`. Having said that, the Public device URL shouldn’t be a production solution.
