> For the complete documentation index, see [llms.txt](https://dnotifier.gitbook.io/product-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://dnotifier.gitbook.io/product-docs/reference/connection-auth.md).

# Connection & auth

Every SDK session starts with `connect()`. Authentication is identical for WebSocket and HTTP transports; only the post-auth step differs.

## Flow overview

```
Client                          DNotifier API
  |                                   |
  |-- Authenticate (HTTPS) ---------->|
  |   appId, secret, userId           |
  |<-- token + plan limits -----------|
  |                                   |
  | [transport = http]                |
  |   connected → onConnected()       |
  |                                   |
  | [transport = ws]                  |
  |-- Open WebSocket (WSS) ---------->|
  |-- Handshake with token ---------->|
  |<-- Handshake acknowledged --------|
  |   connected → onConnected()       |
```

## Auth request

| Field    | Type     | Description                 |
| -------- | -------- | --------------------------- |
| `appId`  | `string` | Application id              |
| `secret` | `string` | Application secret          |
| `userId` | `string` | Connected user / service id |

Credentials are sent over HTTPS to the production API. Exact URLs are handled by the SDK.

## After successful auth

| Result             | Usage                                                                      |
| ------------------ | -------------------------------------------------------------------------- |
| Auth token         | Used for subsequent API and realtime calls (`authToken` after `connect()`) |
| Plan limits        | Available via `getPlanLimits()`                                            |
| AI enabled flag    | Sets `aiEnabled` / related plan fields                                     |
| Message size limit | Used for automatic chunking of large payloads                              |

## WebSocket handshake

After auth, WebSocket clients open a secure connection, complete a short handshake with the auth token, then fire `onConnected`. Until handshake completes, application messages are not delivered.

## HTTP transport

HTTP clients skip the WebSocket handshake. After auth succeeds:

* The client is marked connected and `onConnected()` fires
* Each API method (`sendAI`, `search`, etc.) uses authenticated HTTPS independently

## `getPlanLimits()`

Call after `connect()`. Throws if not connected.

| Field                   | Type      | Description                         |
| ----------------------- | --------- | ----------------------------------- |
| `messagesHardLimit`     | `number`  | Hard cap on messages                |
| `maxAIRequestsPerMonth` | `number`  | AI request quota (0 if AI disabled) |
| `maxAIWordsPerMonth`    | `number`  | AI word quota                       |
| `knowledgeBaseMaxWords` | `number`  | Knowledge-base word cap             |
| `aiEnabled`             | `boolean` | Whether AI/RAG is available         |
| `maxUsers`              | `number`  | Storage user cap                    |
| `maxRowsPerUser`        | `number`  | Rows per user cap                   |

## Sender ids

Pass plain `userId` / `senderId` / `receiverId` values in your application code. The SDK scopes them to your app when talking to the server.

## Error handling

| Condition                       | SDK behavior                            |
| ------------------------------- | --------------------------------------- |
| Auth rejected (bad credentials) | Throws with an authentication error     |
| WebSocket error                 | `onDisconnected`, `isConnected = false` |
| WebSocket close                 | `onDisconnected` with close details     |

## Disconnecting

| SDK            | Method                                                               |
| -------------- | -------------------------------------------------------------------- |
| Dart / Flutter | `notifier.disconnect()` — closes WebSocket                           |
| Python         | `await notifier.disconnect()`                                        |
| JavaScript     | Manage lifecycle in your app (close the underlying socket if needed) |

## See also

* [Endpoints](/product-docs/reference/endpoints.md)
* [Transport matrix](/product-docs/reference/transport-matrix.md)
* [First connection guide](/product-docs/getting-started/first-connection.md)
