0

I have a javascript with mqtt and I wrote a paho python for subscription but I am not getting any result. I want to print received message

var mqtt = require('mqtt');
var client  = mqtt.connect('mqtt://127.0.0.1',{
  username: process.env.TOKEN
});

client.on('connect', function () {
  console.log('connected');
  client.subscribe('v1/devices/me/rpc/request/+')
});


client.on('message', function (topic, message) {
  console.log('request.topic: ' + topic);
  console.log('request.body: ' + message.toString());
  var requestId = topic.slice('v1/devices/me/rpc/request/'.length);
  //client acts as an echo service
  client.publish('v1/devices/me/rpc/response/' + requestId, message);

});

`

Corresponding python for sub not publish But I am not getting any result

def on_connect(client, userdata, flags, rc):
  print("Connected with result code "+str(rc))
  client.subscribe('v1/devices/me/rpc/request/+')

def on_message(client, userdata, msg):

  print( msg.payload.decode())
  client.disconnect()

  client = mqtt.Client()
  client.username_pw_set("5vTGKxGAHzv5TbsW2Jv1")
  client.connect("127.0.0.1",1882,60)
  client.on_connect = on_connect
  client.on_message = on_message
  client.loop_forever()

Thank in advance

hardillb
  • 54,545
  • 11
  • 67
  • 105
Arun Pt
  • 1
  • 2

1 Answers1

0

The NodeJS app is connecting to 1883 (default port) the Python is connecting to 1882.

Both sets of code subscribe to the same topic (v1/devices/me/rpc/request/+) so the Python code will never see the response.

The indenting on the python code is wrong so the connect and loop forever is part of the on_message callback.

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • my message broker is running on mosquitto -p 1882 . That is the reason I am mentioning on python port number 1882 – Arun Pt Mar 07 '18 at 14:18
  • Yes, but you haven't set that port number in the Javascript version so it won't connect. – hardillb Mar 07 '18 at 14:20
  • I am running in my terminal mosquitto -p 1882 . But I can see the java script published message.I am what ever I am sending from my server that is received by JS and replay back .I am also changed python to 1883 but I am unable to see the message content I am confused about username: process.env.TOKEN , TOKEN is 5vTGKxGAHzv5TbsW2Jv1. I converted that in python client.username_pw_set("5vTGKxGAHzv5TbsW2Jv1") is it correct – Arun Pt Mar 07 '18 at 14:37
  • my intention is to some how to display the subscribed message either to a file or std out. I am not familiar with JS. If I can able to see my subscribed message in std out in Js that is also fine – Arun Pt Mar 07 '18 at 14:41
  • can able to use console.info('my message is %s', message); for printing data to std out – Arun Pt Mar 07 '18 at 15:33
  • how can I mention to connect to port number 1882 in NodeJS in the above program – Arun Pt Mar 07 '18 at 16:18
  • mqtt://127.0.0.1:1882 – hardillb Mar 07 '18 at 16:19
  • Now it is connected to my message broker. One more help is needed I want to see the message in std out is it possible in Node JS – Arun Pt Mar 07 '18 at 16:30