---
title: "Close a session"
description: "Learn the ways that a WebSocket session closes, and what happens to the session in each case."
---

# Close a session

> Learn the ways that a WebSocket session closes, and what happens to the session in each case.

There are several ways a [WebSocket](https://docs.moveo.ai/api/websocket/connection.md) session can be closed, each with different
implications and behaviors. This document explains the different scenarios and their associated
events. For more information about sessions, refer to the [sessions lifecycle](https://docs.moveo.ai/api/websocket/life-cycle.md)
page.

## Types of session closure

### Conversation closure by agent

When a [human agent](/docs/chat/human-agents) or a
[resolve response](/docs/ai-agents/operations/resolve) closes the conversation, the following
sequence occurs:

1. The server sends a `conversation:closed` event to the client.
2. This event includes a `keep_alive` time window.
3. During this window, the client can send a `conversation:reopen` event.
4. If the client sends `conversation:reopen`, the server responds with:
   - `conversation:reopened` event
   - `session_id`
   - `status: 'open'`
   - `timestamp`

#### `conversation:closed` event

```json
{
  "keep_alive": 300, // 5 minutes
  "session_id": "123e4567-e89b-12d3-a456-426614174000",
  "status": "closed",
  "timestamp": 123123113,
  // Only if the conversation is closed by a human agent
  "agent_id": "123e4567-e89b-12d3-a456-426614174000",
  "agent_name": "John Agent"
}
```

#### `conversation:reopened` event

```json
{
  "session_id": "123e4567-e89b-12d3-a456-426614174000",
  "status": "open",
  "timestamp": 123123113
}
```

### Session closure

A session can be closed in the following ways:

1. **Automatic closure**: after the [`keep_alive`](/docs/guides/guides-timeouts#keep-alive) time window has
   passed following a `conversation:closed` event.
2. **Manual closure**: when the client sends a `session:close` event.
3. **Session expiration**: when the [`session_timeout`](/docs/guides/guides-timeouts#session-timeout) time
   window has passed.

When the client sends `session:close`, the server emits a `session:closed` event. When the
`keep_alive` or `session_timeout` window ends, the server emits a `session:expired` event. After
either event:

- The session is immediately expired.
- Any subsequent requests related to the session return a `session:expired` message.

#### `session:close` event

```json
{
  "session_id": "123e4567-e89b-12d3-a456-426614174000",
  "timestamp": 123123113
}
```

## Important notes

- `conversation:closed` and `session:closed` are distinct events with different implications.
- `conversation:closed` allows for potential reopening during the `keep_alive` window.
- `session:closed` is final and cannot be reopened.
- Once a session is closed, it is permanently expired and cannot be used for further communication.
