DEV Community

the bomber
the bomber

Posted on

Stopping Chat Spam on Netlify with a Simple Rate‑Limit

Ever get flooded with bot messages on your public chat? It’s the kind of annoyance that turns a smooth user experience into a headache – and it doesn’t have to be that way.

Here are three practical steps I use on my own Netlify‑hosted site to keep chat spam in check without adding a third‑party service:

  • Persist a lightweight counter in Postgres. I store a row per IP address (or per user token) with a timestamp and a hit count. Each time a message hits the endpoint I increment the count and, if the row is older than a day, I reset it. The table stays tiny and the query is fast.
  • Gate the request with a Netlify Edge Function. The Edge Function reads the counter before the request reaches my main API. If the count exceeds the threshold (e.g., 5 messages per minute), the function short‑circuits and returns a 429 response. This stops the traffic before it even hits my backend.
  • Give a friendly “slow down” message. Instead of a generic error, I return a short JSON payload: { "error": "Rate limit exceeded – please wait a moment before sending another message." }. Users understand what happened, and bots get a clear signal to back off.

Why this works for me:

  • No extra SaaS costs – just the Postgres instance I already run at home.
  • The logic lives where the traffic lands (Netlify Edge), so latency stays low.
  • The approach scales with the size of my chat; the counter table stays under a few hundred rows even on busy days.

If you’re a contractor juggling multiple small‑business sites, a tiny Postgres counter plus an Edge Function can be a cheap, reliable safety net against chat spam.

— Kaleb, Dream Futures Tech, Pompano Beach, FL
I build sites and automations for small businesses at dreamfuturestech.com.

Top comments (0)