eclipse-mosquitto / mosquitto

Eclipse Mosquitto - An open source MQTT broker
https://mosquitto.org
Other
9.08k stars 2.4k forks source link

localhost mqtt broker with ditto connection #2775

Open Gauravmeenu opened 1 year ago

Gauravmeenu commented 1 year ago

Actually, I'm using MQTT broker in localhost with the help of docker image, and along with I'm also running eclipse ditto with docker-compose up as given in the ditto deployment GitHub repo. whenever I'm trying to testconnection with mqtt tcp localhost protocol it gives me an error like as given below .

{ "status": 504, "error": "connectivity:connection.failed", "message": "The Connection with ID 'mqtt-connection-source44' failed to connect.", "description": "Could not establish a connection on 'localhost:1883'. Make sure the endpoint is reachable and that no firewall prevents the connection." }

But when I'm trying to connect with mosquitto public server test.mosquitto.org it works fine.

testconnection file given below

{ "targetActorSelection": "/system/sharding/connection", "headers": { "aggregate": false }, "piggybackCommand": { "type": "connectivity.commands:testConnection", "connection": { "id": "mqtt-connection-source44", "connectionType": "mqtt", "connectionStatus": "open", "failoverEnabled": true, "uri": "tcp://localhost:1883", "sources": [{ "addresses": ["my.sensors/#"], "authorizationContext": ["nginx:ditto"], "qos": 0, "filters": [] }], "mappingContext": { "mappingEngine": "JavaScript", "options": { "incomingScript": "function mapToDittoProtocolMsg(headers, textPayload, bytePayload, contentType) {const jsonString = String.fromCharCode.apply(null, new Uint8Array(bytePayload)); const jsonData = JSON.parse(jsonString); const thingId = jsonData.thingId.split(':'); const value = {measurements: {properties: {temperature: jsonData.temperature, humidity: jsonData.humidity}}}; return Ditto.buildDittoProtocolMsg(thingId[0], thingId[1], 'things', 'twin', 'commands', 'modify', '/features', headers, value);}", "outgoingScript": "function mapFromDittoProtocolMsg(namespace, id, group, channel, criterion, action, path, dittoHeaders, value, status, extra) {return null;}", "loadBytebufferJS": "false", "loadLongJS": "false" } } } } }

Daedaluz commented 1 year ago

could you provide mosquitto config?

Gauravmeenu commented 1 year ago

Yeah sure this is my mosquitto.conf file.

listener 1883 listener 9001 protocol websockets allow_anonymous true

Daedaluz commented 1 year ago

sorry, yea.. forgot to ask for mosquitto logs too.

Gauravmeenu commented 1 year ago

This is mosquitto logs.

1679896571: mosquitto version 2.0.15 starting 1679896571: Config loaded from /mosquitto/config/mosquitto.conf. 1679896571: Opening ipv4 listen socket on port 1883. 1679896571: Opening ipv6 listen socket on port 1883. 1679896571: Opening websockets listen socket on port 9001. 1679896571: mosquitto version 2.0.15 running

Daedaluz commented 1 year ago

ugh.. coffee hasn't kicked in. your config looks like it's configured to tcp://localhost:1883.

simply put; all containers has their own localhost (unless net is set to host). you need to have a testconnection config to reach the mosquitto container in some appropriate way.

localhost for where you have testconnection != localhost for mosquitto,

Gauravmeenu commented 1 year ago

Okay, so could you please help me to how I can my testconnection confiig to reach the mosquitto dokcer container .

Daedaluz commented 1 year ago

easiest would probably to just throw in the mosquitto service into the deployment file of ditto, then use tcp://mosquitto:1883 as url or similar.

Gauravmeenu commented 1 year ago

Sorry @Daedaluz I can't get you .

gunjan-it-engg commented 1 year ago

easiest would probably to just throw in the mosquitto service into the deployment file of ditto, then use tcp://mosquitto:1883 as url or similar.

Actually I also got the same issue , can you please elaborate more about this

Gauravmeenu commented 1 year ago

easiest would probably to just throw in the mosquitto service into the deployment file of ditto, then use tcp://mosquitto:1883 as url or similar.

I did this but it gives me a null response error.

Daedaluz commented 1 year ago

how does your docker-compose file look like now then?

Gauravmeenu commented 1 year ago

My docker-compose file looks like this:

version: '3'

services: mongodb: image: docker.io/mongo:4.4.18 mem_limit: 256m restart: always networks: default: aliases:

volumes: ditto_log_files: driver: local driver_opts: type: none device: /var/log/ditto o: bind,uid=1000,gid=1000

Daedaluz commented 1 year ago

what does "null" error response mean? does this mean it works? (like "no error")

or is the url supposed to be a websocket url, hence the websocket configuration?

gunjan-it-engg commented 1 year ago

no it's not work

Gauravmeenu commented 1 year ago

what does "null" error response mean? does this mean it works? (like "no error")

or is the url supposed to be a websocket url, hence the websocket configuration?

No it didn't work I got this kind of error

{ "type": "devops.responses:errorResponse", "status": 503, "serviceName": "connectivity", "instance": "1f46323d92f2", "payload": { "status": 503, "error": "ask.error", "message": "Ask timed out on [ActorSelection[Anchor(akka://ditto-cluster/), Path(/system/sharding/connection)]] after [10000 ms]. Message of type [org.eclipse.ditto.connectivity.model.signals.commands.modify.TestConnection]. A typical reason for AskTimeoutException is that the recipient actor didn't send a reply.", "description": "Please retry the performed action in order to improve resiliency." } }

Daedaluz commented 1 year ago

websocket url didn't work either?

Gauravmeenu commented 1 year ago

websocket url didn't work either?

I think connection is establish with tcp://mosquitto:1833, but I got error with mosquitto-client sub-pub

Unable to connect (Lookup error.).

gunjan-it-engg commented 1 year ago

is there any update regarding this @Daedaluz @Gauravmeenu @thjaeckle