WebSocket:Send
Sends a message through the WebSocket connection.
Syntax
WebSocket:Send(message: string, isBinary?: boolean) -> ()Parameters
| Parameter | Type | Description |
|---|---|---|
message | string | The message to send |
isBinary | boolean? | Send a binary frame when true; defaults to false |
Returns
This method does not return a value.
Description
WebSocket:Send transmits the bytes in message as a text frame by default, or as a binary frame when isBinary is true. Calling it after Close is a no-op.
Example
local ws = WebSocket.connect("wss://your-server.example/socket")
-- Send a simple message
ws:Send("Hello, Server!")JSON Communication
local HttpService = game:GetService("HttpService")
local ws = WebSocket.connect("wss://your-server.example/socket")
-- Send JSON data
local function sendJSON(data)
ws:Send(HttpService:JSONEncode(data))
end
sendJSON({
type = "subscribe",
channel = "updates"
})
sendJSON({
type = "message",
content = "Hello!",
timestamp = os.time()
})Binary Message
local ws = WebSocket.connect("wss://your-server.example/socket")
local bytes = string.char(0x00, 0x01, 0x02, 0xFF)
ws:Send(bytes, true)Notes
- Luau strings can contain arbitrary bytes
Senddoes not return a delivery acknowledgement- Check
IsClosedbefore sending if another part of your script may close the client
Related
WebSocket.connect- Create connectionOnMessage- Receive responses