Skip to main content

WebSocketConnection

Type

Represents an active WebSocketConnection connection.

This object is returned by websocket.connect upon a successful connection. It allows sending messages and listening for incoming messages and closure events.

Properties

OnMessage

This item is read only and cannot be modified. Read Only
WebSocketConnection.OnMessage: RBXScriptSignal<string>

An RBXScriptSignal that fires when a message is received from the WebSocketConnection server.

Connecting
local webSocket = websocket.connect("wss://example.com")
if webSocket then
    webSocket.OnMessage:Connect(function(message)
        print("Received message:", message)
    end)
end

OnClose

This item is read only and cannot be modified. Read Only
WebSocketConnection.OnClose: RBXScriptSignal

An RBXScriptSignal (effectively BindableEvent.Event) that fires when the WebSocketConnection connection is closed, either initiated by the server, the client (:Close()), or due to an error.

Connecting
local webSocket = websocket.connect("wss://example.com")
if webSocket then
    webSocket.OnClose:Connect(function()
        print("WebSocketConnection connection closed")
    end)
end

URL

This item is read only and cannot be modified. Read Only
WebSocketConnection.URL: string

The URL of the websocket connection.

Functions

Send

WebSocketConnection:Send(
messagestring--

The message to send.

) → ()

Sends a string message over the WebSocketConnection connection.

If the connection is not currently open (e.g., it's connecting, closed, or closing), this method does nothing.

Close

WebSocketConnection:Close() → ()

Closes the WebSocketConnection connection.

This will trigger the OnClose event asynchronously. If the connection is already closed or closing, this method does nothing.

Show raw api
{
    "functions": [
        {
            "name": "Send",
            "desc": "Sends a string message over the WebSocketConnection connection.\n\nIf the connection is not currently open (e.g., it's connecting, closed, or closing), this method does nothing.",
            "params": [
                {
                    "name": "message",
                    "desc": "The message to send.",
                    "lua_type": "string"
                }
            ],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 68,
                "path": "impl/Types/WebSocketConnection.luau"
            }
        },
        {
            "name": "Close",
            "desc": "Closes the WebSocketConnection connection.\n\nThis will trigger the `OnClose` event asynchronously. If the connection is already closed or closing, this method does nothing.",
            "params": [],
            "returns": [],
            "function_type": "method",
            "source": {
                "line": 79,
                "path": "impl/Types/WebSocketConnection.luau"
            }
        }
    ],
    "properties": [
        {
            "name": "OnMessage",
            "desc": "An RBXScriptSignal that fires when a message is received from the WebSocketConnection server.\n\n\n:::tip Connecting\n```lua\nlocal webSocket = websocket.connect(\"wss://example.com\")\nif webSocket then\n    webSocket.OnMessage:Connect(function(message)\n        print(\"Received message:\", message)\n    end)\nend\n```\n:::",
            "lua_type": "RBXScriptSignal<string>",
            "readonly": true,
            "source": {
                "line": 29,
                "path": "impl/Types/WebSocketConnection.luau"
            }
        },
        {
            "name": "OnClose",
            "desc": "An RBXScriptSignal (effectively `BindableEvent.Event`) that fires when the WebSocketConnection connection is closed, either initiated by the server, the client (`:Close()`), or due to an error.\n\n\n:::tip Connecting\n```lua\nlocal webSocket = websocket.connect(\"wss://example.com\")\nif webSocket then\n    webSocket.OnClose:Connect(function()\n        print(\"WebSocketConnection connection closed\")\n    end)\nend\n```\n:::",
            "lua_type": "RBXScriptSignal",
            "readonly": true,
            "source": {
                "line": 49,
                "path": "impl/Types/WebSocketConnection.luau"
            }
        },
        {
            "name": "URL",
            "desc": "The URL of the websocket connection.",
            "lua_type": "string",
            "readonly": true,
            "source": {
                "line": 58,
                "path": "impl/Types/WebSocketConnection.luau"
            }
        }
    ],
    "types": [],
    "name": "WebSocketConnection",
    "desc": "Represents an active WebSocketConnection connection.\n\nThis object is returned by `websocket.connect` upon a successful connection. It allows sending messages and listening for incoming messages and closure events.",
    "tags": [
        "Type"
    ],
    "source": {
        "line": 9,
        "path": "impl/Types/WebSocketConnection.luau"
    }
}