How do I connect broker mosquitto to react app?

  broker, docker, mosquitto, mqtt, reactjs

I have an app made with react and I put it up in a docker container. I am also using docker’s eclipse-mosquitto image. I need to establish communication between the 2 containers, but I can’t do it. This is my docker-compose.yml

     version: "3.5"

services:
   app:
     build:
       context: ./
       dockerfile: Dockerfile
     image: node: 16-alpine3.11
     container_name: web
     ports:
       - 3000: 3000
     working_dir: / app
     volumes:
       - ./:/app

   mosquitto:
     image: eclipse-mosquitto
     container_name: mosquitto
     ports:
       - "0.0.0.0:1883:1883"
       - "0.0.0.0:9001:9001"
     volumes:
       - ./mosquitto/mosquitto.conf:/mosquitto/config/mosquitto.conf
       - ./mosquitto/data:/mosquitto/data
       - ./mosquitto/log:/mosquitto/log

What I’m doing is opening 2 terminals, in one I enter the container of my app and in the other I enter the container of mosquitto and when I try to publish something from mosquitto to the app I can’t do it.

To subscribe I write:

 mosquitto_sub -d -h localhost -p 1883 -t 'test'

To publish I write:

 mosquitto_pub -d -h localhost -p 1883 -t 'test' -m 'hello'

I get the following error in the terminal:

bash mosquitto_sub: not found or bash mosquitto_pub: not found 

note: The error in the terminal only appears to me when I try to subscribe or publish from the container called: web, if I do it from the mosquitto container it works fine but it is not what I want. The idea is to subscribe from the web container and publish from the mosquitto container.

Source: Docker Questions

LEAVE A COMMENT