Skip to main content

NetWire

NetWire is a networking library that enables functionality similar to Sleitnick's Comm library, except it doesn't require the usage of intermediate instances. It provides a simple, no hassle, API for setting up client-server communication.


Remote Event Example:

A simple example of how to set up a NetWire instance and use it to send a message from a client to the server.

-- On the server:
-- 1) Require your NetWire module and access the desired run context.
local NetWire = require(Packages.NetWire).Server -- or .Client if in a LocalScript

-- 2) Create a new NetWire instance with a unique namespace by calling the Package.
local myWire = NetWire("MyWire")

-- 3) Define events and/or properties on the wire instance.
--    Here we will define some new event named `MyEvent`.
myWire.MyEvent = NetWire.createEvent()

-- 4) We will then connect to it and start listening.
myWire.MyEvent:Connect(function(player: Player, data: any)
    print(player, "sent data:", data)
end)

5) On the opposite run context, we will access the NetWire via the same name and fire the event.

-- On the client:
local NetWire = require(Packages.NetWire).Client
local myWire = NetWire("MyWire")
myWire.MyEvent:Fire("Hello from the client!")

Remote Function Example:

(Client -> Server -> Client)

-- On the server:
local NetWire = require(Packages.NetWire).Server
local myWire = NetWire("MyWire")

function myWire:MyFunction(player: Player, arg1: any, arg2: any)
    print(player, "called MyFunction with args:", arg1, arg2)
    return "Hello from the server!"
end
-- On the client:
local NetWire = require(Packages.NetWire).Client
local myWire = NetWire("MyWire")

local response = myWire:MyFunction("Hello", 42)
-- ! Remote functions return as promises !
response:andThen(function(result)
    print("Received response:", result)
end)
info

Remote functions are not available for (Server -> Client -> Server) communication.

Properties

Server

This item only works when running on the server. Server
NetWire.Server: ServerNetWire

Access point for server-side NetWire functionality. Can also be called as a function to create/access a new server wire instance.

local myWire = NetWire.Server("MyWire")
local myWireOther = NetWire.Server.new("MyWire")
print(myWire == myWireOther) -- true
info

NetWire memoizes the created wire instance so any subsequent calls with the same name will return the same wire instance.

Client

This item only works when running on the client. Client
NetWire.Client: ClientNetWire

Access point for client-side NetWire functionality. Can also be called as a function to create/access a new client wire instance.

local myWire = NetWire.Client("MyWire")
local myWireOther = NetWire.Client.new("MyWire")
print(myWire == myWireOther) -- true

Functions

createEvent

NetWire.createEvent(
inboundMiddleware{ServerMiddleware}?,
outboundMiddleware{ServerMiddleware}?
) → ServerRemoteEvent

Returns an EventMarker that is used to mark where a remoteSignal should be created. Calls ServerNetWire:RegisterEvent() when set to the index of a ServerNetWire. See ServerNetWire:RegisterEvent for more information.

local MyWire = NetWire.Server("MyWire")
MyWire.MyEvent = NetWire.createEvent()

MyWire.MyEvent:Connect(function(plr: Player, msg: string)
    print(plr, "said:", msg)
end)

createUnreliableEvent

NetWire.createUnreliableEvent(
inboundMiddleware{ServerMiddleware}?,
outboundMiddleware{ServerMiddleware}?
) → ServerRemoteEvent

Returns an EventMarker that is used to mark where an unreliable remoteSignal should be created. Calls ServerNetWire:RegisterEvent() when set to the index of a ServerNetWire. See ServerNetWire:RegisterEvent for more information.

local MyWire = NetWire.Server("MyWire")
MyWire.MyEvent = NetWire.createUnreliableEvent()

MyWire.MyEvent:FireAll("Hello, world!")

createProperty

NetWire.createProperty(
initialValueany?,--

Initial value for the property

inboundMiddleware{ServerMiddleware}?,
outboundMiddleware{ServerMiddleware}?
) → ServerRemoteProperty

Returns a PropertyMarker that is used to mark where a remoteProperty should be created. NetWire will transform this marker into an actual ServerRemoteProperty. The returned value of this is actually a symbol and only becomes usable when set to the index of a ServerNetWire.

local MyWire = NetWire.Server("MyWire")
MyWire.MyProperty = NetWire.createProperty("Initial Value")
MyWire.MyProperty:Set("New Value")
print(MyWire.MyProperty:Get())
Show raw api
{
    "functions": [
        {
            "name": "createEvent",
            "desc": "Returns an EventMarker that is used to mark where a remoteSignal should be created.\nCalls ServerNetWire:RegisterEvent() when set to the index of a ServerNetWire.\nSee ServerNetWire:RegisterEvent for more information.\n\n```lua\nlocal MyWire = NetWire.Server(\"MyWire\")\nMyWire.MyEvent = NetWire.createEvent()\n\nMyWire.MyEvent:Connect(function(plr: Player, msg: string)\n    print(plr, \"said:\", msg)\nend)\n```",
            "params": [
                {
                    "name": "inboundMiddleware",
                    "desc": "",
                    "lua_type": "{ ServerMiddleware }?"
                },
                {
                    "name": "outboundMiddleware",
                    "desc": "",
                    "lua_type": "{ ServerMiddleware }?\r\n"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "ServerRemoteEvent\r\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 86,
                "path": "lib/netwire/src/Markers.luau"
            }
        },
        {
            "name": "createUnreliableEvent",
            "desc": "Returns an EventMarker that is used to mark where an unreliable remoteSignal should be created.\nCalls ServerNetWire:RegisterEvent() when set to the index of a ServerNetWire.\nSee ServerNetWire:RegisterEvent for more information.\n\n```lua\nlocal MyWire = NetWire.Server(\"MyWire\")\nMyWire.MyEvent = NetWire.createUnreliableEvent()\n\nMyWire.MyEvent:FireAll(\"Hello, world!\")\n```",
            "params": [
                {
                    "name": "inboundMiddleware",
                    "desc": "",
                    "lua_type": "{ ServerMiddleware }?"
                },
                {
                    "name": "outboundMiddleware",
                    "desc": "",
                    "lua_type": "{ ServerMiddleware }?\r\n"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "ServerRemoteEvent\r\n"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 108,
                "path": "lib/netwire/src/Markers.luau"
            }
        },
        {
            "name": "createProperty",
            "desc": "Returns a PropertyMarker that is used to mark where a remoteProperty should be created.\nNetWire will transform this marker into an actual ServerRemoteProperty. The returned value\nof this is actually a symbol and only becomes usable when set to the index of a ServerNetWire.\n\n\n```lua\nlocal MyWire = NetWire.Server(\"MyWire\")\nMyWire.MyProperty = NetWire.createProperty(\"Initial Value\")\nMyWire.MyProperty:Set(\"New Value\")\nprint(MyWire.MyProperty:Get())\n```",
            "params": [
                {
                    "name": "initialValue",
                    "desc": "Initial value for the property",
                    "lua_type": "any?"
                },
                {
                    "name": "inboundMiddleware",
                    "desc": "",
                    "lua_type": "{ServerMiddleware}?"
                },
                {
                    "name": "outboundMiddleware",
                    "desc": "",
                    "lua_type": "{ServerMiddleware}?"
                }
            ],
            "returns": [
                {
                    "desc": "",
                    "lua_type": "ServerRemoteProperty"
                }
            ],
            "function_type": "static",
            "source": {
                "line": 135,
                "path": "lib/netwire/src/Markers.luau"
            }
        }
    ],
    "properties": [
        {
            "name": "Server",
            "desc": "Access point for server-side NetWire functionality.\nCan also be called as a function to create/access a new server wire instance.\n```lua\nlocal myWire = NetWire.Server(\"MyWire\")\nlocal myWireOther = NetWire.Server.new(\"MyWire\")\nprint(myWire == myWireOther) -- true\n```\n:::info\nNetWire memoizes the created wire instance so any subsequent calls with the same name will return the same wire instance.\n:::",
            "lua_type": "ServerNetWire",
            "realm": [
                "Server"
            ],
            "source": {
                "line": 110,
                "path": "lib/netwire/src/init.luau"
            }
        },
        {
            "name": "Client",
            "desc": "Access point for client-side NetWire functionality.\nCan also be called as a function to create/access a new client wire instance.\n```lua\nlocal myWire = NetWire.Client(\"MyWire\")\nlocal myWireOther = NetWire.Client.new(\"MyWire\")\nprint(myWire == myWireOther) -- true\n```",
            "lua_type": "ClientNetWire",
            "realm": [
                "Client"
            ],
            "source": {
                "line": 123,
                "path": "lib/netwire/src/init.luau"
            }
        }
    ],
    "types": [],
    "name": "NetWire",
    "desc": "NetWire is a networking library that enables functionality similar to Sleitnick's [Comm](https://sleitnick.github.io/RbxUtil/api/Comm/) library,\nexcept it doesn't require the usage of intermediate instances. It provides a simple, no hassle, API for setting up\nclient-server communication.\n\n----\n### Remote Event Example:\nA simple example of how to set up a NetWire instance and use it to send a message from a client to the server.\n```lua\n-- On the server:\n-- 1) Require your NetWire module and access the desired run context.\nlocal NetWire = require(Packages.NetWire).Server -- or .Client if in a LocalScript\n\n-- 2) Create a new NetWire instance with a unique namespace by calling the Package.\nlocal myWire = NetWire(\"MyWire\")\n\n-- 3) Define events and/or properties on the wire instance.\n--    Here we will define some new event named `MyEvent`.\nmyWire.MyEvent = NetWire.createEvent()\n\n-- 4) We will then connect to it and start listening.\nmyWire.MyEvent:Connect(function(player: Player, data: any)\n    print(player, \"sent data:\", data)\nend)\n```\n\n5) On the opposite run context, we will access the NetWire via the same name and fire the event.\n```lua\n-- On the client:\nlocal NetWire = require(Packages.NetWire).Client\nlocal myWire = NetWire(\"MyWire\")\nmyWire.MyEvent:Fire(\"Hello from the client!\")\n```\n\n----\n### Remote Function Example:\n**(Client -> Server -> Client)**\n```lua\n-- On the server:\nlocal NetWire = require(Packages.NetWire).Server\nlocal myWire = NetWire(\"MyWire\")\n\nfunction myWire:MyFunction(player: Player, arg1: any, arg2: any)\n    print(player, \"called MyFunction with args:\", arg1, arg2)\n    return \"Hello from the server!\"\nend\n```\n```lua\n-- On the client:\nlocal NetWire = require(Packages.NetWire).Client\nlocal myWire = NetWire(\"MyWire\")\n\nlocal response = myWire:MyFunction(\"Hello\", 42)\n-- ! Remote functions return as promises !\nresponse:andThen(function(result)\n    print(\"Received response:\", result)\nend)\n```\n:::info\nRemote functions are not available for (Server -> Client -> Server) communication.\n:::",
    "source": {
        "line": 68,
        "path": "lib/netwire/src/init.luau"
    }
}