Skip to main content

Middleware

Middleware is a powerful feature of YetAnotherNet that allows you to validate types before they are processed when sending and receiving data.

To create Middleware, you can specify a function in either Route:addIncomingMiddleware() or Route:addOutgoingMiddleware().

local YetAnotherNet = require("@packages/YetAnotherNet")

local Route = YetAnotherNet.Route
type Route<U...> = YetAnotherNet.Route<U...>;

local route: Route<number, string, boolean> = Route.new()

-- Sets a function to be ran on Incoming packets before they are received over the network
route:addIncomingMiddleware(function(number: unknown, string: unknown, boolean: unknown)
-- Do something, e.g., Validate Types

return number, string, boolean
end)

-- Sets a function to be ran on Outgoing packets before they are sent over the network
route:addOutgoingMiddleware(function(number: number, string: string, boolean: boolean)
-- Do something

return number, string, boolean
end)

To validate your types, you can return the values in order to allow it to be processed, or you can drop the packet by returning nil. Dropped packets will never reach your code, meaning you can ensure that the types your code receives are always the types you expect.