Add Moderation to Chat
How to monitor and manage your in-game chat using Pubnub function modules
Introduction
For your event, you may want to control and moderate chat that's sent to your pubnub channel. This can be done by creating a new moderation function on your application's keyset.
Pubnub functions can be configured to manage different rules you want to enforce. In this guide, we will look at setting up a basic moderation function, and also a rate limiter to prevent spam.

💥 Creating a New Moderation Function
For more information on how Pubnub functions operate, we recommend looking their documentation.
To get started, first create a new module in the "Functions" section of your Pubnub application:

In your new function module, click on " Create New Function", at which point you will be presented with the following modal:

Set the name to an appropriate value, and set the following:
Select an event type: this must be set to
Before Publish or Fire
, in order to catch messages before they are published to your channel.Channel name: set this to
*
to apply it to all channels for your keyset.
Once done, you will be able to define some custom javascript to handle how your function responds to certain requests. An example with some basic filtering from a list of banned words can be seen below, but you can customise this however you like to suit your moderation purposes.
Once done, you can test your moderation module immediately by sending a test payload. Start your module (in the top right), and click publish on a test message payload on the bottom left of the page. If functioning correctly, you should see something like this:

You can test your moderation is correctly blocking messages by testing the in-game chat when playing in editor (see an example at the top of this guide).
🏎️ Rate Limiting
You may also want to control the rate in which users can send messages to your application's channels. This can be done by adding a rate limiter function to your existing moderation function (or creating a new function just for rate limiting if you don't want moderation).
If you want to set up rate limiting for your keyset, you can use this example script below. You can configure both the maximum number of messages a user can send, as well as the duration period in which they can be sent.
Full Function Including Moderation and Rate Limiting
You can copy and paste the full script below into your newly created Pubnub function to set up some initial moderation and rate limiting for your application.
Last updated
Was this helpful?