# model/sessionStorage

Source: https://zotonic.com/cotonic/models/sessionstorage

URI: https://zotonic.com/id/2742

Language: en



<a name="model.sessionStorage"></a>
The sessionStorage model provides access to the <tt>sessionStorage</tt> of the browser. It makes it possible to set and retrieve values via mqtt topics. For more information about the session storage see:
<a name="model.sessionStorage.get.key"></a>
get/+key
--------

Get the element stored as <tt>key</tt> from the sessionStorage. Returns the contents as payload, or <tt>null</tt> 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.
           
```
<a name="model.sessionStorage.get.key.subkey"></a>
get/+key/+subkey
----------------

Get a sub element stored as <tt>key.subkey</tt> 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.
```
<a name="model.sessionStorage.post.key"></a>
post/+key
---------

Store a message under <tt>key</tt> in the sessionStorage.

```
self.publish("model/sessionStorage/post/item-1", "Cucumbers are sometimes green");
=> New value stored.
```
<a name="model.sessionStorage.post.key.subkey"></a>
post/+key/+subkey
-----------------

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

```
self.publish("model/sessionStorage/post/item-2/a", "hello");
```
<a name="model.sessionStorage.delete.key"></a>
delete/+key
-----------

Delete an element stored under <tt>key</tt> from the sessionStorage.

```
self.publish("model/sessionStorage/delete/item-2");
```
<a name="model.sessionStorage.delete.key.subkey"></a>
delete/+key/+subkey
-------------------

Delete an element stored under <tt>key.subkey</tt> from the sessionStorage.

```
self.publish("model/sessionStorage/delete/item-2");
```
<a name="model.sessionStorage.event.key"></a>
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
```
<a name="model.sessionStorage.event.ping"></a>
event/ping
----------

When the sessionStorage model is enabled a retained message is published on the <tt>model/sessionStorage/event/ping</tt> 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
```
<!-- sessionId model -->

License: 

Attribution: 

Related resources: https://zotonic.com/api/agent/related/2742?language=en


## API access

- [API help](<https://zotonic.com/api-help>)
- [OpenAPI specification](<https://zotonic.com/openapi.json>)
- [This resource as JSON](<https://zotonic.com/api/agent/resource/2742?language=en>)
