As a Roblox developer, it is currently impractical to add shims and middleware to RemoteFunctions due to not being able to get the value of OnServerInvoke / OnClientInvoke.
If Roblox is able to address this issue, it would improve my development experience because I can build generic tools for RemoteFunctions.
Coming from a web developer perspective this would enable generic building tools such as:
- Network performance debugging tools (see proof of concept below)
- Remote call throttles and mutexes
- Other types of generic middleware (validation, auth etc)
Example how I see it would be used:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
print("Initializing call stats")
local function hookRemoteFunction(remoteFunction)
local originalInvoke = remoteFunction.OnServerInvoke -- This will throw error
remoteFunction.OnServerInvoke = function(...)
print("TODO: increment counter for RemoteFunction:", remoteFunction.Name)
return originalInvoke(...)
end
end
for _, desc in pairs(ReplicatedStorage:GetDescendants()) do
if desc:IsA("RemoteFunction") then
hookRemoteFunction(desc)
end
end
ReplicatedStorage.DescendantAdded:Connect(function(desc)
if desc:IsA("RemoteFunction") then
hookRemoteFunction(desc)
end
end)
Current workaround:
Call the “middleware” function in the beginning of each invoke handler and/or assign the SetInvoke using a custom setup function that hooks the middleware before calling the actual handler.
Downside with the workaround is that the if you build a reusable middleware it will need manual code changes for each RemoteFunction used in the project.
Some reflections on this:
The workaround(s) might not be a huge effort to do as a developer, but on the other hand I cannot see any reason why get calls to OnServerInvoke / OnClientInvoke are not allowed.
Related References:
- Middleware in Rodux - Middleware example for state management
- Angular HttpInterceptor - Client side network middleware example in JavaScript
- gorilla/mux Middleware - Server side middleware in Go