WebSocket.connect Asynchronous

  • WebSocket.Connect
  • WebSocket.new
  • syn.websocket.Connect
  • syn.websocket.connect
  • syn.websocket.new
  • websocket.Connect
  • websocket.connect
  • websocket.new

Connect to a WebSocket server to exchange messages over a persistent connection. The returned socket provides send and close methods plus events for incoming messages and closure.

Syntax
Luau
WebSocket.connect(url: string) -> WebSocket

Parameters

Function parameters
ParameterTypeDescription
urlstringServer URL beginning with ws:// or wss://.

Returns

WebSocket

A connected WebSocket object. OnMessage supplies incoming messages as strings; OnClose notifies listeners that the connection has closed.

WebSocket fields
Luau
WebSocket = {
  OnMessage: NativeSignal, OnClose: NativeSignal,
  IsClosed: boolean
}

Usage notes

This library connects as a client. Register event handlers on the returned socket and close it when it is no longer needed.

Differences from sUNC

Kawaii also exposes IsClosed on the returned socket. Send accepts an optional binary flag, documented on its method page.

Example

Example
Luau
-- Start an echo server you control on this device, or replace the URL.
local socket = WebSocket.connect("ws://127.0.0.1:8765")
local received = socket.OnMessage:Connect(function(message)
    print("Echo:", message)
end)
socket:Send("example message")
task.wait(2)
received:Disconnect()
socket:Close()
Kawaii documentation