Volt

WebSocket.connect

Creates a new WebSocket connection to the specified URL.

Syntax

WebSocket.connect(url: string) -> WebSocket

Parameters

ParameterTypeDescription
urlstringThe WebSocket URL to connect to

Returns

TypeDescription
WebSocketA WebSocket object with methods and events

Description

WebSocket.connect establishes a persistent connection to a WebSocket server. The URL must use the ws:// or wss:// protocol (wss for secure/encrypted connections).

Example

-- Connect to a WebSocket server
local ws = WebSocket.connect("wss://echo.websocket.org")

-- Handle incoming messages
ws.OnMessage:Connect(function(message)
    print("Received:", message)
end)

-- Handle connection close
ws.OnClose:Connect(function()
    print("Connection closed")
end)

-- Send a message
ws:Send("Hello, WebSocket!")

Echo Server Example

local ws = WebSocket.connect("wss://echo.websocket.org")

ws.OnMessage:Connect(function(msg)
    print("Echo:", msg)
end)

-- Send messages that will be echoed back
ws:Send("Test message 1")
ws:Send("Test message 2")

Notes

  • WebSocket URLs must use ws:// or wss:// protocol
  • wss:// is WebSocket Secure (encrypted)
  • Connection is maintained until closed or server disconnects

On this page