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. ServerNetWire.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. ClientNetWire.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
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
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
(
initialValue:
any?
,
--
Initial value for the property
) →
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())