Object MethodNetwork and Integration

WebSocket:once(eventName, handler)

Adds a listener that runs once on the connection.

Documented for app version 1.0.51Updated:

Detailed Explanation

This section explains when to use the API, how to call it, and which structures it works best with in production flow.

How To Call It

This is an object method: call it with a colon on a connection object opened with WebSocket.connect(url) (socket:<method>(...)). The global WebSocket table does not have this method.

When To Use It

For network integration, use HTTPS only, keep the response size bounded, and validate the response code before moving into JSON or File steps.

Parameters and Return

Required: eventName, handler. Returns a number.

Best Combined With

The most common pattern is to check a Request response with getResponseCode(), read its body with getData() and parse it with JSON.parse().

Example Usage

The snippet below is a starter pattern that can be applied directly in runtime flow.

local socket = WebSocket.connect("wss://example.com/socket")
local result = socket:once("open", function(event) print(event) end)

Copyable Progressive Examples

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.

Foundation

Shows the shortest direct way to call the API.

Foundation
local socket = WebSocket.connect("wss://example.com/socket")
local result = socket:once("open", function(event) print(event) end)

Simple

Wraps the base call with minimal flow control.

Simple
local stepOk = true
local socket = WebSocket.connect("wss://example.com/socket")
local result = socket:once("open", function(event) print(event) end)
if stepOk then
  wait(200)
end

Practical Flow

A practical pattern for real macros with pcall, logging, and guards.

Practical Flow
local ok, err = pcall(function()
  local socket = WebSocket.connect("wss://example.com/socket")
  local result = socket:once("open", function(event) print(event) end)
end)

if not ok then
  print("API step failed: WebSocket:once: " .. tostring(err))
  requestStop()
end

Detailed

This level packages the API into a reusable helper with error reporting.

Detailed
-- 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_once_step()
  local socket = WebSocket.connect("wss://example.com/socket")
  local result = socket:once("open", function(event) print(event) end)
end

local ok, err = pcall(run_once_step)
if not ok then
  toast("Step failed")
  print(err)
end

Combined

Combines the API with related structures to form a more realistic workflow.

Combined
local socket = WebSocket.connect("wss://example.com/socket")
local result = socket:once("open", function(event) print(event) end)
KV.set("last_network_step", DateTime.now():format("yyyy-MM-dd HH:mm:ss"))
print("Network step finished")