How To Call It
WebSocket is a global table: call its functions with a dot (WebSocket.<method>(...)), not a colon.
Opens a WebSocket connection and returns a connection object with send, receive, on, once, off, close and isOpen; raises an error when it cannot connect. The timeout is 1-60 seconds (default 15).
Documented for app version 1.0.51Updated:
WebSocket is a global table: call its functions with a dot (WebSocket.<method>(...)), not a colon.
For network integration, use HTTPS only, keep the response size bounded, and validate the response code before moving into JSON or File steps.
Required: url. Optional: timeoutMs, headers (omit to pass nil and use the runtime default). Returns a WebSocket object.
The most common pattern is to check a Request response with getResponseCode(), read its body with getData() and parse it with JSON.parse().
The snippet below is a starter pattern that can be applied directly in runtime flow.
local socket = WebSocket.connect("wss://example.com/socket", 10000)
socket:send("ping")
local event = socket:receive(5000)
if event then print(event.type) end
socket:close()From foundation to combined usage, each level is provided as a separate code block so you can copy the level you need and adapt it directly.
local socket = WebSocket.connect("wss://example.com/socket", 10000)
socket:send("ping")
local event = socket:receive(5000)
if event then print(event.type) end
socket:close()local stepOk = true
local socket = WebSocket.connect("wss://example.com/socket", 10000)
socket:send("ping")
local event = socket:receive(5000)
if event then print(event.type) end
socket:close()
if stepOk then
wait(200)
endlocal ok, err = pcall(function()
local socket = WebSocket.connect("wss://example.com/socket", 10000)
socket:send("ping")
local event = socket:receive(5000)
if event then print(event.type) end
socket:close()
end)
if not ok then
print("API step failed: WebSocket.connect: " .. tostring(err))
requestStop()
end-- For network integration, use HTTPS only, keep the response size bounded, and validate the response code before moving into JSON or File steps.
local function run_connect_step()
local socket = WebSocket.connect("wss://example.com/socket", 10000)
socket:send("ping")
local event = socket:receive(5000)
if event then print(event.type) end
socket:close()
end
local ok, err = pcall(run_connect_step)
if not ok then
toast("Step failed")
print(err)
endlocal socket = WebSocket.connect("wss://example.com/socket", 10000)
socket:send("ping")
local event = socket:receive(5000)
if event then print(event.type) end
socket:close()
KV.set("last_network_step", DateTime.now():format("yyyy-MM-dd HH:mm:ss"))
print("Network step finished")