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 yields while Volt establishes a connection. It returns a WebSocket object after a successful handshake and raises an error if the connection fails.

Example

-- Replace this with a WebSocket endpoint you control
local ws = WebSocket.connect("wss://your-server.example/socket")

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

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

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

Notes

  • Use a ws:// or wss:// WebSocket URL; wss:// encrypts the connection
  • Volt attempts to reconnect automatically after a remote disconnection
  • Call Close to stop the client intentionally