WebSocket:Send Asynchronous

Queue a message for transmission on an open WebSocket. Use this after connecting and registering an OnClose handler.

Syntax
Luau
WebSocket:Send(message: string, binary: boolean?) -> ()

Parameters

Call this method with a WebSocket receiver. The colon supplies self implicitly; a dot call must pass the receiver explicitly.

Function parameters
ParameterTypeDescription
messagestringString payload, up to 16 MiB.
binaryboolean?Optional boolean. true sends a binary frame; false or omission sends text.

Returns

()

No values.

Usage notes

Returning means the message was queued, not received by the peer. A closed socket or oversized message raises immediately; a later write failure appears through OnClose.

Limits

ResourceLimitWhat happens
Message size16 MiB per messageAn oversized send is rejected; an oversized incoming message closes the connection. Text messages must be valid UTF-8.
Per-connection queue256 messages and 16 MiB of payload, combining incoming and outgoing messagesExceeding either limit closes the overloaded connection, discards queued payloads, and reports closure through OnClose.
Shared queue budget32 MiB of payload across connectionsA connection that cannot reserve more queue space is closed. An outgoing frame counts toward the budget until its write finishes.
Write deadline10 seconds per outgoing frameA stalled write closes the connection. Returning from Send confirms queuing, not delivery.

Send can raise while queuing, and a write can fail after Send returns. Handle both the immediate error and OnClose. Once the connection closes, queued data is not guaranteed to reach the server.

Differences from sUNC

The sUNC method accepts only a message string. Kawaii adds the optional binary argument.

Example

Example
Luau
local socket = WebSocket.connect("wss://echo.websocket.events")
socket.OnClose:Connect(function() print("Connection ended") end)
local ok, reason = pcall(function()
    socket:Send("hello")
end)
if not ok then warn(reason) end
socket:Close()
Kawaii documentation