# 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);
});
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://irl-be.sabn.xyz/socket-client-setup-and-authentication.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
