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

# Websocket User Guide

### How to apply websocket? <a href="#h2-0" id="h2-0"></a>

To integrate with WebSocket, refer to the following sample code in the Overview section.&#x20;

```py
# python example

import json

import paho.mqtt.client as mqtt

# Football Topic
football_topic = ''
# Basketball Topic
basketball_topic = ''
# Tennis Topic
tennis_topic = ''

# username
username = ''
# password
password = ''


# Connection callback
def on_connect(c, userdata, flags, rc):
    # The value of rc indicates success or not:
    # 0: Connection successful
    # 1: Connection refused - incorrect protocol version
    # 2: Connection refused - invalid client identifier
    # 3: Connection refused - server unavailable
    # 4: Connection refused - bad username or password
    # 5: Connection refused - not authorised
    # 6-255: Currently unused.

    # connection succeeded
    if rc == 0:
        # Subscribe to related topics
        c.subscribe(football_topic)
        c.subscribe(basketball_topic)
        c.subscribe(tennis_topic)
    elif rc in [4, 5]:
        print('If the verification fails, please confirm whether the user name, key, and authorized ip are correct, otherwise the authentication will fail')


# Message callback
def on_message(c, userdata, msg):
    # Message processing logic, please refer to the document for specific format
    print(msg.topic)
    print(json.loads(msg.payload))


if __name__ == '__main__':
    # websocket protocol
    client = mqtt.Client(transport='websockets')
    client.tls_set()
    client.username_pw_set(username=username, password=password)
    client.on_connect = on_connect
    client.on_message = on_message

    client.connect("mq.thesports.com", 443)
    client.loop_forever()
```

TheSports currently provides sample code in Python only. If you use another programming language, you will need to implement the WebSocket connection yourself.

### What else information do I need? <a href="#h2-1" id="h2-1"></a>

You need to fill in with **topic**, **username** and **password** so that you can get the results.

{% stepper %}
{% step %}

#### **Sports Topic**

For the topic, you can **ask your business manager** or **our technical support** to provide.

Don't write it by yourself.
{% endstep %}

{% step %}

#### **Account**

For the username and password, they are the same as your "user" and "secret" used to request API endpoints.
{% endstep %}
{% endstepper %}

### What fields will I get? <a href="#h2-2" id="h2-2"></a>

Take football as an example, you can get `score`, `stats`, `incidents` and `tlive` these four fields.

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

Updates for different data types are returned in separate arrays.&#x20;

For example, score updates are returned in the `score` array, while statistics updates are returned in the `stats` array.

If a message contains only one of these fields, no new data is available for the other fields. Wait for subsequent messages to receive further updates.


---

# 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://docs.thesports.com/help-center/first-integration/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.
