model/sessionStorage Cotonic

model.sessionStorage

The sessionStorage model provides access to the sessionStorage of the browser. It makes it possible to set and retrieve values via mqtt topics. For more information about the session storage see:

get/+key

Get the element stored as key from the sessionStorage. Returns the contents as payload, or null if it is not found.

self.call("model/sessionStorage/get/item-1")
.then(function(m) {
    console.log("item-1", m.payload);
});
=> Logs the item-1 on the console, or null otherwise.
           

get/+key/+subkey

Get a sub element stored as key.subkey from the sessionStorage.

self.call("model/sessionStorage/get/item-2/a")
.then(function(m) {
    console.log("item-2", m.payload);
});
=> Logs the item-2 on the console, or null otherwise.

post/+key Store a message under key in the sessionStorage.
self.publish("model/sessionStorage/post/item-1", "Cucumbers are sometimes green");
=> New value stored.

post/+key/+subkey

Store a message under key.subkey in the sessionStorage. When no item is stored under key a new object is created, and subkey is added as sub-element. When key exists it must be an object, then subkey is added or overwritten.

self.publish("model/sessionStorage/post/item-2/a", "hello");

delete/+key

Delete an element stored under key from the sessionStorage.

self.publish("model/sessionStorage/delete/item-2");

delete/+key/+subkey

Delete an element stored under key.subkey from the sessionStorage.

self.publish("model/sessionStorage/delete/item-2");

event/+key

Subscribe to sessionStorage updates and deletes. When entry is updated you will get a notification.

self.subscribe("model/sessionStorage/subscribe/+key",
    function(m, a) {
        console.log(m, a);
    });
self.publish("model/sessionStorage/post/a", "hello");
self.publish("model/sessionStorage/post/b", "world");
=> Logs the update in the console

event/ping

When the sessionStorage model is enabled a retained message is published on the model/sessionStorage/event/ping topic.

let storageReady = false;
cotonic.broker.subscribe("model/sessionStorage/event/+key",
    function(msg, args) {
        switch(args.key)
        case "pong":
            storageReady = true;
            break;
        ...
    });
=> When the storage is ready a pong message will be available

Edit on GitHub