LivingDigital Demos
LivingDigital Demos

Reputation: 1

Paho clients disconnecting on Mosquitto Broker

I created a MQTT broker using Mosquitto library. I have created 1 Angular 5 Ionic 3 mobile app, which acts as paho clients. I am able to start the mqtt broker. When I run the mobile app on Android device. It tries to connect to broker and I get "Socket error on client , disconnecting." immediately.

I would like to know how can I resolve this issue. Also I would like to know how to create a Paho broker on mac

Musquitto broker: Following steps were used to install and run broker on mac OSX

/usr/bin/ruby -e "$(curl -fsSL
https://raw.githubusercontent.com/Homebrew
/install/master/install)"

  brew install mosquitto
  /usr/local/sbin/mosquitto -c 
 /usr/local/etc/mosquitto/mosquitto.conf

This is the response I got from terminal

1548310503: mosquitto version 1.5.1 starting
1548310503: Config loaded from /usr/local/etc/mosquitto/mosquitto.conf.
1548310503: Opening ipv6 listen socket on port 1883.
1548310503: Opening ipv4 listen socket on port 1883 

Now here is the angular 5 ionic 3 app code

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import {Paho} from 'ng2-mqtt/mqttws31';

@Component({
 selector: 'page-home',
 templateUrl: 'home.html'
})
export class HomePage {

 client;

constructor(public navCtrl: NavController) {

// Anikets LG Phone  Client ID: 123456 aniketPhone
// Pixel  Client ID qwerty12345  pixelPhone
this.client = new Paho.MQTT.Client('192.168.225.58', 1883, 
'aniketPhone');

this.onMessage();
this.onConnectionLost();
this.client.connect({onSuccess: this.onConnected.bind(this)});

}

 onConnected() {
console.log("Connected");
this.client.subscribe("pixelPhone");
this.sendMessage('HelloWorld');
 }

sendMessage(message: string) {
let packet = new Paho.MQTT.Message(message);
packet.destinationName = "pixelPhone";
this.client.send(packet);
}

onMessage() {
this.client.onMessageArrived = (message: Paho.MQTT.Message) => {
  console.log('Message arrived : ' + message.payloadString);
  alert(message.payloadString)
};
}

onConnectionLost() {
this.client.onConnectionLost = (responseObject: Object) => {
  console.log('Connection lost : ' + JSON.stringify(responseObject));
};
}
}

Upvotes: 0

Views: 1084

Answers (1)

hardillb
hardillb

Reputation: 59816

The Paho Javascript client only supports MQTT over Websockets.

Mosquitto defaults to only listening for native MQTT (on port 1883), if you want to use a Websocket client you need to add an extra listener.

Add the following to the mosquitto.conf file:

listener 8883
protocol websockets

You will need to change the port number in your angular code.

You also have a hard coded clientId in the code, this will only allow 1 client to be connected at a time as every client needs a unique clientId.

Upvotes: 1

Related Questions