> For the complete documentation index, see [llms.txt](https://obytejs.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://obytejs.com/client/connection-lifecycle.md).

# Connection lifecycle

`onConnect` fires on every successful WebSocket connection, including every reconnection when `reconnect: true` is enabled.

Hub-side subscriptions live only for the current connection, so register them inside `onConnect` when you use reconnecting clients.

### Example

```js
const client = new obyte.Client('wss://obyte.org/bb', {
  reconnect: true,
});

client.onConnect(function() {
  client.justsaying('light/new_aa_to_watch', {
    aa: 'AA_ADDRESS_TO_WATCH',
  });

  client.subscribe(function(err, result) {
    if (err) return console.error(err);
    console.log('notification:', result);
  });
});

client.onError(function(err) {
  console.error('connection error:', err);
});

const heartbeat = setInterval(function() {
  client.api.heartbeat();
}, 10 * 1000);
```

### Close permanently

When `reconnect: true` is enabled, a closed socket is treated as a dropped connection and the client reconnects. Disable reconnection before closing if you want to shut down permanently.

```js
client.client.reconnect = false;
client.close();
clearInterval(heartbeat);
```

### Returns

`subscribe` receives hub notifications.

```js
['justsaying', { subject: 'light/have_updates', body: { ... } }]
```
