Version: 3.0.4

Writing custom middleware

If you want to write a custom middleware (e.g. for logging, or other purposes), you must write a Net.Middleware compatible function. This is similar to a middleware function in rodux.

This function is given a callback nextEvent, and instance (the remote instance).

It expects you to return a function, which is given the sender, and the args (arguments) after that, that were passed. This inner function should call and return nextEvent (if you want it to succeed) - or return nothing (if you don't want your remote to succeed with calling)

Middleware Type Signature
type NetMiddleware<CallArguments, PreviousCallArguments> =
(next: (player: Player, ...args: CallArguments) => void, event: NetManagedInstance)
=> (sender: Player, ...args: PreviousCallArguments) => void;
export const MyMiddleware: Net.Middleware = (nextMiddleware, instance) => {
return (sender, ...args) => {
return nextMiddleware(sender, ...args);
};
};

For example, if we want a specific remote to only be callable by administrators in our game:-

const GROUP_ID = 2664663; // Group id would go here
export const AdministratorMiddleware: Net.Middleware = (nextMiddleware, instance) => {
return (sender, ...args) => {
if (sender.GetRankInGroup(GROUP_ID) >= 250) {
// This would continue the remote execution
return nextMiddleware(sender, ...args);
}
// Otherwise the remote request is ignored
};
};

The above middleware, when applied to a remote will only continue and call the callback/listener if the next/nextMiddleware callbacks are called. Otherwise RbxNet will drop these requests.