---
source_url: https://www.pubnub.com/docs/chat/sdks/moderation/anti-spam
title: Spam prevention (deprecated)
updated_at: 2026-06-15T12:11:46.983Z
---

> Documentation Index
> For a curated overview of PubNub documentation, see: https://www.pubnub.com/docs/llms.txt
> For the full list of all documentation pages, see: https://www.pubnub.com/docs/llms-full.txt


# Spam prevention (deprecated)

:::warning Use Chat SDKs
This documentation is deprecated. Use any of our dedicated [Chat SDKs](https://www.pubnub.com/docs/chat/overview) to quickly implement chat functionality in your application.
:::

[PubNub Anti-Spam](https://www.pubnub.com/integrations/chat-message-profanity-filter/) function stops users from spamming on your application. This is done by limiting the rate at which messages can be published.

Anti Spam Block rate limits the publish rate from any IP address. The limit is configurable. The block keeps track of the times of published messages, and allows a message to get published only if the IP address doesn't cross the maximum number of messages allowed.

## 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 channel (such as spam-blocker-channel), a set of channels (such as chat.*), or on all channels using wildcards (*).
2. Copy the function code below. 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. 1// Requires the console module2const console = require('console');3const store = require('kvstore');4 5export default (request) => {6 // Window, aggregation period7 const aggregationWindow = 120; // Seconds8 9 // Duration10 const duration = 60; // Seconds11 12 // No of messages13 const noOfMessages = 5;14 15 // Since this is an after-publish event handler, we can't modify the16 // message17 18 let messageAllowed = false;19 20 // Check if an auth value exists in the incoming message21 // Messages without metadata will be discarded by returning null22 23 if (typeof request.params.auth === 'undefined') {24 request.status = 400;25 return request.abort('Message Dropped');26 }27 28 const currentTime = Math.round(Date.now() / 1000);29 30 return store.get('anti_spam').then((value) => {31 let antiSpamAuth = value && value.auth || {};32 const aggregationStart = value && value.aggregation_start || 0;33 34 if (currentTime - aggregationStart > aggregationWindow) {35 antiSpamAuth = {};36 }37 // get auth value38 var authValue = request.params.auth;39 40 if (!authValue) {41 request.status = 400;42 return request.abort('Message Dropped');43 }44 45 var authMessages = antiSpamAuth[authValue] || [];46 var oldestMessageTime = authMessages[0];47 48 if ((currentTime - oldestMessageTime) > duration ||49 authMessages.length < noOfMessages) {50 messageAllowed = true;51 }52 53 if (!messageAllowed) {54 request.status = 400;55 return request.abort('Message Dropped');56 }57 58 if (authMessages.length === noOfMessages) {59 authMessages.shift();60 }61 62 authMessages.push(currentTime);63 antiSpamAuth[authValue] = authMessages;64 65 store.set('anti_spam', {66 auth: antiSpamAuth,67 aggregation_start: currentTime68 });69 70 return request.ok();71 72 })73 .catch((e) => {74 console.error(e);75 });76 77}
3. 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 text on the input channel: spam-blocker-channel

```json
{
    "text": "Hi"
}
```

**Output**: If the message rate is exceeded a 400 error will be returned.

```json
{
    "message": "Message Dropped",
    "error": true,
    "status": 400
}
```