> For the complete documentation index, see [llms.txt](https://developer.aiodds.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://developer.aiodds.com/help-center/websocket/websocket-user-guide.md).

# WebSocket User Guide

## How to connect to WebSocket?

To connect to WebSocket, use the following Python sample code.

{% code expandable="true" %}

```python
import ssl
import paho.mqtt.client as mqtt
USER = "your user"
SECRET = "your secret"
topics = [
    "match/<user>",
    "odds/<category>/<user>",  # category: 1/2 (1:in-play. 2:pre-match)
]
# Connection callback
def on_connect(client, userdata, flags, rc):
    print("Connected:", rc)
    for topic in topics:
        client.subscribe(topic)
# Message callback
def on_message(client, userdata, msg):
    print(msg.topic, msg.payload)
# websocket protocol
def run():
    client_id = f"<user>/<Unique ID string>"
    client = mqtt.Client(client_id=client_id, transport='websockets')
    client.tls_set(cert_reqs=ssl.CERT_NONE)
    client.username_pw_set(username=USER, password=SECRET)
    client.ws_set_options(path='/')
    client.on_connect = on_connect
    client.on_message = on_message
    client.connect("mq.aiodds.com", 443, 10)
    client.loop_forever()
if __name__ == '__main__':
    run()
```

{% endcode %}

{% hint style="warning" %}
Although we have provided the sample code, you still need to provide the following information.
{% endhint %}

{% code expandable="true" %}

```python
// 1
USER = "your user"
SECRET = "your secret"

// 2
topics = [
    "match/<user>",
    "odds/<category>/<user>",  # category: 1/2 (1:in-play. 2:pre-match)
]

// 3
client_id = f"<user>/<Unique ID string>"
```

{% endcode %}

1. user and secret: This is unique for each customer.
2. topics: Use this field to subscribe to one or more topics in the same connection. The next section explains the available topics.
3. client\_id: Each connection should have one unique client\_id. It is defined by yourself. **The maximum number of client\_id is 20.**

## WebSocket Topic

Currently, we have provided 5 WebSockets: 'Bookmaker Odds', 'Settlement', 'Match', 'Match score' and 'Trading Odds'.

Here are topics for these 5 WebSockets:

* Bookmaker Odds:  odds/\<category>/\<user>
  * where \<category>: 1-in-play, 2-pre-match

* Settlement: settlement/\<user>

* Match: match/\<user>

* Match score: match\_score/\<user>

* Trading Odds: aiodds/\<category>/\<user>
  * where \<category>: 1-in-play, 2-pre-match

## What data will I get in each WebSocket?

In the WebSocket section, we have provided each WebSocket with its individual 'Return data structure' to display the data returned in each WebSocket.

You can also check the following links:

* [Bookmaker Odds](/bookmaker-odds/websocket/bookmaker-odds.md)
* [Settlement](/bookmaker-odds/websocket/settlement.md)
* [Match](/bookmaker-odds/websocket/match.md)
* [Match score](/bookmaker-odds/websocket/match-score.md)
* [AiOdds](/trading-odds/websocket/ai-trading.md)

## Important notes:

Each WebSocket connection can include multiple topics but must use one unique client\_id.

It means if you want to get both 'Match WebSocket' and 'Match score WebSocket', you have the following two options:

* Create one WebSocket connection and include both the Match topic and Match Score topic in the topic list and fill in one unique client\_id.

<figure><img src="/files/eZvSuNdNQsQddQvq1UHZ" alt=""><figcaption></figcaption></figure>

* Create two separate WebSocket connections, each with its own topic and unique client\_id.

<figure><img src="/files/6pjzbWaGLIq5pkAqhzOV" alt=""><figcaption></figcaption></figure>

{% hint style="danger" %}
**Please note that creating two different WebSocket connections with only one client\_id is FORBIDDEN. This will cause one of the connections to disconnect.**
{% endhint %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://developer.aiodds.com/help-center/websocket/websocket-user-guide.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
