Filter profanity from messages

Moderation Dashboard

In addition to the moderation functionality supported by the SDKs, PubNub offers the open-source Moderation Dashboard that is an interactive UI you can use with your chat app to moderate users and messages.

The PubNub Profanity Filter function checks the content of a published message against a dictionary of objectionable words. You can either replace a portion of the text or completely block the message.

Setting up the PubNub Function

  1. Create a new function. Go to your Admin Portal and create a new module, and then create a new Before Publish function. Set up the function to trigger on a specific set of channels (such as chat.*), or on all channels using wildcards (*).

  2. Copy the function code from the gist. Make changes as necessary to the dictionary, and configure the function to either replace the swear words, or block the entire message from being published on the channel.

export default (request) => {
if (request.channels[0].endsWith('-pnpres') || request.channels[0].startsWith("blocks-output") ) {
return request.ok();
// The code in the linked gist has the real list of
// profanity to be blocked. Don't use this code as-is!
var badWords =new RegExp(/.*\b(hello|world)\b.*/,"i");

//Option 1 - Replace profanity text with * and allow the publish
if(request.message && request.message.text && badWords.test(request.message.text)){
var newString = request.message.text;
newString = newString.replace(badWords, "***");
request.message.text = newString;
return request.ok(); // Return a promise when you're done
show all 25 lines
  1. Click Start module on the right to start the function, and test it using the Test Payload field and Publish button on the left.

Testing the Function

Input: Publish a message on any channel with a text field included.

"text": "profanity message here"

Output: If profanity is present in the original message text, the output message will have the profanity filtered.

"text": "**** message here"
Last updated on