Skip to content

Search documentation

Enter at least two characters.

    TcpConnection

    SDK 0.1.0-preview.1 · ExperimentalDownload & release notes ↗

    An owned TCP byte stream with bounded asynchronous reads and writes.

    Advanced app/host integration API. Useful for lower-level work; ordinary app authors can start with the app-building path. This is public API, not permission to access private toolkit objects.

    Advanced raw networking. Calls that wait for I/O belong in task.spawn/task.defer; normal HTTP apps can use HttpService instead.

    Where to put it: replace Src/Main.luau in a project created with this SDK. This is the complete entry file.

    Project setup: add network.raw, network.client, network.server to Capabilities in lui.json. See the capability guide.

    local Window = Instance.new("Window", {
    Title = "LUI example", Size = UDim2.fromOffset(560, 280),
    })
    local Status = Instance.new("TextLabel", {
    Parent = Window, Text = "Ready", Position = UDim2.fromOffset(16, 170),
    Size = UDim2.new(1, -32, 0, 72),
    })
    local Network = app:GetService("NetworkService")
    local Listener = Network:ListenTcp({Address = "loopback", Port = 0})
    task.spawn(function()
    local Peer = Listener:AcceptAsync()
    local Bytes = Peer:ReadExactAsync(4)
    Peer:WriteAsync(Bytes)
    Peer:Close()
    Listener:Close()
    end)
    task.spawn(function()
    local Peer = Network:ConnectTcp({Address = "127.0.0.1", Port = Listener.Port})
    Peer:WriteAsync("ping")
    local Reply = Peer:ReadExactAsync(4)
    Status.Text = buffer.tostring(Reply)
    Peer:Close()
    end)
    Window.Destroying:Connect(function() Listener:Close() end)
    Window.Visible = true

    Expected result: A local TCP client sends four bytes and receives the echo. The fixed four-byte length is this example’s framing rule, not a TCP message boundary.

    Whether the connection remains open. Detecting a remote close requires an I/O operation.

    Type: boolean. Read-only.

    This side’s address and port.

    Type: NetworkEndpoint. Read-only.

    The peer’s address and port.

    Type: NetworkEndpoint. Read-only.

    Yields for up to MaxBytes bytes, at most 64 KiB. Returns a buffer, or nil for clean end-of-stream. A read is not a message boundary.

    ReadAsync(MaxBytes: number?) -> buffer?

    Yields until exactly Bytes bytes arrive, or raises UnexpectedEof if the peer ends early. Use only when your application protocol defines that length.

    ReadExactAsync(Bytes: number) -> buffer

    Yields until the bytes are accepted for sending. This does not prove that the peer application processed them.

    WriteAsync(Data: string | buffer) -> ()

    Closes Read, Write or Both directions without itself firing Closed. Use Close when finished with the connection.

    Shutdown(Direction: "Read" | "Write" | "Both") -> ()

    Closes the connection and cancels pending I/O. Repeated close is safe.

    Close() -> ()

    Fires once when close/EOF/terminal error is observed. No payload is passed; IsOpen is already false.

    Type: Signal. Subscribe with :Connect(function(...) ... end).

    Full type declaration — for type checking and deeper reference

    The declaration is generated from the SDK. Self is supplied by a colon call; read means the field cannot be assigned. See how to read this reference.

    export type TcpConnection = {
    read IsOpen: boolean,
    read LocalEndpoint: NetworkEndpoint,
    read RemoteEndpoint: NetworkEndpoint,
    read ReadAsync: (Self: TcpConnection, MaxBytes: number?) -> buffer?,
    read ReadExactAsync: (Self: TcpConnection, Bytes: number) -> buffer,
    read WriteAsync: (Self: TcpConnection, Data: string | buffer) -> (),
    read Shutdown: (Self: TcpConnection, Direction: "Read" | "Write" | "Both") -> (),
    read Close: (Self: TcpConnection) -> (),
    read Closed: Signal,
    }

    See platform limits before relying on host-specific behavior. The framework design specification and reflection/type sources are deeper engineering references, not extra setup required to build an app.