Skip to content

Juerd/mqtt2web

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 

Repository files navigation

MQTT to Server Sent Events (SSE) aka EventSource aka text/event-stream gateway

mqtt2web lets HTTP clients subscribe to whitelisted MQTT topics. On the MQTT
side, it subscribes and unsubscribes automatically.

Please note: this implementation does not use or support WebSockets. Several
MQTT brokers support WebSockets natively. SSE makes pushing events to a browser
much easier.

This gateway operates at QoS 0.

Tested with mosquitto.


=== Subscribing ===

Javascript example:

    var s = new EventSource("http://.../mqtt?topic1=a,topic2=b);
    s.addEventListener("a", function (e) {
        var json = JSON.parse(e.data),
            topic  = json[0],
            value  = json[1],
            retain = json[2];

        ...
    });
    s.addEventListener("b", ...);

    var t = new EventSource("http://.../mqtt?revspace/%23=");
    t.addEventListener("message", ...);

If no event name is given after the "=" in a URL, the default event name
("message") will be implied, but the "=" itself needs to be included.

Note that # needs to be escaped in a URL: "revspace/#" becomes "revspace/%23".

In normal MQTT, the retain flag is boolean. This gateway adds support for "fake
retain", in which case the gateway will retain values regardless of the value
of the original retain flag. The retain value passed in the third value of the
JSON array will be:

    0 = no retain
    1 = real retain, possibly stale
    2 = fake retain, possibly stale
    3 = fake retain, *probably* stale

"Possibly stale" means it will be stale data if the connection to the MQTT
broker is lost, "probably stale" means it's old because we unsubscribed from
the topic automatically. To prevent MQTT unsubscription, add the topic to the
array @keep_subscribed or always keep at least one HTTP client connected that
subscribes to that topic.


=== Publishing ===

Just send a POST request over HTTP with typical text/x-www-form-urlencoded
key/value pairs, where the keys are the values to publish to. Here's a jQuery
example to illustrate the concept:

    $.post("http://.../mqtt", {
        "example/topic": "some value",
        "example": "multiple publishes can be done with one POST request"
    });

For publishing messages, this gateway does not (yet) support the retain flag.

About

MQTT to SSE aka EventSource aka text/event-stream

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages