# model/sessionId

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

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

Language: en



<a name="model.sessionId"></a>
The sessionId model manages a browser identifier stored as JSON under <tt>cotonic-sid</tt> in localStorage. It is shared by pages on the same origin and survives page reloads and browser restarts until it is reset, deleted, or the browser storage is cleared. It is not limited to a tab or to a sessionStorage session.

On initialization the model reuses a non-empty stored identifier or generates a new one. It also writes a <tt>cotonic-sid</tt> cookie through the [document model](/id/doc_cotonic_model_document#model.document), using <tt>SameSite=Strict</tt>. A newly generated identifier gets a four-day cookie; reusing an identifier on initialization refreshes the cookie for fourteen days. Cookie expiry does not remove the localStorage value.

The identifier has a UUID-like shape and is generated using <tt>Math.random()</tt>. It is not an authentication token or a cryptographically secure identifier.
<a name="model.sessionId.get"></a>
get
---

Call <tt>model/sessionId/get</tt> to retrieve the stored identifier as the response payload. If the localStorage value is missing or cannot be parsed as JSON, a new identifier is generated. The request must have a response topic; use <tt>cotonic.broker.call</tt> or a worker's <tt>self.call</tt>.

```
cotonic.broker.call("model/sessionId/get").then(function(msg) {
    console.log("Browser identifier:", msg.payload);
});
```
<a name="model.sessionId.post.reset"></a>
post/reset
----------

Generate and store a new identifier, update the cookie, and publish the new identifier on <tt>model/sessionId/event</tt>. When called with a response topic, return the new identifier as the response payload. The request payload is not used.

```
cotonic.broker.call("model/sessionId/post/reset").then(function(msg) {
    console.log("New browser identifier:", msg.payload);
});
```
<a name="model.sessionId.delete"></a>
delete
------

Remove the localStorage value, expire the cookie, and publish <tt>null</tt> on <tt>model/sessionId/event</tt>. If the request has a response topic, its response payload is also <tt>null</tt>. A later <tt>get</tt> call or model initialization generates a new identifier.

```
cotonic.broker.publish("model/sessionId/delete");
```
<a name="model.sessionId.event"></a>
event
-----

Identifier generation and reset publish the new string; deletion publishes <tt>null</tt>. These messages are not retained. Reusing an existing identifier on initialization does not publish this event; call <tt>get</tt> to read the current value.

Changes from other tabs are forwarded from <tt>model/localStorage/event/cotonic-sid</tt>. The current implementation forwards the complete storage message as the event payload, so the identifier for these forwarded events is in <tt>msg.payload.payload</tt>.

```
cotonic.broker.subscribe("model/sessionId/event", function(msg) {
    const value = msg.payload;
    const id = value !== null && typeof value === "object"
        ? value.payload
        : value;
    console.log("Browser identifier changed:", id);
});
```
<a name="model.sessionId.event.ping"></a>
event/ping
----------

After initialization the model publishes the retained payload <tt>"pong"</tt> on <tt>model/sessionId/event/ping</tt>. Subscribe to this topic to detect that the model is available.

```
cotonic.broker.subscribe("model/sessionId/event/ping", function(msg) {
    console.log("sessionId model is available:", msg.payload);
});
```

License: 

Attribution: 

Related resources: https://zotonic.com/api/agent/related/2743?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/2743?language=en>)
