WebSocket: Send Asynchronous
Queue a message for transmission on an open WebSocket. Use this after connecting and registering an OnClose handler.
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.
| Parameter | Type | Description |
|---|---|---|
message | string | String payload, up to 16 MiB. |
binary | boolean? | 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
| Resource | Limit | What happens |
|---|---|---|
| Message size | 16 MiB per message | An oversized send is rejected; an oversized incoming message closes the connection. Text messages must be valid UTF-8. |
| Per-connection queue | 256 messages and 16 MiB of payload, combining incoming and outgoing messages | Exceeding either limit closes the overloaded connection, discards queued payloads, and reports closure through OnClose. |
| Shared queue budget | 32 MiB of payload across connections | A connection that cannot reserve more queue space is closed. An outgoing frame counts toward the budget until its write finishes. |
| Write deadline | 10 seconds per outgoing frame | A 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
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()