> For the complete documentation index, see [llms.txt](https://docs.chainupcloud.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.chainupcloud.com/zhong-wen/qu-kuai-lian-api/polkadot/websocket-stream.md).

# WebSocket 流

Unlike HTTP, with WebSockets, you don't need to continuously make requests when you want specific information. WebSockets maintain a network connection for you (if done right) and listen for changes . You may view the official documentation [here](https://wiki.polkadot.network/docs/maintain-wss).  与 HTTP 不同，使用 WebSockets，当您需要特定信息时，您无需不断发出请求。WebSockets 会为您维护网络连接（如果操作正确）并监听更改。您可以在[**此处查看官方文档**](https://wiki.polkadot.network/docs/maintain-wss)。

{% tabs %}
{% tab title="WebSocket" %}

```
wscat -c wss://api.chainup.net/ws/polkadot/mainnet/<YOUR_API_KEY>
<  {"jsonrpc":"2.0","method":"chain_getBlock","params":[],"id":32}
<  {"jsonrpc":"2.0","result":{"block":{"header":{"parentHash":"0x8a55738db2a55ee21eadf27756348c6e3883c93fc4c55424511da8c5ea8d3a13","number":"0xb04b2a", ... }
```

{% endtab %}

{% tab title="Javascript" %}

```
// Required imports
const { ApiPromise, WsProvider } = require('@polkadot/api');

async function main () {
  // Initialise the provider to connect to the local node
  const provider = new WsProvider('wss://api.chainup.net/ws/polkadot/mainnet/<YOUR_API_KEY>');

  // Create the API and wait until ready
  const api = await ApiPromise.create({ provider });

  // Retrieve the chain & node information information via rpc calls
  const [chain, nodeName, nodeVersion] = await Promise.all([
    api.rpc.system.chain(),
    api.rpc.system.name(),
    api.rpc.system.version()
  ]);

  console.log(`You are connected to chain ${chain} using ${nodeName} v${nodeVersion}`);
}

main().catch(console.error).finally(() => process.exit());
```

{% endtab %}

{% tab title="Python" %}

```
import asyncio
from jsonrpc_websocket import Server

async def routine():
    server = Server('wss://api.chainup.net/ws/polkadot/mainnet/<YOUR_API_KEY>')
    try:
        await server.ws_connect()

        chain = await server.api.rpc.system.chain()
        nodeName = await api.rpc.system.name()
        nodeVersion = await api.rpc.system.version()
        
        print(f'You are connected to chain {chain} using {nodeName} v{nodeVersion}')
    finally:
        await server.close()

asyncio.get_event_loop().run_until_complete(routine())
```

{% endtab %}
{% endtabs %}


---

# 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:

```
GET https://docs.chainupcloud.com/zhong-wen/qu-kuai-lian-api/polkadot/websocket-stream.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.
