Adds a websocket server to Payload that emits events when documents in chosen collections are created or updated. Events can be scoped to rooms such as per user or per chat, and JWT authentication for socket connections is optional. A React hook and a client class let frontends subscribe to collection events or custom events. Requires Payload 3.37 or later.
This plugin provides an implementation of websockets to enable real-time events in Payload CMS collections.
Installation
npm i @alejotoro-o/payload-real-time30
Stars
0
Forks
1
Open Issues
11 months ago
Last Updated
64
Weekly Downloads
v1.2.0
npm Version
46.8 KB
Unpacked Size
3
Dependencies
30+2 since Sep 7
| Date | Stars |
|---|---|
| Dec 6, 2026 | 30 |
| Dec 5, 2026 | 30 |
| Dec 4, 2026 | 30 |
| Dec 3, 2026 | 30 |
| Dec 2, 2026 | 30 |
| Dec 1, 2026 | 30 |
| Nov 30, 2026 | 30 |
| Nov 29, 2026 | 30 |
| Nov 28, 2026 | 30 |
| Nov 27, 2026 | 30 |
| Nov 26, 2026 | 30 |
| Nov 25, 2026 | 30 |
| Nov 24, 2026 | 30 |
| Nov 23, 2026 | 30 |
| Nov 22, 2026 | 30 |
| Nov 21, 2026 | 30 |
| Nov 20, 2026 | 30 |
| Nov 19, 2026 | 30 |
| Nov 18, 2026 | 30 |
| Nov 17, 2026 | 30 |
| Nov 16, 2026 | 30 |
| Nov 15, 2026 | 30 |
| Nov 14, 2026 | 30 |
| Nov 13, 2026 | 30 |
| Nov 12, 2026 | 30 |
| Nov 11, 2026 | 30 |
| Nov 10, 2026 | 30 |
| Nov 9, 2026 | 30 |
| Nov 8, 2026 | 30 |
| Nov 7, 2026 | 30 |
| Nov 6, 2026 | 30 |
| Nov 5, 2026 | 30 |
| Nov 4, 2026 | 30 |
| Nov 3, 2026 | 30 |
| Nov 2, 2026 | 30 |
| Nov 1, 2026 | 30 |
| Oct 31, 2026 | 30 |
| Oct 30, 2026 | 30 |
| Oct 29, 2026 | 30 |
| Oct 28, 2026 | 30 |
| Oct 27, 2026 | 30 |
| Oct 26, 2026 | 30 |
| Oct 25, 2026 | 30 |
| Oct 24, 2026 | 30 |
| Oct 23, 2026 | 30 |
| Oct 22, 2026 | 30 |
| Oct 21, 2026 | 30 |
| Oct 20, 2026 | 30 |
| Oct 19, 2026 | 30 |
| Oct 18, 2026 | 30 |
| Oct 17, 2026 | 30 |
| Oct 16, 2026 | 30 |
| Oct 15, 2026 | 30 |
| Oct 14, 2026 | 30 |
| Oct 13, 2026 | 30 |
| Oct 12, 2026 | 30 |
| Oct 11, 2026 | 30 |
| Oct 10, 2026 | 30 |
| Oct 9, 2026 | 30 |
| Oct 8, 2026 | 30 |
| Oct 7, 2026 | 30 |
| Oct 6, 2026 | 30 |
| Oct 5, 2026 | 30 |
| Oct 4, 2026 | 30 |
| Oct 3, 2026 | 30 |
| Oct 2, 2026 | 30 |
| Oct 1, 2026 | 30 |
| Sep 30, 2026 | 30 |
| Sep 29, 2026 | 30 |
| Sep 28, 2026 | 30 |
| Sep 27, 2026 | 30 |
| Sep 26, 2026 | 30 |
| Sep 25, 2026 | 30 |
| Sep 24, 2026 | 30 |
| Sep 23, 2026 | 30 |
| Sep 22, 2026 | 30 |
| Sep 21, 2026 | 30 |
| Sep 20, 2026 | 30 |
| Sep 19, 2026 | 30 |
| Sep 18, 2026 | 30 |
| Sep 17, 2026 | 30 |
| Sep 16, 2026 | 30 |
| Sep 15, 2026 | 30 |
| Sep 14, 2026 | 30 |
| Sep 13, 2026 | 30 |
| Sep 12, 2026 | 30 |
| Sep 11, 2026 | 30 |
| Sep 10, 2026 | 30 |
| Sep 9, 2026 | 29 |
| Sep 8, 2026 | 28 |
| Sep 7, 2026 | 28 |
69−8 since Sep 7
| Date | Weekly downloads |
|---|---|
| Dec 6, 2026 | 69 |
| Dec 5, 2026 | 71 |
| Dec 4, 2026 | 89 |
| Dec 3, 2026 | 112 |
| Dec 2, 2026 | 138 |
| Dec 1, 2026 | 163 |
| Nov 30, 2026 | 174 |
| Nov 29, 2026 | 162 |
| Nov 28, 2026 | 153 |
| Nov 27, 2026 | 132 |
| Nov 26, 2026 | 116 |
| Nov 25, 2026 | 101 |
| Nov 24, 2026 | 67 |
| Nov 23, 2026 | 52 |
| Nov 22, 2026 | 62 |
| Nov 21, 2026 | 63 |
| Nov 20, 2026 | 88 |
| Nov 19, 2026 | 85 |
| Nov 18, 2026 | 103 |
| Nov 17, 2026 | 100 |
| Nov 16, 2026 | 95 |
| Nov 15, 2026 | 79 |
| Nov 14, 2026 | 81 |
| Nov 13, 2026 | 67 |
| Nov 12, 2026 | 83 |
| Nov 11, 2026 | 91 |
| Nov 10, 2026 | 106 |
| Nov 9, 2026 | 112 |
| Nov 8, 2026 | 125 |
| Nov 7, 2026 | 151 |
| Nov 6, 2026 | 151 |
| Nov 5, 2026 | 147 |
| Nov 4, 2026 | 119 |
| Nov 3, 2026 | 118 |
| Nov 2, 2026 | 118 |
| Nov 1, 2026 | 119 |
| Oct 31, 2026 | 91 |
| Oct 30, 2026 | 83 |
| Oct 29, 2026 | 62 |
| Oct 28, 2026 | 53 |
| Oct 27, 2026 | 42 |
| Oct 26, 2026 | 43 |
| Oct 25, 2026 | 31 |
| Oct 24, 2026 | 24 |
| Oct 23, 2026 | 15 |
| Oct 22, 2026 | 37 |
| Oct 21, 2026 | 54 |
| Oct 20, 2026 | 51 |
| Oct 19, 2026 | 46 |
| Oct 18, 2026 | 47 |
| Oct 17, 2026 | 63 |
| Oct 16, 2026 | 67 |
| Oct 15, 2026 | 51 |
| Oct 14, 2026 | 43 |
| Oct 13, 2026 | 44 |
| Oct 12, 2026 | 49 |
| Oct 11, 2026 | 50 |
| Oct 10, 2026 | 46 |
| Oct 9, 2026 | 54 |
| Oct 8, 2026 | 62 |
| Oct 7, 2026 | 54 |
| Oct 6, 2026 | 56 |
| Oct 5, 2026 | 50 |
| Oct 4, 2026 | 55 |
| Oct 3, 2026 | 45 |
| Oct 2, 2026 | 44 |
| Oct 1, 2026 | 53 |
| Sep 30, 2026 | 66 |
| Sep 29, 2026 | 104 |
| Sep 28, 2026 | 106 |
| Sep 27, 2026 | 98 |
| Sep 26, 2026 | 144 |
| Sep 25, 2026 | 133 |
| Sep 24, 2026 | 114 |
| Sep 23, 2026 | 104 |
| Sep 22, 2026 | 63 |
| Sep 21, 2026 | 69 |
| Sep 20, 2026 | 70 |
| Sep 19, 2026 | 32 |
| Sep 18, 2026 | 50 |
| Sep 17, 2026 | 57 |
| Sep 16, 2026 | 54 |
| Sep 15, 2026 | 53 |
| Sep 14, 2026 | 45 |
| Sep 13, 2026 | 50 |
| Sep 12, 2026 | 45 |
| Sep 11, 2026 | 49 |
| Sep 10, 2026 | 47 |
| Sep 9, 2026 | 60 |
| Sep 8, 2026 | 72 |
| Sep 7, 2026 | 77 |
Each point is npm's rolling 7-day download count.
Health Score
Composite score based on GitHub activity, stars, npm downloads, publish recency, dependency count, and package size.
Badge for your README
[](https://payloaddirectory.dev/plugins/1088989388-root)<a href="https://payloaddirectory.dev/plugins/1088989388-root"><img src="https://payloaddirectory.dev/badge/1088989388-root.svg" alt="Payload Directory"></a>README
This plugin provides an implementation of websockets to enable real-time events in Payload CMS collections.
Install the package running:
npm install @alejotoro-o/payload-real-time
NOTE: On some implementation you can get the error [TypeError: bufferUtil.unmask is not a function], in that case you need to install the bufferutil package with npm install bufferutil.
useRealtime) for subscribing to events.First configure the plugin in payload.config.ts. The plugin accepts a configuration object with the following shape:
export type PayloadRealTimeConfig = {
collections?: { [K in CollectionSlug]?: {
room: (
doc: Extract<DefinedCollections[number], { slug: K }>['fields'],
operation?: 'create' | 'update',
previousDoc?: Extract<DefinedCollections[number], { slug: K }>['fields'],
) => string | undefined,
events?: Array<'create' | 'update'>
} }
serverOptions?: {
port?: number,
cors?: {
origin: string | string[],
methods?: string | string[],
}
}
requireAuth?: boolean,
disabled?: boolean,
}
collections: Define which collections should emit real-time events. For each collection, you can specify:
events: Limit which operations trigger events. Use 'create', 'update', or both. If omitted, all operations will emit.room: A function that receives the resulting document, the operation being performed in the collection and the previous document; the function returns a room string. This scopes the event to a specific room (e.g. user:{userId}).serverOptions: Customize the WebSocket server:
port: Port to run the server on (default is 3001)cors: CORS configuration (e.g. { origin: '*' })requireAuth: Set to true to enable JWT authentication for socket connections. Default is false.
disabled: Set to true to disable the plugin entirely.
Here is an example configuring the plugin to emit events when documents are created or updated in the notifications collection.
import payloadRealTime from '@alejotoro-o/payload-real-time'
export const config = buildConfig({
// ... Payload config options
plugins: [
payloadRealTime({
collections: {
notifications: {
room: (doc: Notification) => {
const user = doc.user
const userId = typeof user === 'number' ? user : user?.id
return userId ? `user:${userId}` : undefined
},
events: ['create', 'update'],
},
},
requireAuth: true
}),
],
})
Use the useRealtime hook to subscribe to events in your frontend:
'use client'
import { User } from "payload-types.js"
import { useEffect, useState } from "react"
import { useRealtime } from "../../../src/client/useRealtime.js"
export default function Page() {
const [user, setUser] = useState<User>()
const [token, setToken] = useState()
const realtime = useRealtime(user?.id.toString() ?? '', token ?? '')
const [message, setMessage] = useState()
useEffect(() => {
const getUser = async () => {
const response = await fetch('http://localhost:3000/api/users/me')
if (response.ok) {
const user = await response.json()
setUser(user.user)
setToken(user.token)
} else {
console.log('Error')
}
}
getUser()
}, [])
// Websockets
useEffect(() => {
if (!user || !realtime) return
realtime.join(`user:${user.id}`)
realtime.onCollection('notifications', (data) => {
console.log(data.data)
setMessage(data.data.message)
})
}, [user, realtime])
return (
<div>
<div>
{user?.email}
</div>
<div>
{message}
</div>
</div>
)
}
useRealtime(userId: string, token?: string): PayloadRealtimeClientA React hook that initializes a real-time client, identifies the user for presence tracking, and returns a PayloadRealtimeClient instance.
Use this in components to manage socket lifecycle automatically:
const realtime = useRealtime(user.id, user.token)
PayloadRealtimeClientYou can also instantiate the client manually:
import { PayloadRealtimeClient } from '@alejotoro-o/payload-real-time'
const client = new PayloadRealtimeClient({ url, token })
type RealtimeClientOptions = {
url?: string // WebSocket server URL (default: http://localhost:3001)
token?: string // Optional JWT for authentication
}
| Method | Description |
|---|---|
identify(userId: string) | Identifies the user and registers them for presence tracking. Should be called once after connecting. |
join(roomId: string) | Joins a specific room (e.g. user:123, chat:abc). Enables receiving events scoped to that room. |
leave(roomId: string) | Leaves a previously joined room. Useful for cleaning up listeners when navigating away. |
onCollection(slug: string, callback: (data) => void) | Subscribes to real-time events for a specific collection. Events are emitted as realtime:{slug}. |
on(event: string, callback: (data) => void) | Subscribes to any custom event. Useful for chat messages, typing indicators, etc. |
emit(event: string, data: any) | Emits a custom event to the server. The payload should include a roomId if it's meant to be scoped. |
disconnect() | Gracefully disconnects the socket. Useful on logout or component unmount. |
client.emit('chat-message', {
roomId: 'chat:abc123',
content: 'Hello world!',
})
client.on('chat-message', (data) => {
console.log('New message:', data.content)
})
The plugin attaches the Socket.IO server instance to payload.io, allowing you to emit events from any hook or endpoint:
const io = (payload as any).io
io.to(`user:${userId}`).emit('custom-event', { message: 'Hello!' })
payload
Push publish, update and delete events from Payload to Hibira and refresh Next.js pages automatically
6814/wk0v3
cloud storage
Store Payload uploads in third-party cloud storage through a generic adapter interface
1001.0M/wk46.1kv3 v4
seo
Manage SEO metadata for your content from within the Payload admin panel
100346.5k/wk46.1kv3 v4
redirects
Manage internal and external redirects from the Payload admin panel
100200.1k/wk46.1kv3 v4
form builder
Let admin editors build and manage forms from within the Payload admin panel
100171.2k/wk46.1kv3 v4
multi tenant
Manage multiple tenants and tenant-scoped content from a single Payload admin panel
100165.4k/wk46.1kv3 v4