> For the complete documentation index, see [llms.txt](https://irl-be.sabn.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://irl-be.sabn.xyz/socket-client-setup-and-authentication.md).

# Socket Client Setup & Authentication

### Authentication Mechanism

* For the socket connection to be authenticated, this cookie **MUST** be sent with the handshake request.

### Installation

```bash
npm install socket.io-client
# or
yarn add socket.io-client
```

### Initialization Code

To ensure cookies are sent, you must set `withCredentials: true`.

#### Basic Setup

```javascript
import { io } from "socket.io-client";

const SOCKET_URL = "https://api.yourdomain.com";

export const socket = io(SOCKET_URL, {
    transports: ["websocket"], // Force WebSocket for better performance
    withCredentials: true, // CRITICAL: Sends cookies (connect.sid) with request
    autoConnect: true, // Connect automatically
});

socket.on("connect", () => {
    console.log("Connected with ID:", socket.id);
});

socket.on("connect_error", err => {
    console.error("Connection failed:", err.message);
});
```
