# Socket.IO Integration Guide

**Server URL:** `https://market.saied.aait-d.com:4112`

---

## Connection

Connect to the server using the Socket.IO client with the server URL above. No special auth headers are required at the connection level (authentication is handled per-event via the payload).

---

## Rooms

### Join Order Room

Before listening to order-specific events (`message`, `typing`, `message-read`), the client must join the order room.

**Emit event:** `join-order-room`

**Payload:**
```json
{
  "orderId": "53"
}
```

> Once joined, the server will start routing order-specific events to this client.

---

## Emitted Events (Client → Server)

### 1. Typing Indicator

Notify others in the order room that the user is typing.

**Emit event:** `typing`

**Payload:**
```json
{
  "orderId": 53,
  "user_type": "client",
  "typing": true
}
```

| Field | Type | Values |
|-------|------|--------|
| `orderId` | integer | The order ID |
| `user_type` | string | `"client"` or `"driver"` |
| `typing` | boolean | `true` when typing, `false` when stopped |

---

### 2. Mark Message as Read

Mark a specific message as read within an order room.

**Emit event:** `message-read`

**Payload:**
```json
{
  "orderId": 53,
  "messageId": 10,
  "reader_type": "client"
}
```

| Field | Type | Values |
|-------|------|--------|
| `orderId` | integer | The order ID |
| `messageId` | integer | The message ID to mark as read |
| `reader_type` | string | `"client"` or `"driver"` |

---

## Listened Events (Server → Client)

These are the events the client should subscribe to after connecting.

| Event | Scope | Description |
|-------|-------|-------------|
| `message` | Order room | New message received in the order chat |
| `typing` | Order room | Another user's typing status changed |
| `message-read` | Order room | A message was marked as read |
| `notification:{userId}` | Global | Personal notification for the user (e.g. `notification:1`) |

> **Note:** `message`, `typing`, and `message-read` require the client to be in the order room first (see [Join Order Room](#join-order-room)).

---

## Notification Event Payload

The `notification:{userId}` event delivers real-time notifications. The payload structure:

```json
{
  "title": "Notification title",
  "body": "Notification body text",
  "notify_type": "order",
  "notify_id": 53,
  "icon": "https://..."
}
```

| Field | Type | Description |
|-------|------|-------------|
| `title` | string | Localized notification title |
| `body` | string | Localized notification body |
| `notify_type` | string | Type of notification (e.g. `order`, `management`, `test`) |
| `notify_id` | integer / null | Related entity ID (e.g. order ID) |
| `icon` | string / null | Icon URL |

---

## Connection Lifecycle (Example)

```
Client connects to wss://market.saied.aait-d.com:4112
  → emit "join-order-room" { orderId: "53" }
  → listen "message"         (new chat messages)
  → listen "typing"          (typing indicators)
  → listen "message-read"    (read receipts)
  → listen "notification:1"  (personal notifications)

User types:
  → emit "typing" { orderId: 53, user_type: "client", typing: true }

User reads a message:
  → emit "message-read" { orderId: 53, messageId: 10, reader_type: "client" }
```

---

## Notes

- The `assigned-order:{orderId}` channel is also used internally (visible in Socket.IO logs). Listen to it if you need to track order assignment updates.
- All event names are **lowercase with hyphens** (e.g. `join-order-room`, `message-read`).
- `user_type` and `reader_type` accept only `"client"` or `"driver"`.
