# Use Cotonic and MQTT for browser messaging

Source: https://zotonic.com/docs/2965/use-cotonic-and-mqtt-for-browser-messaging

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

Language: en

Use the existing Cotonic browser models and Zotonic MQTT bridge when components need to communicate through topics. First identify whether the topic stays inside the browser or crosses to the server.

Define a small payload and a clear reply contract. Subscribe before requesting data when the response can arrive immediately. Clean up component subscriptions when their lifetime ends, and handle reconnects without duplicating actions.

Server-side topic access must follow the site's permission rules. A topic name is not a secret or an access check.

With the greeting model from the model task active, try this read in the browser console:

```javascript
cotonic.ready.then(() =>
    cotonic.broker.call("bridge/origin/model/garden/get/greeting", {})
).then(reply => console.log(reply.payload));
```

Inspect the reply's status and result; expect the greeting on success. An error or disconnected bridge is not an empty greeting. The `bridge/origin/` prefix crosses to the site's server model, while a browser-local model such as `model/location` stays in the browser. Use <a href="/id/2736" class="doc-reference doc-reference-cotonic"><code>cotonic#location</code></a> for the latter's topic contract. Do not insert response text as HTML without escaping it.

License: 

Attribution: 

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