ServerNetWire
Server-side NetWire implementation using Sleitnick's Comm under the hood. Provides a comprehensive interface for creating and managing networking infrastructure across your server codebase.
Structure:
ServerNetWireClass: Contains static methods (constructors, utilities)ServerNetWire: Contains instance methods (RegisterEvent, RegisterProperty, etc.)
This separation ensures better linting support and prevents static methods from appearing in instance method suggestions.
Key Features:
- Declarative API: Use markers to declare networking infrastructure
- Memoization: NetWires are cached by namespace for consistency
- Service Integration: Built-in support for Roam and similar frameworks
- Middleware Support: Comprehensive inbound and outbound middleware system
- Type Safety: Full typing with proper metatable integration
NetWire Creation: The following variables are all equivalent as NetWires are memoized by their namespace, so creating a new one with the same namespace will return the same object.
local TestNetWire1 = NetWire.Server.new("MyNetWire")
local TestNetWire2 = NetWire.Server("MyNetWire")
print(TestNetWire1 == TestNetWire2) -- true
INFO
You can also create a by calling the package directly, but this is not encouraged as it obscures the RunContext in which the NetWire is being created.
local TestNetWire3 = NetWire("MyNetWire")
Event and Property Creation: You can create client exposed events by registering them with the method or setting their index: The following two lines accomplish the same thing.
TestNetWire:RegisterEvent("TestEvent")
TestNetWire.TestEvent = NetWire.createEvent()
More examples can be found under the respective construction methods.
Types
ServerMiddleware
Middleware function for server-side operations. Returns whether to continue processing and any modified arguments.
Functions
new
constructorstaticConstructs a new ServerNetWire. If a ServerNetWire with the same nameSpace already exists, it will be returned instead.
setupServiceNetworking
constructorstatic
Creates a ServerNetWire from a Roam Service.
This method will read the service's Name and Client table to create the NetWire.
The goal of this function is to recreate the simplicity of Knit's networking features without
the systems being coupled together.
In order to access the service on the client, use NetWire.Client("SERVICE_NAME").
local NetWire = require(Packages.NetWire).Server
local Roam = require(Packages.Roam)
----------------------------------------------------------------
local ExampleService = {}
ExampleService.Client = {
TestEvent = NetWire.createEvent()
}
function ExampleService.Client:Greeting(plr: Player, msg: string)
print(plr.Name, "said", msg)
end
----------------------------------------------------------------
function ExampleService:RoamInit()
-- Build the NetWire from the service Client table and replace it
NetWire.setupServiceNetworking(self)
end
function ExampleService:RoamStart()
task.wait(10)
-- send a message to all clients
self.Client.TestEvent:FireAll("Hello from ExampleService!")
end
----------------------------------------------------------------
Roam.registerService(ExampleService, "ExampleService")
return ExampleService
Client Table Overwrite
Calling this function will overwrite the service's Client table with the NetWire.
You should not store anything aside from supported NetWire objects in the Client table.
Where to call
This function should be called within the init method of the service. This is to prevent netwires from being created outside of a running game.
RegisterEvent
ServerNetWire:RegisterEvent(eventName: string,isUnreliable: boolean?,) → ()Creates a remote event with the given name.
Server Documentation: https://sleitnick.github.io/RbxUtil/api/RemoteSignal
Client Documentation: https://sleitnick.github.io/RbxUtil/api/ClientRemoteSignal
-- Server Side
local myWire = NetWire.Server("MyWire")
myWire.TestEvent = NetWire.createEvent()
myWire.TestEvent:Connect(function(plr: Player, someArg)
print(someArg)
end)
myWire.TestEvent:FireAll("Hello from the server!")
---------------------------------------------------------
-- Client Side
local myWire = NetWire.Client("MyWire")
myWire.TestEvent:Connect(function(someArg)
print(someArg)
end)
myWire.TestEvent:Fire("Hello from the client!")
RegisterProperty
ServerNetWire:RegisterProperty(propertyName: string,initialValue: any?,) → ()Creates a remote property with the given name.
Server Documentation: https://sleitnick.github.io/RbxUtil/api/RemoteProperty
Client Documentation: https://sleitnick.github.io/RbxUtil/api/ClientRemoteProperty
-- Server Side
local myWire = NetWire.Server("MyWire")
myWire.TestProperty = NetWire.createProperty("Hello")
---------------------------------------------------------
-- Client Side
local myWire = NetWire.Client("MyWire")
if myWire.TestProperty:IsReady() then -- Check if its ready first
print( myWire.TestProperty:Get() ) -- "Hello"
end
RegisterMethod
ServerNetWire:RegisterMethod(functionName: string,tbl: {}?,) → ()Creates a remote function with the given name. This is not suggested to be used by end users; instead you should just append a function to a netwire object and it will properly wrap it for you.
-- Server Side
local myWire = NetWire.Server("MyWire")
function myWire:TestMethod(plr: Player, arg: number)
return arg * 2
end
---------------------------------------------------------
-- Client Side
local myWire = NetWire.Client("MyWire")
myWire:TestMethod(5):andThen(function(result)
print(result) -- 10
end)