Eclipse Paho Java client has WebSocket support. You can use Paho Java client for using MQTT over WebSocket.
Please refer to the project page at https://eclipse.org/paho/clients/java/
The project is EOL. Thank you for all our project users and contributors.
Daisuke Baba
This library offers MQTT client functionality over WebSocket transport with Paho library and Jetty library.
- MQTT v3.1 (with Sub-Protocol:
mqttv3.1
) - MQTT v3.1.1 (with Sub-Protocol:
mqtt
) ... DEFAULT
JDK/JRE 1.7+ is required as Jetty no longer supports JDK/JRE 1.6.
However, mqtt-websocket-jdk16-android
library works on JDK/JRE 1.6 environment.
Jetty9 WebSocket client doesn't support HTTP proxy. Therefore, this library won't work with HTTP proxy.
The following libraries are requried as well.
GroupId | ArtifactId | Version |
---|---|---|
org.eclipse.jetty | jetty-io | 9.2.14.v20151106 |
org.eclipse.jetty | jetty-util | 9.2.14.v20151106 |
org.eclipse.jetty.websocket | websocket-api | 9.2.14.v20151106 |
org.eclipse.jetty.websocket | websocket-common | 9.2.14.v20151106 |
Adds the following elements to your pom.xml if you're using maven.
<dependency>
<groupId>io.inventit.dev</groupId>
<artifactId>mqtt-websocket-java</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.websocket</groupId>
<artifactId>websocket-client</artifactId>
<version>9.2.14.v20151106</version>
</dependency>
<dependency>
<groupId>org.eclipse.paho</groupId>
<artifactId>org.eclipse.paho.client.mqttv3</artifactId>
<version>1.0.2</version>
</dependency>
curl -O -L https://github.com/inventit/mqtt-websocket-java/releases/download/1.0.1/mqtt-websocket-java-1.0.1.jar
Or you can create a jar file by yourself (see below).
mvn install:install-file -Dfile=mqtt-websocket-java-1.0.1.jar \
-DgroupId=io.inventit.dev -DartifactId=mqtt-websocket-java \
-Dversion=1.0.1 -Dpackaging=jar
You can use this library as the same manner as Paho's library but use MqttWebSocketAsyncClient
instead of Paho's classes such as MqttClient
and MqttAsyncClient
.
The MqttWebSocketAsyncClient
supports the following URI schimes:
ws://<host>:<port>
... for a plain WebSocketwss://<host>:<port>
... for a WebSocket with SSL/TLStcp://<host>:<port>
... for a plain TCP MQTT socketssl://<host>:<port>
... for a secure SSL/TLS MQTT socket
Here is sample code to use MqttWebSocketAsyncClient
.
// Plain MQTT
// final String uriString = "tcp://your-mqtt-broker:1883";
// MQTT over WebSocket
final String uriString = "wss://your-ws-broker/mqtt";
// Credentials
final String clientId = "your-client-id";
final String userName = "your-user-name";
final String password = "your-password";
final IMqttAsyncClient client = new MqttWebSocketAsyncClient(
uriString, clientId, new MemoryPersistence());
final MqttConnectOptions options = new MqttConnectOptions();
options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1_1);
options.setCleanSession(true);
options.setUserName(userName);
options.setPassword(password.toCharArray());
client.connect(options);
client.setCallback(new MqttCallback() {
@Override
public void messageArrived(String topic, MqttMessage message)
throws Exception {
}
@Override
public void deliveryComplete(IMqttDeliveryToken token) {
}
@Override
public void connectionLost(Throwable cause) {
}
});
Install maven then run the following command on the project root directory.
$ mvn clean package
Then you'll get mqtt-websocket-java-<version>.jar
under the target
directory.
All program source codes are available under the MIT style License.
Copyright (c) 2014 Inventit Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- Upgrades Jetty 9 library
- Adds a new factory method for WebSocketNetworkModule instance
- Adds new constructors with a new paramter for specifying the logger name to MqttWebSocketAsyncClient
- Releases a JDK1.6 class version (50) jar as well in order for Android app to include this library (Note that Jetty 9 itself doesn't support Android and JDK 1.6)
- Initial