• Stars
    star
    2,381
  • Rank 19,310 (Top 0.4 %)
  • Language
    Go
  • License
    Other
  • Created almost 9 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PkgGoDev Go Report Card

Eclipse Paho MQTT Go client

This repository contains the source code for the Eclipse Paho MQTT 3.1/3.11 Go client library.

This code builds a library which enable applications to connect to an MQTT broker to publish messages, and to subscribe to topics and receive published messages.

This library supports a fully asynchronous mode of operation.

A client supporting MQTT V5 is also available.

Installation and Build

The process depends upon whether you are using modules (recommended) or GOPATH.

Modules

If you are using modules then import "github.com/eclipse/paho.mqtt.golang" and start using it. The necessary packages will be download automatically when you run go build.

Note that the latest release will be downloaded and changes may have been made since the release. If you have encountered an issue, or wish to try the latest code for another reason, then run go get github.com/eclipse/paho.mqtt.golang@master to get the latest commit.

GOPATH

Installation is as easy as:

go get github.com/eclipse/paho.mqtt.golang

The client depends on Google's proxy package and the websockets package, also easily installed with the commands:

go get github.com/gorilla/websocket
go get golang.org/x/net/proxy

Usage and API

Detailed API documentation is available by using to godoc tool, or can be browsed online using the pkg.go.dev service.

Samples are available in the cmd directory for reference.

Note:

The library also supports using MQTT over websockets by using the ws:// (unsecure) or wss:// (secure) prefix in the URI. If the client is running behind a corporate http/https proxy then the following environment variables HTTP_PROXY, HTTPS_PROXY and NO_PROXY are taken into account when establishing the connection.

Troubleshooting

If you are new to MQTT and your application is not working as expected reviewing the MQTT specification, which this library implements, is a good first step. MQTT.org has some good resources that answer many common questions.

Error Handling

The asynchronous nature of this library makes it easy to forget to check for errors. Consider using a go routine to log these:

t := client.Publish("topic", qos, retained, msg)
go func() {
    _ = t.Wait() // Can also use '<-t.Done()' in releases > 1.2.0
    if t.Error() != nil {
        log.Error(t.Error()) // Use your preferred logging technique (or just fmt.Printf)
    }
}()

Logging

If you are encountering issues then enabling logging, both within this library and on your broker, is a good way to begin troubleshooting. This library can produce various levels of log by assigning the logging endpoints, ERROR, CRITICAL, WARN and DEBUG. For example:

func main() {
	mqtt.ERROR = log.New(os.Stdout, "[ERROR] ", 0)
	mqtt.CRITICAL = log.New(os.Stdout, "[CRIT] ", 0)
	mqtt.WARN = log.New(os.Stdout, "[WARN]  ", 0)
	mqtt.DEBUG = log.New(os.Stdout, "[DEBUG] ", 0)

	// Connect, Subscribe, Publish etc..
}

Common Problems

  • Seemingly random disconnections may be caused by another client connecting to the broker with the same client identifier; this is as per the spec.
  • Unless ordered delivery of messages is essential (and you have configured your broker to support this e.g. max_inflight_messages=1 in mosquitto) then set ClientOptions.SetOrderMatters(false). Doing so will avoid the below issue (deadlocks due to blocking message handlers).
  • A MessageHandler (called when a new message is received) must not block (unless ClientOptions.SetOrderMatters(false) set). If you wish to perform a long-running task, or publish a message, then please use a go routine (blocking in the handler is a common cause of unexpected pingresp not received, disconnecting errors).
  • When QOS1+ subscriptions have been created previously and you connect with CleanSession set to false it is possible that the broker will deliver retained messages before Subscribe can be called. To process these messages either configure a handler with AddRoute or set a DefaultPublishHandler. If there is no handler (or DefaultPublishHandler) then inbound messages will not be acknowledged. Adding a handler (even if it's opts.SetDefaultPublishHandler(func(mqtt.Client, mqtt.Message) {})) is highly recommended to avoid inadvertently hitting inflight message limits.
  • Loss of network connectivity may not be detected immediately. If this is an issue then consider setting ClientOptions.KeepAlive (sends regular messages to check the link is active).
  • Reusing a Client is not completely safe. After calling Disconnect please create a new Client (NewClient()) rather than attempting to reuse the existing one (note that features such as SetAutoReconnect mean this is rarely necessary).
  • Brokers offer many configuration options; some settings may lead to unexpected results.
  • Publish tokens will complete if the connection is lost and re-established using the default options.SetAutoReconnect(true) functionality (token.Error() will return nil). Attempts will be made to re-deliver the message but there is currently no easy way know when such messages are delivered.

If using Mosquitto then there are a range of fairly common issues:

  • listener - By default Mosquitto v2+ listens on loopback interfaces only (meaning it will only accept connections made from the computer its running on).
  • max_inflight_messages - Unless this is set to 1 mosquitto does not guarantee ordered delivery of messages.
  • max_queued_messages / max_queued_bytes - These impose limits on the number/size of queued messages. The defaults may lead to messages being silently dropped.
  • persistence - Defaults to false (messages will not survive a broker restart)
  • max_keepalive - defaults to 65535 and, from version 2.0.12, SetKeepAlive(0) will result in a rejected connection by default.

Reporting bugs

Please report bugs by raising issues for this project in github https://github.com/eclipse/paho.mqtt.golang/issues

A limited number of contributors monitor the issues section so if you have a general question please see the resources in the more information section for help.

We welcome bug reports, but it is important they are actionable. A significant percentage of issues reported are not resolved due to a lack of information. If we cannot replicate the problem then it is unlikely we will be able to fix it. The information required will vary from issue to issue but almost all bug reports would be expected to include:

  • Which version of the package you are using (tag or commit - this should be in your go.mod file)
  • A full, clear, description of the problem (detail what you are expecting vs what actually happens).
  • Configuration information (code showing how you connect, please include all references to ClientOption)
  • Broker details (name and version).

If at all possible please also include:

  • Details of your attempts to resolve the issue (what have you tried, what worked, what did not).
  • A Minimal, Reproducible Example. Providing an example is the best way to demonstrate the issue you are facing; it is important this includes all relevant information (including broker configuration). Docker (see cmd/docker) makes it relatively simple to provide a working end-to-end example.
  • Broker logs covering the period the issue occurred.
  • Application Logs covering the period the issue occurred. Unless you have isolated the root cause of the issue please include a link to a full log (including data from well before the problem arose).

It is important to remember that this library does not stand alone; it communicates with a broker and any issues you are seeing may be due to:

  • Bugs in your code.
  • Bugs in this library.
  • The broker configuration.
  • Bugs in the broker.
  • Issues with whatever you are communicating with.

When submitting an issue, please ensure that you provide sufficient details to enable us to eliminate causes outside of this library.

Contributing

We welcome pull requests but before your contribution can be accepted by the project, you need to create and electronically sign the Eclipse Contributor Agreement (ECA) and sign off on the Eclipse Foundation Certificate of Origin.

More information is available in the Eclipse Development Resources; please take special note of the requirement that the commit record contain a "Signed-off-by" entry.

More information

Stack Overflow has a range questions/answers covering a range of common issues (both relating to use of this library and MQTT in general). This is the best place to ask general questions (including those relating to the use of this library).

Discussion of the Paho clients takes place on the Eclipse paho-dev mailing list.

General questions about the MQTT protocol are discussed in the MQTT Google Group.

There is much more information available via the MQTT community site.

More Repositories

1

mosquitto

Eclipse Mosquitto - An open source MQTT broker
C
7,649
star
2

che

Kubernetes based Cloud Development Environments for Enterprise Teams
TypeScript
6,868
star
3

jetty.project

Eclipse Jettyยฎ - Web Container & Clients - supports HTTP/2, HTTP/1.1, HTTP/1.0, websocket, servlets, and more
Java
3,655
star
4

paho.mqtt.android

MQTT Android
Java
2,708
star
5

eclipse-collections

Eclipse Collections is a collections framework for Java with optimized data structures and a rich, functional and fluent API.
Java
2,283
star
6

paho.mqtt.java

Eclipse Paho Java MQTT client library. Paho is an Eclipse IoT project.
Java
2,095
star
7

paho.mqtt.python

paho.mqtt.python
Python
1,946
star
8

sumo

Eclipse SUMO is an open source, highly portable, microscopic and continuous traffic simulation package designed to handle large networks. It allows for intermodal simulation including pedestrians and comes with a large set of tools for scenario creation.
1,902
star
9

paho.mqtt.c

An Eclipse Paho C client library for MQTT for Windows, Linux and MacOS. API documentation: https://eclipse.github.io/paho.mqtt.c/
C
1,736
star
10

eclipse.jdt.ls

Java language server
Java
1,410
star
11

mraa

Linux Library for low speed IO Communication in C with bindings for C++, Python, Node.js & Java. Supports generic io platforms, as well as Intel Edison, Intel Joule, Raspberry Pi and many more.
C
1,349
star
12

paho.mqtt.embedded-c

Paho MQTT C client library for embedded systems. Paho is an Eclipse IoT project (https://iot.eclipse.org/)
C
1,307
star
13

openvsx

An open-source registry for VS Code extensions
Java
1,181
star
14

paho.mqtt.javascript

paho.mqtt.javascript
JavaScript
1,145
star
15

paho.mqtt.cpp

C++
976
star
16

milo

Eclipse Miloโ„ข - an open source implementation of OPC UA (IEC 62541).
Java
976
star
17

omr

Eclipse OMRโ„ข Cross platform components for building reliable, high performance language runtimes
C++
917
star
18

xtext

Eclipse Xtextโ„ข is a language development framework
Java
715
star
19

upm

UPM is a high level repository that provides software drivers for a wide variety of commonly used sensors and actuators. These software drivers interact with the underlying hardware platform through calls to MRAA APIs.
C++
651
star
20

microprofile

Repository for important documentation - the index to the project / community
Java
635
star
21

californium

CoAP/DTLS Java Implementation
Java
620
star
22

leshan

Java Library for LWM2M
Java
614
star
23

paho.mqtt-spy

mqtt-spy is an open source desktop & command line utility intended to help you with monitoring activity on MQTT topics
Java
605
star
24

steady

Analyses your Java applications for open-source dependencies with known vulnerabilities, using both static analysis and testing to determine code context and usage for greater accuracy. https://eclipse.github.io/steady/
Java
518
star
25

sprotty

A diagramming framework for the web
TypeScript
514
star
26

paho.mqtt.m2mqtt

C#
513
star
27

jifa

๐Ÿ”ฌ Online Heap Dump, GC Log, Thread Dump & JFR File Analyzer.
Java
509
star
28

buildship

The Eclipse Plug-ins for Gradle project.
Java
507
star
29

lsp4j

A Java implementation of the language server protocol intended to be consumed by tools and language servers implemented in Java.
Java
473
star
30

kura

Eclipse Kuraโ„ข project
Java
469
star
31

wakaama

Eclipse Wakaama is a C implementation of the Open Mobile Alliance's LightWeight M2M protocol (LWM2M).
C
465
star
32

streamsheets

An open-source tool for processing stream data using a spreadsheet-like interface.
JavaScript
449
star
33

paho.mqtt.rust

paho.mqtt.rust
Rust
422
star
34

hawkbit

Eclipse hawkBitโ„ข
Java
416
star
35

eclipse-collections-kata

Eclipse Collections Katas
Java
411
star
36

hono

Eclipse Honoโ„ข Project
Java
378
star
37

repairnator

Software development bots for Github. Join the bot revolution! ๐ŸŒŸ๐Ÿค–๐ŸŒŸ๐Ÿ’ž
Java
370
star
38

ponte

Ponte Project
JavaScript
360
star
39

birt

Eclipse BIRTโ„ข The open source reporting and data visualization project.
Java
355
star
40

paho.golang

Go libraries
Go
324
star
41

rdf4j

Eclipse RDF4J: scalable RDF for Java
Java
323
star
42

paho.mqtt-sn.embedded-c

Paho C MQTT-SN gateway and libraries for embedded systems. Paho is an Eclipse IoT project.
C++
313
star
43

lemminx

XML Language Server
Java
255
star
44

vorto

Vorto Project
Java
221
star
45

tahu

Eclipse Tahu addresses the existence of legacy SCADA/DCS/ICS protocols and infrastructures and provides a much-needed definition of how best to apply MQTT into these existing industrial operational environments.
Java
220
star
46

kapua

Java
218
star
47

elk

Eclipse Layout Kernel - Automatic layout for Java applications.
Java
211
star
48

jnosql

Eclipse JNoSQL is a framework which has the goal to help Java developers to create Jakarta EE applications with NoSQL.
Java
210
star
49

corrosion

Eclipse Corrosion - Rust edition in Eclipse IDE
Java
199
star
50

capella

Open Source Solution for Model-Based Systems Engineering
Java
197
star
51

dirigible

Eclipse Dirigibleโ„ข Project
JavaScript
196
star
52

microprofile-config

MicroProfile Configuration Feature
Java
182
star
53

wildwebdeveloper

Simple and productive Web Development Tools in the Eclipse IDE
Java
181
star
54

pdt

PHP Development Tools project (PDT)
PHP
178
star
55

org.aspectj

Java
172
star
56

xacc

XACC - eXtreme-scale Accelerator programming framework
C++
137
star
57

thingweb.node-wot

thingweb.node-wot
TypeScript
130
star
58

microprofile-rest-client

MicroProfile Rest Client
Java
124
star
59

tycho

Tycho project repository (tycho)
Java
119
star
60

microprofile-conference

Microprofile.io Demo Code - Web Services Conference Application
Java
117
star
61

microprofile-fault-tolerance

microprofile fault tolerance
Java
115
star
62

microprofile-samples

Micro Profile Samples
Java
115
star
63

xtext-core

xtext-core
Java
114
star
64

microprofile-open-api

Microprofile open api
Java
112
star
65

jbom

Java
111
star
66

gef

Eclipse GEFโ„ข
Java
111
star
67

transformer

Eclipse Transformer provides tools and runtime components that transform Java binaries, such as individual class files and complete JARs and WARs, mapping changes to Java packages, type names, and related resource names.
Java
108
star
68

paho.mqtt.testing

An Eclipse Paho project - a Python broker for testing
Python
104
star
69

tinydtls

Eclipse tinydtls
C
102
star
70

xtext-xtend

xtext-xtend
Java
100
star
71

microprofile-graphql

microprofile-graphql
Java
98
star
72

microprofile-lra

microprofile-lra
Java
97
star
73

microprofile-health

microprofile-health
Java
95
star
74

microprofile-metrics

microprofile-metrics
Java
94
star
75

kuksa.val

kuksa.val
C++
93
star
76

sw360

SW360 project
Java
92
star
77

microprofile-jwt-auth

Java
92
star
78

mosaic

Eclipse MOSAIC is a Multi-Domain and Multi-Scale Simulation Framework for Automated and Connected Mobility Scenarios.
Java
88
star
79

nebula

Nebula Project
Java
84
star
80

microprofile-reactive-streams-operators

Microprofile project
Java
79
star
81

mosquitto.rsmb

Mosquitto rsmb
C
75
star
82

microprofile-starter

MicroProfile project generator source code
Java
69
star
83

aCute

Eclipse aCute - C# edition in Eclipse IDE
Java
65
star
84

ditto-examples

Eclipse Dittoโ„ข: Digital Twin framework - Examples
Java
63
star
85

epsilon

Epsilon is a family of Java-based scripting languages for automating common model-based software engineering tasks, such as code generation, model-to-model transformation and model validation, that work out of the box with EMF (including Xtext and Sirius), UML (including Cameo/MagicDraw), Simulink, XML and other types of models.
Java
61
star
86

lsp4e

Language Server Protocol support in Eclipse IDE
Java
60
star
87

tm4e

TextMate support in Eclipse IDE
Java
60
star
88

californium.tools

Californium project
Java
59
star
89

microprofile-reactive-messaging

Java
59
star
90

microprofile-opentracing

microprofile-opentracing
Java
57
star
91

eclemma

๐ŸŒ˜ใ€€Java Code Coverage for Eclipse IDE
Java
57
star
92

texlipse

Eclipse Texlipse
Java
56
star
93

mita

mita
Xtend
55
star
94

dartboard

Dart Plugin for Eclipse
Java
55
star
95

jnosql-databases

This project contains Eclipse JNoSQL databases
Java
54
star
96

windowbuilder

Eclipse Windowbuilder
Java
54
star
97

xtext-eclipse

xtext-eclipse
Java
49
star
98

adore

Eclipse ADORe is a ROS based modular software library and toolkit for decision making, planning, control and simulation of automated vehicles supporting CARLA and SUMO.
Makefile
48
star
99

packages

IoT Packages project
Smarty
46
star
100

amlen

Message Broker for IoT/Mobile/Web. Mainly uses MQTT v3.x and v5. Aims to be easy to use, scalable and reliable
C
46
star