# Server Connection Class

## Content

The [Connection ](/spreadjs/api/collaboration/js-collaboration/classes/Connection)class represents a bidirectional communication channel between server and client instances, enabling sending and receiving messages, broadcasting, and event management.
The server creates a `Connection` object upon client connection, which is used to manage communication with a specific client. The server accesses this object through Middleware and Hooks.

## Properties

**id**: The unique identifier of the connection (string).

```typescript
console.log(connection.id); // "XBtqzsRMPh7MaVvaAAAB"
```

**roomId**: The ID of the room to which the connection belongs (string).
**query**: The query parameters provided when the client connects (key-value pair object).
**auth**: The client's authentication data (key-value pair object).
**tags**: A Map for storing custom data.

```typescript
connection.tags.set('role', 'admin');
```

## Methods

### send

Use the `send` method to send messages to the server.

```typescript
send(data: MessageData, type?: MessageType): void;
```

**Parameters**

* `data`: The data to be sent.
* `type` (optional): The message type, used to identify the purpose or category of the message

### broadcast

Using the `broadcast()` method to broadcast messages to clients in the same room.

```typescript
broadcast(data: MessageData, type?: MessageType, includeSelf?: boolean): void;
```

**Parameters**

* `data`: The content of the message to be sent.
* `type`: The message type, defaults to an empty string.
* `includeSelf`(optional): If set to `true`, all clients in the room (including the current connection) will receive the message.

**Examples**

* Broadcast a message to all clients in the same room except the sender:

    ```typescript
    connection.broadcast('hello, client!');
    ```
* Broadcast a message to all clients in the same room, including the sender:

    ```typescript
    connection.broadcast('hello, client!', '', true);
    ```

### close

use the `close` method to close the connection with the client.

```typescript
connection.close();
```

This method will terminate the current connection with the client and clean up related resources.