---
title: "WebSocket (_WebSocket class)"
canonical: "https://docs.cuesystem.com/space/CVCHELP/1264156675/WebSocket%20(_WebSocket%20class)"
format: markdown
---
> Macro (toc)

---

WebSocket channel implements WebSocket protocol [https://datatracker.ietf.org/doc/html/rfc6455](https://datatracker.ietf.org/doc/html/rfc6455) . It allows to connect to the WebSocket server. 

> ℹ️ This type of channel is supported since firmware 21.10

# WebSocket channels limits

[https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510787585/CUE+System+limits#WebSocket-channels](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510787585/CUE+System+limits#WebSocket-channels) 

# Connect

The command opens a connection to the WebSocket server. It returns **True** on success or **False** if an error occurs. 

#### Syntax 

```visualbasic
Function Connect( 
  server As Text, 
  url As Text,
  Optional port As Long := 80,
  Optional usewss As Boolean := False,
  Optional checkcertificate As Boolean := False,
  Optional username As Text := "", 
  Optional password As Text := "",
  Optional sendbasicauthenticationdirectly As Boolean := False,
  Optional addheader As ByteString := &S"",
  Optional timeout As Time := :10.0)
As Boolean
```

#### Parameters 

server

<u>[Text](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Text)</u> It is the IP address of a server. It is specified by numbers (e.g. "192.168.30.5") or a domain name (e.g. "[echo.websocket.org](http://echo.websocket.org) "). 

url 

<u>[Text](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/495058958/Data+types#Text)</u>. endpoint url. 

port 

<u>[Long](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Long)</u> It is the port of a server. 

usewss 

<u>[Boolean](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u>. If it is True the encrypted WSS protocol is used, otherwise unecrypted WS protocol is used. TLS 1.1, TLS 1.2, and TLS 1.3 are supported. 

checkservercertificate 

<u>[Boolean](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u>. If it is True and encrypted WSS protocol is used, the server HTTPS/WebSocket certificate is checked. The certificate must be valid and signed by a trusted CA. 

username 

<u>[Text](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/495058958/Data+types#Text)</u> It is the username. 

password 

<u>[Text](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/495058958/Data+types#Text)</u> It is the password. 

sendbasicauthenticationdirectly

<u>[Boolean](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u>. It is optional parameter, default value is False. If True, the basic authorization is sent directly, in the HTTP header, without asking which authorization the HTTP/WebSocket server requires. This is required by some devices.

addheader 

<u>[ByteString](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/495058958/Data+types#ByteString)</u>. add more header items to the HTTP header. If you want to add more header items there must be \0D\0A  (CRLF) between them.  

> ℹ️ If server requires Bearer authentication you can use addheader parameter too add item Authorization: Bearer <token>

> ℹ️ If server requires WebSocket subprotocol you can use addheader parameter to add item
> ℹ️ 
> ℹ️ Sec-WebSocket-Protocol: <protocolname>

timeout 

<u>[Time](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Time)</u> Optional, defaults to 10 seconds. It is the connection timeout. 

#### Return value 

<u>[Boolean](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u>  

**True** on success 

**False** if an error occurs. A function <u>[GetLastError](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#GetLastError)</u> returns the error number that occurred while executing the command. The following errors may occur: <u>[88](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#88)</u> <u>[92](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#92)</u>, <u>[94](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#94)</u>, <u>[278](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#278)</u>, <u>[283](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#283)</u>, <u>[284](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#284)</u><u>,</u> <u>[285](https://cue.atlassian.net/wiki/spaces/CVCHELP/pages/510525445/Command+errors#285)</u>

#### Example 

```visualbasic

// connect to "ws://192.168.100.100/"
If WebSocket1.Connect("192.168.100.100","",80,False) Then 
    TcpClient1.Disconnect() 
End If 

```

# Disconnect

The command disconnects a client from the WebSocket server. 

#### Syntax* *

```visualbasic
Function Disconnect()
```

#### Example 

```visualbasic
// connect to "ws://192.168.100.100/"
If WebSocket1.Connect("192.168.100.100","",80,False) Then 
    TcpClient1.Disconnect() 
End If 

```

# GetProtocol

The command returns protocol which is used.  It is value of HTTP header tag **Sec-WebSocket-Protocol.**

This command could be used only on connected WebSocket channel.

#### Syntax* *

```visualbasic
Function GetProtocol(ByRef protocol As ByteString) As Boolean
```

#### Parameters 

protocol 

<u>[ByteString](/wiki/spaces/CVCHELP/pages/495058958/Data+types#ByteString)</u> It is a variable where the value of the HTTP header tag **Sec-WebSocket-Protocol** is placed to. 

#### Return value 

<u>[Boolean](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u>  

**True** on success otherwise **False.**

# ReceiveMessage

This function receives Websocket message from WebSocket server. It returns **True** if a message is received. It returns **False** if no data is received within the timeout limit or some error occurs. 

WebSocket protocol supports text message and binary message. If text masseage is received, parameter istextmessage is set to True. If binary message is received parameter istextmessage is set to True.

It text message is received is appropriate to convert received data to Text by command ByteStringToText. Take a look at the example

#### Syntax 

```visualbasic
Function ReceiveMessage(
  ByRef message As ByteString,
  ByRef istextmessage As Boolean,
  Optional timeout As Time : = : 10)
As Boolean
```

#### Parameters 

message

<u>[ByteString](/wiki/spaces/CVCHELP/pages/495058958/Data+types#ByteString)</u> It is a variable where received message is placed to. 

istextmessage 

<u>[Boolean](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u> It is a variable where type of received message is placed to. 

timeout 

<u>[Time](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Time)</u> Optional, defaults to 10 seconds. It is the receive timeout. 

#### Return value 

<u>[Boolean](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u>  

**True** if message is received.  

**False** if no data is received within the timeout limit or some error occurs. A function GetLastError returns the error number that occurred while executing the command. The following errors may occur: <u>[91](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#91)</u>, <u>[92](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#92)</u>. 

#### Example 

```visualbasic
Var RecvData As ByteString[100] 
Var txtmessage As Text[100]
Var istextmessage As Boolean

// connect to "wss://echo.websocket.org/.sse"
If WebSocket1.Connect("echo.websocket.org","/.sse", 443, True) = True Then 
  // wait for an message
  If WebSocket1.ReceiveMessage(RecvData,istextmessage) = True Then 
      If istextmessage Then
			ByteStringToText(RecvData,txtmessage ,_UTF8_TO_TEXT)
			DebugPrintFormat("Received Text Message: {t}", txtmessage)
		Else
			DebugPrintFormat("Received Binary Message: {s}",RecvData)
		End If
  End If 
  WebSocket1.Disconnect() 
End If
```

```visualbasic
Var RecvData As ByteString[100] 
Var txtmessage As Text[100]
Var istextmessage As Boolean

// connect to "ws://192.168.100.100/api/testwebsocket"
If WebSocket1.Connect("192.168.100.100","api/testwebsocket", 80) = True Then 
  // wait for an message
  If WebSocket1.ReceiveMessage(RecvData,istextmessage) = True Then 
      If istextmessage Then
			ByteStringToText(RecvData,txtmessage ,_UTF8_TO_TEXT)
			DebugPrintFormat("Received Text Message: {t}", txtmessage)
		Else
			DebugPrintFormat("Received Binary Message: {s}",RecvData)
		End If
  End If 
  WebSocket1.Disconnect() 
End If
```


# SendBinaryMessage

The function sends binary message to the WebSocket server . It returns **True** on success or **False** otherwise

#### Syntax 

```visualbasic
Function SendBinaryMessage(message As ByteString) As Boolean
```

#### Parameters 

message 

<u>[ByteString](/wiki/spaces/CVCHELP/pages/495058958/Data+types#ByteString)</u> It is the data to be sent. 

#### Return value 

<u>[Boolean](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u> 

***True*** on success 

***False*** if the *datatosend* is not accessible or some error occurs. A function GetLastError returns the error number that occurred while executing the command. The following errors may occur: <u>[91](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#91)</u>. 

#### Example 

```visualbasic

// connect to "ws://192.168.100.100/"
If WebSocket1.Connect("192.168.100.100", "", 80) = True Then 
  WebSocket1.SendBinaryMessage(&S"SomeBinaryMessage") 
  WebSocket1.Disconnect() 
End If
```

# SendTextMessage

The function sends text message to the WebSocket server . It returns **True** on success or **False** otherwise

#### Syntax 

```visualbasic
Function SendTextMessage(message As Text) As Boolean
```

#### Parameters 

message 

<u>[Text](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Text)</u> It is the data to be sent. 

#### Return value 

<u>[Boolean](/wiki/spaces/CVCHELP/pages/495058958/Data+types#Boolean)</u> 

***True*** on success 

***False*** if some error occurs. A function GetLastError returns the error number that occurred while executing the command. The following errors may occur: <u>[91](/wiki/spaces/CVCHELP/pages/510525445/Command+errors#91)</u>. 

#### Example 

```visualbasic

// connect to "ws://192.168.100.100/"
If WebSocket1.Connect("192.168.100.100", "", 80) = True Then 
  WebSocket1.SendTextMessage("SomeTextMessage") 
  WebSocket1.Disconnect() 
End If
```