Mhmod-Hsn

payload plugin notifications

by Mhmod-Hsn
Notifications & messaging
v3
^3.90.0

Overview

Summary generated from the README

Adds a notifications collection to Payload where each record belongs to one recipient from an auth collection. Trusted server code creates records with a helper that supports idempotent deduplication keys. Recipients can list and read only their own notifications and mark them read. An optional polling-backed Server-Sent Events endpoint streams new notifications. Requires Payload 3.90 or later.

Features

  • Notifications collection with recipient relationship and readAt timestamp
  • createNotification helper for trusted server code using the Local API
  • Optional unique dedupeKey returns the existing record on duplicate inserts
  • Access rules limit list and read to the recipient; create and delete are denied
  • Opt-in SSE endpoint that polls the database and checks access on every poll
  • Configurable collection slug, field names, stream path and poll interval

Use cases

  • Show an in-app inbox of unread and read notifications to signed-in users
  • Create idempotent notifications from server hooks when comments or other events occur
  • Push new notifications to an open browser session without WebSockets

From the author

A durable, recipient-scoped notification inbox plugin for Payload CMS 3

Installation

npm i payload-plugin-notifications

3

Stars

0

Forks

0

Open Issues

2 months ago

Last Updated

12

Weekly Downloads

v1.1.0

npm Version

44.5 KB

Unpacked Size

0

Dependencies

GitHub stars

3+3 since Sep 27

Show data
GitHub stars by day (UTC)
DateStars
Dec 5, 20263
Dec 4, 20263
Dec 3, 20263
Dec 2, 20263
Dec 1, 20263
Nov 30, 20263
Nov 29, 20263
Nov 28, 20263
Nov 27, 20263
Nov 26, 20263
Nov 25, 20263
Nov 24, 20263
Nov 23, 20263
Nov 22, 20263
Nov 21, 20263
Nov 20, 20263
Nov 19, 20263
Nov 18, 20263
Nov 17, 20263
Nov 16, 20263
Nov 15, 20263
Nov 14, 20263
Nov 13, 20263
Nov 12, 20263
Nov 11, 20263
Nov 10, 20263
Nov 9, 20263
Nov 8, 20263
Nov 7, 20263
Nov 6, 20263
Nov 5, 20263
Nov 4, 20263
Nov 3, 20263
Nov 2, 20263
Nov 1, 20263
Oct 31, 20263
Oct 30, 20263
Oct 29, 20263
Oct 28, 20263
Oct 27, 20263
Oct 26, 20263
Oct 25, 20263
Oct 24, 20263
Oct 23, 20263
Oct 22, 20263
Oct 21, 20263
Oct 20, 20263
Oct 19, 20263
Oct 18, 20263
Oct 17, 20263
Oct 16, 20263
Oct 15, 20262
Oct 14, 20262
Oct 13, 20261
Oct 12, 20261
Oct 11, 20261
Oct 10, 20261
Oct 9, 20261
Oct 8, 20261
Oct 7, 20261
Oct 6, 20261
Oct 5, 20261
Oct 4, 20260
Oct 3, 20260
Oct 2, 20260
Oct 1, 20260
Sep 30, 20260
Sep 29, 20260
Sep 28, 20260
Sep 27, 20260
npm weekly downloads

5−265 since Oct 6

Show data
npm weekly downloads by day (UTC)
DateWeekly downloads
Dec 5, 20265
Dec 4, 20265
Dec 3, 20263
Dec 2, 20264
Dec 1, 20266
Nov 30, 20266
Nov 29, 20265
Nov 28, 20265
Nov 27, 20268
Nov 26, 20268
Nov 25, 20266
Nov 24, 20264
Nov 23, 20264
Nov 22, 20266
Nov 21, 20266
Nov 20, 20266
Nov 19, 20267
Nov 18, 20267
Nov 17, 20267
Nov 16, 20268
Nov 15, 20269
Nov 14, 20269
Nov 13, 20268
Nov 12, 20269
Nov 11, 202611
Nov 10, 202612
Nov 9, 202612
Nov 8, 202610
Nov 7, 202612
Nov 6, 20269
Nov 5, 20267
Nov 4, 20265
Nov 3, 20265
Nov 2, 20268
Nov 1, 20268
Oct 31, 20266
Oct 30, 20266
Oct 29, 20266
Oct 28, 20266
Oct 27, 202614
Oct 26, 202613
Oct 25, 202616
Oct 24, 202618
Oct 23, 202618
Oct 22, 202619
Oct 21, 202621
Oct 20, 202612
Oct 19, 202610
Oct 18, 20269
Oct 17, 202610
Oct 16, 202613
Oct 15, 202617
Oct 14, 202634
Oct 13, 202677
Oct 12, 2026346
Oct 11, 2026343
Oct 10, 2026340
Oct 9, 2026337
Oct 8, 2026332
Oct 7, 2026313
Oct 6, 2026270

Each point is npm's rolling 7-day download count.

Health Score

59/100Good

Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.

Badge for your README

Payload Directory health badge
Markdown
[![Payload Directory](https://payloaddirectory.dev/badge/1403694868-root.svg)](https://payloaddirectory.dev/plugins/1403694868-root)
HTML
<a href="https://payloaddirectory.dev/plugins/1403694868-root"><img src="https://payloaddirectory.dev/badge/1403694868-root.svg" alt="Payload Directory"></a>
Updated Oct 4, 2026Created Oct 3, 2026Published Oct 3, 2026

README

Payload Notifications Plugin

A Payload 3 plugin for a durable notification inbox scoped to the authenticated recipient. The package adds a notifications collection and exports a helper for creating records from trusted server code.

Install and configure

Install this package in a Payload 3 project and add the plugin to buildConfig. The configured auth collection must be an authentication-enabled collection.

import { buildConfig } from 'payload'
import { notificationsPlugin } from 'payload-plugin-notifications'

export default buildConfig({
  collections: [Users],
  plugins: [
    notificationsPlugin({
      authCollection: 'users',
      streaming: {
        enabled: true,
        pollIntervalMs: 1000,
      },
    }),
  ],
  // database, editor, secret, and other application config
})

Options and defaults:

OptionDefaultPurpose
enabledtrueDisable collection registration when false.
collectionSlugnotificationsSlug for the generated collection.
authCollectionusersCollection related to each recipient.
recipientFieldrecipientName of the recipient relationship.
readAtFieldreadAtName of the read timestamp.
deduplicationFielddedupeKeyName of the optional unique idempotency key.
streaming.enabledfalseAdd the authenticated SSE endpoint.
streaming.path/notifications/streamPath of the SSE endpoint under Payload's API route.
streaming.pollIntervalMs1000How often the open stream polls the adapter for new records; minimum 250 ms.

The plugin reports an error if its collection slug is already in use. Configure different field names in the plugin and pass the matching recipientField and deduplicationField to createNotification when using non-default names.

Create a notification

Only trusted server-side code should call the helper. Authenticate and authorize the domain action before creating a record. The helper uses Payload's Local API with access override because normal collection creates are denied.

import { createNotification } from 'payload-plugin-notifications'

const notification = await createNotification({
  payload,
  recipientId: user.id,
  type: 'comment.created',
  title: 'New comment',
  body: 'Someone commented on your post.',
  data: { postId: 'post-123' },
  dedupeKey: 'comment:123:user:456',
})

data is application-defined JSON and should be treated as untrusted content by consumers. The plugin does not turn it into a URL or navigate automatically.

Access and deduplication

Authenticated users can list and read only records whose recipient relationship matches their user ID. They can update their own readAt timestamp. Creation and deletion are denied through normal Payload access, and notification content, recipient, and deduplication key cannot be changed through normal updates.

When supplied, dedupeKey is globally unique within the notifications collection. Concurrent attempts rely on the adapter's unique constraint; when a duplicate insert fails, the helper returns the existing record for that key. Omitting the key creates a new notification each time. Deduplication keys are not scoped by recipient, so include recipient identity in keys when that matches the desired behavior.

List notifications through the normal Payload Local or REST API while authenticated. To mark one read with the Local API:

await payload.update({
  collection: 'notifications',
  id: notificationId,
  data: { readAt: new Date().toISOString() },
  user,
  overrideAccess: false,
})

The same recipient access rules apply to REST requests. Deletes are disabled by default; retention cleanup should be performed by trusted server code.

Live updates with Server-Sent Events

Streaming is opt-in. When enabled, the plugin registers GET /api/notifications/stream (replace /api if the host config uses another API route). The endpoint authenticates the request, checks the recipient access rule on every poll, and sends newly created notification documents as notification events.

const since = new Date().toISOString()
const events = new EventSource(
  `/api/notifications/stream?since=${encodeURIComponent(since)}`,
)

events.addEventListener('notification', (event) => {
  const notification = JSON.parse((event as MessageEvent).data)
  // Add or update it in the inbox by notification.id.
})

events.onerror = () => {
  // EventSource reconnects automatically. Close it when the inbox unmounts.
}

Use the regular notifications query to load inbox history; SSE delivers changes after the supplied since cursor. The event ID lets EventSource resume after a disconnect, and consumers should upsert by notification ID because a reconnect can replay an event from the same timestamp. Same-origin EventSource requests include the user's Payload session cookie. For cross-origin use, configure credentialed CORS and construct EventSource with { withCredentials: true }.

SSE uses database polling because Payload adapters do not share a portable change-feed API. Each open client creates a query at the configured interval, so use one EventSource per signed-in client session and tune the interval for the expected connection count. The stream needs a host that supports long-lived HTTP responses; serverless platforms may impose timeouts or buffering that make SSE unsuitable. This works across app instances without in-memory pub/sub, but it is not a push notification service.

Scope

This package provides storage, recipient access control, server-side creation, and optional polling-backed SSE. It does not include WebSocket delivery, email/push/SMS adapters, an inbox UI, domain-specific event catalogs, or automatic permission seeding. It uses Payload collection fields and indexes, so schema generation/migrations remain managed by the host application's Payload adapter and workflow. The package declares Payload ^3.90.0 as its peer dependency.

Development

The dev/ directory is a minimal Payload app using MongoDB Memory Server for integration tests.

pnpm install
pnpm test:int
pnpm lint
pnpm build

Set a DATABASE_URL and PAYLOAD_SECRET in dev/.env to run the dev app outside the test environment.

Similar plugins