---
id: "T:cAlgo.API.WebSocketClient"
kind: "class"
namespace: "cAlgo.API"
url: "https://help.ctrader.com/ctrader-algo/references/WebSocket/WebSocketClient/"
---

> Documentation index for agents: https://help.ctrader.com/ctrader-algo/llms.txt · machine index: https://help.ctrader.com/ctrader-algo/api.json

# WebSocketClient (class)

Represents a web socket client.

Environments: Desktop supported · CLI supported · Cloud not supported.

- Cloud: Connect, Send and Close do nothing: "WebSocket is not supported in cloud environment." is written to the log, State stays None and no events are raised.

## Definition

```csharp
public class WebSocketClient
```

## Remarks

Algos can use WebSocketClient to establish a WebSocket connection with a local or remote host and send or receive data.

## Examples

C#:

```csharp
using System;
using System.Text;
using cAlgo.API;

namespace cAlgo.Robots;

public partial class Test : Robot
{
    protected override void OnStart()
    {
        var webSocket = new WebSocketClient();

        webSocket.Connected += args => Print("Connected");
        webSocket.Disconnected += args => Print("Disconnected");
        webSocket.TextReceived += args => Print("Text Received: ", args.Text);
        webSocket.BinaryReceived += args => Print("Binary Received: ", Encoding.UTF8.GetString(args.Data));

        var panel = new StackPanel()
        {
            BackgroundColor = Color.BlueViolet,
            Orientation = Orientation.Vertical,
            HorizontalAlignment = HorizontalAlignment.Center,
            VerticalAlignment = VerticalAlignment.Center
        };

        var connectButton = new Button
        {
            Text = "Connect"
        };

        connectButton.Click += args => webSocket.Connect(new Uri(Host));

        var disconnectButton = new Button
        {
            Text = "Disconnect"
        };

        disconnectButton.Click += args => webSocket.Close(WebSocketClientCloseStatus.Empty, null);

        var disposeButton = new Button
        {
            Text = "Dispose"
        };

        disposeButton.Click += args =>
        {
            webSocket.Dispose();

            Print($"WebSocket Disposed, state: {webSocket.State}");
        };

        var textBox = new TextBox()
        {
            Text = "Type your message...",
            Width = 100
        };

        var sendTextButton = new Button
        {
            Text = "Send Text"
        };

        var sendBinaryButton = new Button
        {
            Text = "Send Binary"
        };

        sendTextButton.Click += args => webSocket.Send(textBox.Text);
        sendBinaryButton.Click += args => webSocket.Send(Encoding.UTF8.GetBytes(textBox.Text));

        panel.AddChild(connectButton);
        panel.AddChild(disconnectButton);
        panel.AddChild(disposeButton);
        panel.AddChild(textBox);
        panel.AddChild(sendTextButton);
        panel.AddChild(sendBinaryButton);

        Chart.AddControl(panel);
    }
}
```

Python:

```python
import clr
clr.AddReference("cAlgo.API")

from cAlgo.API import *
from System import Uri
from System.Text import Encoding

class Test():
    def on_start(self):
        webSocket = WebSocketClient()
        webSocket.Connected += lambda args: print("Connected")
        webSocket.Disconnected += lambda args: print("Disconnected")
        webSocket.TextReceived += lambda args: print("Text Received: ", args.Text)
        webSocket.BinaryReceived += lambda args: print("Binary Received: ", Encoding.UTF8.GetString(args.Data))

        panel = StackPanel()
        panel.BackgroundColor = Color.BlueViolet
        panel.Orientation = Orientation.Vertical
        panel.HorizontalAlignment = HorizontalAlignment.Center
        panel.VerticalAlignment = VerticalAlignment.Center

        connectButton = Button()
        connectButton.Text = "Connect"
        connectButton.Click += lambda args: webSocket.Connect(Uri(api.Host))

        disconnectButton = Button()
        disconnectButton.Text = "Disconnect"
        disconnectButton.Click += lambda args: webSocket.Close(WebSocketClientCloseStatus.Empty, None)

        disposeButton = Button()
        disposeButton.Text = "Dispose"

        disposeButton.Click += lambda args: self.dispose_client(webSocket)

        textBox = TextBox()
        textBox.Text = "Type your message..."
        textBox.Width = 100

        sendTextButton = Button()
        sendTextButton.Text = "Send Text"
        sendTextButton.Click += lambda args: webSocket.Send(textBox.Text)

        sendBinaryButton = Button()
        sendBinaryButton.Text = "Send Binary"
        sendBinaryButton.Click += lambda args: webSocket.Send(Encoding.UTF8.GetBytes(textBox.Text))

        panel.AddChild(connectButton)
        panel.AddChild(disconnectButton)
        panel.AddChild(disposeButton)
        panel.AddChild(textBox)
        panel.AddChild(sendTextButton)
        panel.AddChild(sendBinaryButton)

        api.Chart.AddControl(panel)

    def dispose_client(self, webSocketClient):
        webSocketClient.Dispose()
        print(f"WebSocket Disposed, state: {webSocketClient.State}")
```

config.json:

```json
{
  "AccessRights": "None",
  "Parameters": [
    {
      "Name": "Host",
      "Type": "String",
      "DefaultValue": "ws://localhost:1337"
    }
  ]
}
```

## Constructors

- `public WebSocketClient()` — Creates a WebSocketClient with default options. [details](https://help.ctrader.com/ctrader-algo/references/WebSocket/WebSocketClient/Constructor/index.md)
- `public WebSocketClient(WebSocketClientOptions options)` — Creates a WebSocketClient with passed options. [details](https://help.ctrader.com/ctrader-algo/references/WebSocket/WebSocketClient/Constructor/index.md)

## Methods

- `public void Close(WebSocketClientCloseStatus closeStatus, string closeStatusDescription)` — Disconnects WebSocketClient. [details](https://help.ctrader.com/ctrader-algo/references/WebSocket/WebSocketClient/Close/index.md)
- `public void Connect(Uri uri)` — Connects WebSocketClient to passed URI. [details](https://help.ctrader.com/ctrader-algo/references/WebSocket/WebSocketClient/Connect/index.md)
- `public void Dispose()` — Releases the unmanaged resources used by the WebSocketClient instance. [details](https://help.ctrader.com/ctrader-algo/references/WebSocket/WebSocketClient/Dispose/index.md)
- `public void Send(string text)` — Sends passed text. [details](https://help.ctrader.com/ctrader-algo/references/WebSocket/WebSocketClient/Send/index.md)
- `public void Send(byte[] data)` — Sends passed binary data. [details](https://help.ctrader.com/ctrader-algo/references/WebSocket/WebSocketClient/Send/index.md)

## Properties

- `public WebSocketClientCloseStatus? CloseStatus {get;}` — Gets close status / reason. [details](https://help.ctrader.com/ctrader-algo/references/WebSocket/WebSocketClient/CloseStatus/index.md)
- `public string CloseStatusDescription {get;}` — Gets a description of the reason why the instance was closed. [details](https://help.ctrader.com/ctrader-algo/references/WebSocket/WebSocketClient/CloseStatusDescription/index.md)
- `public WebSocketClientState State {get;}` — Gets current state of WebSocketClient instance. [details](https://help.ctrader.com/ctrader-algo/references/WebSocket/WebSocketClient/State/index.md)

## Events

- `public event Action<WebSocketClientBinaryReceivedEventArgs> BinaryReceived;` — Occurs when WebSocketClient receives binary data. [details](https://help.ctrader.com/ctrader-algo/references/WebSocket/WebSocketClient/BinaryReceived/index.md)
- `public event Action<WebSocketClientConnectedEventArgs> Connected;` — Occurs when WebSocketClient is connected. [details](https://help.ctrader.com/ctrader-algo/references/WebSocket/WebSocketClient/Connected/index.md)
- `public event Action<WebSocketClientDisconnectEventArgs> Disconnected;` — Occurs when WebSocketClient is disconnected. [details](https://help.ctrader.com/ctrader-algo/references/WebSocket/WebSocketClient/Disconnected/index.md)
- `public event Action<WebSocketClientTextReceivedEventArgs> TextReceived;` — Occurs when WebSocketClient receives text data. [details](https://help.ctrader.com/ctrader-algo/references/WebSocket/WebSocketClient/TextReceived/index.md)

