eclipse-paho / eclipse-paho/paho.mqtt.javascript

client.send results in "Cannot read property 'length' of undefined" TypeError

Open
#162 3 comments 2 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
1.2k
Forks
466
PR merge metrics
No merged PRs in 30d

Description

Hi all,

In my onConnect cb I'm simply trying to send a "Hello" message on the topic I subscribed to just before I send it. However, for some reason it's giving me a TypeError: Cannot read property length of undefined, which point me to this section:

```
/**
* Called when the underlying websocket has received a complete packet.
* @ignore
*/
ClientImpl.prototype._on_socket_message = function (event) {
this._trace("Client._on_socket_message", event.data);
var messages = this._deframeMessages(event.data);
for (var i = 0; i < messages.length; i+=1) {
this._handleMessage(messages[i]);
}
};
```
So messages is undefined apparently.
I'm hoping somebody encountered this before or could point me in the right direction.
Or perhaps it's a bug, but seems unlikely since nobody's having this issue as of yet.

I'm using React 15.4.1 with WebPack and npm package paho-mqtt 1.0.4
I thought it might be related to below issue 150. However the proposed solution didn't help me:
https://github.com/eclipse/paho.mqtt.javascript/issues/150

What I'm doing is real straightforward:
```
this.client.connect(
{
userName: this.userName,
password: this.password,
useSSL: true,
onSuccess: this.onConnect.bind(this),
onFailure: this.showConnectionErrorMessage,
willMessage: this.lastWill
}
);
```
And my onConnect handling:
```
onConnect() {
console.log('onSucces');
this.client.subscribe(this.broadcastTopic);
this.client.onMessageArrived = this.onMessageArrived;

// I tried setting a timeout on my first attempt
// setTimeout( () => {
let message = new Paho.Message("Hello");
message.destinationName = this.broadcastTopic;

this.client.publish(message);
// I also tried the direct way of sending a message
// this.client.send(this.broadcastTopic, 'World')
// }, 3000)
},
onMessageArrived(message) {
console.log('onMessageArrived');
console.log(message);
},
```
Thanks all!

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.