Giter Club home page Giter Club logo

mqtt-websocket-java's Introduction

Announcement

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


MQTT over WebSocket library for Java

This library offers MQTT client functionality over WebSocket transport with Paho library and Jetty library.

Supported MQTT Version

  1. MQTT v3.1 (with Sub-Protocol: mqttv3.1)
  2. MQTT v3.1.1 (with Sub-Protocol: mqtt) ... DEFAULT

Supported Paho MQTT library version and Jetty WebSocket Client version

  1. Paho org.eclipse.paho.mqtt.java 1.0.2
  2. Jetty websocket-client 9.2.14.v20151106

Supported JDK/JRE Version

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.

HTTP Proxy Unsupported

Jetty9 WebSocket client doesn't support HTTP proxy. Therefore, this library won't work with HTTP proxy.

Dependencies

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

maven pom.xml settings

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>

How to install

Downloading the jar file

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).

Installing into your mvn local repository

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

How to use

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:

  1. ws://<host>:<port> ... for a plain WebSocket
  2. wss://<host>:<port> ... for a WebSocket with SSL/TLS
  3. tcp://<host>:<port> ... for a plain TCP MQTT socket
  4. ssl://<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) {
  	}
  });

How to build

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.

Source Code License

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.

Change History

1.0.1 : November 29, 2014

  • 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)

1.0.0 : July 30, 2014

  • Initial

mqtt-websocket-java's People

Contributors

dbaba avatar glopezdev avatar zerogvt avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.