How to rate-limit (flood prevention) chat messages

Out of the box user can send thousands messages per second via writeChatMessage and this messages broadcasted to other users. How we can limit this for example one message per second?

What I would do is set up an RPC call on the server that sends the chat message (if you’re using Typescript the documentation can be found here. This allows you to use the server to verify the person hasn’t sent a message in that channel in the last second before the message gets sent.

If you aren’t as worried about security an easier way would be to limit it on the client, but given your question you probably don’t want that.

Hi @formatCvt the client SDK does not rate-limit calls to writeChatMessage. It’s good practice to add a simple client side check to limit the time between sending messages.

Then on the server, add a chat message before hook to track when users send chat messages in an in-memory datastore such as Go’s map data structure.

@Confuzzleinator was not wrong with the RPC suggestion, before hooks just provide a lighter way of hooking into specifc features. For example, here’s a guide of how hooks can be used to limite API access Nakama: Guarding APIs | Heroic Labs Documentation

thanks, is there a way to do this on Typescript framework?

Yep! You can change this dropdown to Typescript on the top right of the documentation page to show how to do it in Typescript.

The before hook documentation page that @sean linked should then show you exactly how to create and register these hooks.