Hello, i have a question regarding firing asynchronous HTTP requests from within a match handler.
Say i want my match handler to confirm a player fulfills certain criteria that allow him to perform an action.
That action could for example be the player completing a quest. The player sends a message to the match handler requesting completion of the quest after having talked to an NPC, and Nakama makes sure the player is in the correct location to talk to the relevant NPC, has appropriate items in their inventory, has accepted the quest previously etc. Authoritatively confirming these things server-side is critical due to rewards being given to the player upon completion of the quest that may encourage cheating in order to obtain them.
Finishing the quest however also requires us to update data on a server separate from Nakama, which is accessed by multiple apps (social, web, etc.), not just the game-client, hence this data not being stored in Nakama. In this case, finishing a quest may update a users profile, or unlock a reward that can be redeemed for other valuables in the web-app.
So we send a request from Nakama to that server telling it to update the appropriate data. That server will confirm the request is valid and return a 200 response, which Nakama should receive and only then inform the player that the action was successful.
This means calling an HTTP route from within the Nakama match handlers matchLoop
, which however can only happen synchronously using the httpRequest
method. The alternative i have been using for this is to fire an event instead, which is then handled in Go (my match handler is written in TypeScript). This works fine for fire-and-forget requests, but if we want to handle the response, we need a way to pass that back to the match handler. I found that this is theoretically possible by using a function closure as described here and writing the response to a storage object, then checking that storage object in the match handler every tick (or every x ticks) until it has been created or changed (perhaps via bool flag added to the storage object), then reading it once it has.
This does seem like a pretty mad workaround though, and i expect that all those storage reads would not be the most performant thing to do either.
So i’m wondering, is there another way to handle HTTP requests asynchronously from within a match handler?
I do realize that the root of the problem lies with Nakama being dependent on a response from the second server to confirm an in-game action in the first place, and this could be resolved by instead storing the relevant data on the Nakama-server instead. That is an option i’m keeping open, however there is a sizeable existing architecture in place already, and not all relevant data stored on the second server can realistically be moved to Nakama. Having asynchronous communication between the two from within a match handler would make things a lot more flexible.
I’d appreciate any advice on this. Thank you!