Skip to Content

Not able to connect to IoT MMS- MQTT websocket

May 17, 2017 at 04:24 AM


avatar image
Former Member

I've been trying to connect to the WSS MMQT endpoint created for the device i added in HCP IOT service .

Everything works fine when i test it in the MMS testing client - MQTT WebSocket API.

But when i try to do it through from Node.JS program using the MQTT module , it always shows that the client is offline.

Here is the code I'm using ,which i got from :

Please let me know what is going wrong . And if possible share a sample program for posting MQTT messages from SAPUI5 / or using node.JS.

Nodejs code :

// ========================================================================
va rmy_account_id='p1082370trial';
var my_device_id='418d17a4-ea20-4087-bc4d-f58335cf0af3';
var my_oauth_token='ebd38cbe3438faea2ab0721b8b54a';
// ========================================================================
var endpoint='wss://iotmms'+my_account_id+'';
va roptions= {
// ========================================================================
varclient=mqtt.connect(endpoint, options);
client.on('connect', function () {
vardata= {"mode":"async","messageType":"c04d602114e9df66a2b5","messages":[{"text":"this is sample text","count":"11","timestamp":1413191650}]};
client.publish('iot/data/'+my_device_id, JSON.stringify(data));
client.on('error', function () {
client.on('offline', function () {
client.on('close', function () {
client.on('message', function (topic, message) {
// message is Buffer

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Nadine Gaertner
Jun 06, 2017 at 03:55 PM

Finally found a working version!

I managed to successfully send messages via the IoT service after modifying the sendPacket method.

Also, in the options it is necessary to increment the protocol version and use MQTT as protocol ID:

protocolId: 'MQTT',

protocolVersion: 4,


10 |10000 characters needed characters left characters exceeded
Anton Levin
May 17, 2017 at 06:26 AM

Hello Shyam, just a question - are you able to run the simplistic example provided on mqtt npm package web site [1]? You may extend it with "error", "offline" and "close" handlers before trying. Regards, Anton


Show 3 Share
10 |10000 characters needed characters left characters exceeded

@Shyam, have you found a solution in the meantime?

@Anton: I'm having the same issue. I tried the example program which you linked "as is" and it works fine. When I adapt it to connect to the IoT service instead of the mosquitto test I get "offline" and "closed" events.

I have tried different protocols ("mqtt" and "mqtts" like in the original mosquitto example, "wss" like mentioned above and "https" like in the documentation) and also added port 443 to the URL, but none of these variants worked.

Someone else seems to have fixed the same issue by modifying file mqtt/lib/client.js. I tried the same but without success. Note that in the example they are working with a p12 certificate which I cannot use due to working in a trial instance. Also I have not succeeded adapting their code from p12 to bearer token.

I am kind of out of ideas.

My best guess would be that maybe the options for the mqtt connect or even the websocket options might need to be set differently in order to be properly mapped to the needs of the IoT service.

Do you have any more tips or ideas?

Thanks & cheers!


Hello Nadine, interestingly the default npmjs example (mosquitto connectivity) didn't work for me. I think it has something to do with the mqtt library (MQTT.js) itself. For example, Python example from the IoT StarterKit [1] works perfectly fine sending messages to the SAP Cloud Platform Internet of Things. It uses Paho MQTT library.

Regards, Anton



Hi Anton,

I could get the example with the mqtt library to run when I modified the sendPacket method.

Now I tried adjusting my JavaScript code to make use of the Paho mqtt, but had to learn fairly quickly that Paho is not working with node.js.

So back to square one and looking into mqtt and seeing if I can fix anything via parameter settings.

Could you do me the favour and check if you can get the example with the plain mqtt library to work when you adjust the sendPacket from the link I gave above?

Many thanks