Port → Using Ports

Using Ports

Pass the port name as the second argument to RoExpress() to get a Network or Listener wired to that port's isolated RemoteEvent. The call signature is identical to the main-channel versions.

Client Network on a port

RoExpress("Network", portName: string) → Network
local RoExpress = require(game.ReplicatedStorage.RoExpress)
local combatNet = RoExpress("Network", "combat")

-- callback style
combatNet:Post("shoot/123", nil, function(res)
    if res.ok then showHit(res.data) end
end)

-- promise style
combatNet:PostAsync("shoot/123")
    :Then(function(res) showHit(res.data) end)
    :Catch(function(err) warn(err.message) end)

Client Listener on a port

RoExpress("Listener", portName: string) → Listener

For push events fired from a port (port:Push() / port:PushAll()), subscribe with a Listener on the same port name:

local combatListener = RoExpress("Listener", "combat")

combatListener:On("hit.confirmed", function(data)
    print("damage:", data.damage)
end)

Caching

Each port name is cached — calling RoExpress("Network", "combat") from multiple scripts returns the same instance. There is no penalty for requiring it in several places.

Port name must match. The name passed on the client must exactly match the name used in app:Listen(name, ...) on the server. Mismatched names will fail to find the RemoteEvent.

Full combat example

-- Server (Script)
local RoExpress = require(game.ReplicatedStorage.RoExpress)
local app = RoExpress.GetApp()

app:Listen("combat", function(port)
    port:Post("shoot/:targetId=number", function(req, res)
        res:Send(handleShoot(req.player, req.params.targetId))
    end)
end)

-- Client (LocalScript)
local RoExpress = require(game.ReplicatedStorage.RoExpress)
local combatNet = RoExpress("Network", "combat")

combatNet:Post("shoot/456", nil, function(res)
    print(res.data)
end)

See also

← Port  ·  Creating Ports | server-side setup  ·  Port Settings | rate limiting per port  ·  Network | full client Network API  ·  Listener | full client Listener API