Realtime
The flarum/realtime extension provides a self-hosted WebSocket daemon and an extender API that lets any other extension push live updates to connected users — without flarum/realtime needing to know about those extensions at all.
This page covers how to integrate your extension with Realtime. For installation and server configuration, see the realtime README.
Realtime is an optional extension. All integration code must be guarded so your extension continues to work when Realtime is not installed. The patterns shown on this page demonstrate the correct way to do this.
How it works
When a backend event fires (e.g. a post is liked), Realtime:
- Looks up which users are currently connected via the WebSocket daemon.
- For each relevant connected user, generates a personalised JSON:API payload by calling the internal Flarum API as that user.
- Pushes the payload to that user's private Pusher channel.
On the frontend, Pusher delivers the payload. The browser calls app.store.pushPayload() to merge the data into the local store, and your callback decides what to update in the UI.
Backend integration
The Realtime extender
Register your integration in extend.php using the Flarum\Realtime\Extend\Realtime extender, wrapped in a Conditional so it only runs when Realtime is enabled:
use Flarum\Extend;
use Flarum\Realtime\Extend\Realtime as RealtimeExtend;
return [
(new Extend\Conditional())
->whenExtensionEnabled('flarum-realtime', fn () => [
(new RealtimeExtend())
->broadcastModelEvent(/* ... */),
]),
];
Broadcasting model events
Use broadcastModelEvent to push a JSON:API payload of a model whenever certain backend events fire.
(new RealtimeExtend())
->broadcastModelEvent(
// One or more backend event class names that should trigger the broadcast.
[\Flarum\Likes\Event\PostWasLiked::class, \Flarum\Likes\Event\PostWasUnliked::class],
// Return the model whose JSON:API representation should be pushed.
// Supported out of the box: Discussion, Post (wraps in discussion payload),
// User, Notification.
fn ($event) => $event->post,
// (optional) Return the User who caused the event.
// That user is excluded from the broadcast — they already know what happened.
fn ($event) => $event->user,
// (optional) Override the Pusher event name sent to JS clients.
// Defaults to the fully-qualified PHP class name of the first event.
'likesMutation'
),
What gets pushed: Realtime calls the internal API as each recipient (GET /api/posts/{id} in this example) and pushes the resulting JSON:API document. For Post models, the owning discussion is used as the primary resource with the post in included.
Choosing which model to return: The getModel callback should return a model that already has a JSON:API endpoint — one that renders everything the frontend needs to see the change. For events that mutate a post (likes, reactions, votes), return $event->post rather than the pivot/reaction model itself. The post's endpoint already serialises its aggregated state (like counts, reaction counts etc.) and the frontend can refresh its display from that. Only return a custom model class if you have a dedicated API endpoint for it and the frontend needs to render new data from that resource directly.
Custom model types: If you are broadcasting a model that isn't a core Discussion, Post, User, or Notification, register its API endpoint:
(new RealtimeExtend())
->broadcastModelEvent(
MyModel\Event\Created::class,
fn ($event) => $event->model,
)
->registerModelEndpoint(\My\Extension\MyModel::class, 'my-models'),
Broadcasting dialog message events
Use broadcastDialogEvent to push a message to all connected participants of a private dialog (requires flarum/messages):
(new RealtimeExtend())
->broadcastDialogEvent(
\Flarum\Messages\DialogMessage\Event\Created::class,
fn ($event) => $event->message,
)
->registerModelEndpoint(\Flarum\Messages\DialogMessage::class, 'dialog-messages')
->registerModelEndpoint(\Flarum\Messages\Dialog::class, 'dialogs'),
The payload is sent only to connected users who are members of the dialog.
Broadcasting flag events
Use broadcastFlagEvent to push flag-related updates exclusively to connected users who have the discussion.viewFlags permission on the relevant discussion:
(new RealtimeExtend())
->broadcastFlagEvent(
[\Flarum\Flags\Event\Created::class, \Flarum\Flags\Event\Deleting::class],
// Return the Discussion the flag belongs to.
fn ($event) => $event->flag->post->discussion,
// Pusher event name.
'flagged'
),
Registering model endpoints
If you are broadcasting a model type that Realtime does not know about by default, tell it which API endpoint to use for payload generation:
(new RealtimeExtend())
->registerModelEndpoint(\My\Extension\MyModel::class, 'my-models'),
Realtime will call GET /api/my-models/{id} (as the recipient user) to generate the payload.
Frontend integration
The JS Realtime extender
Import the extender using the ext: prefix, which resolves cross-extension modules at runtime. Guard the whole block with an extension check so your extension still loads when Realtime is absent.
The recommended pattern is to move the integration into a separate extendRealtime.ts file and call it conditionally from your index.ts:
// forum/extendRealtime.ts
import app from 'flarum/forum/app';
import RealtimeExtend from 'ext:flarum/realtime/forum/extenders/Realtime';
export default function extendRealtime() {
new RealtimeExtend()
.onDiscussionStreamEvent('likesMutation')
.extend(app, { name: 'my-extension', exports: {} });
}
// forum/index.ts
app.initializers.add('my-extension', () => {
if ('flarum-realtime' in flarum.extensions) {
extendRealtime();
}
});
The ext:flarum/realtime/... import prefix tells the Flarum module loader to resolve the module from the flarum-realtime extension's registered exports. This is the correct way to import from another extension — it avoids bundling the other extension's code into yours and ensures the module isn't loaded at all when Realtime is not installed.
See the extending extensions guide for details.
Triggering a discussion stream reload
When a Pusher event should cause DiscussionPage to reload the post stream (e.g. a new post, a like, a lock), use onDiscussionStreamEvent:
new RealtimeExtend()
.onDiscussionStreamEvent('likesMutation')
The event name here is the Pusher event name — what you set as $eventName in the PHP broadcastModelEvent call. When this event fires on either the public or user channel while DiscussionPage is open, the post stream refreshes.
Listening to user channel events
For events pushed only to the currently logged-in user's private channel:
new RealtimeExtend()
.onUserChannelEvent('my-extension.somethingHappened', (data: unknown) => {
const model = app.store.pushPayload(data as any);
// update UI as needed
m.redraw();
})
Listening to public channel events
For events visible to all connected users including guests:
new RealtimeExtend()
.onPublicChannelEvent('my-extension.publicEvent', (data: unknown) => {
app.store.pushPayload(data as any);
m.redraw();
})
Listening to both channels
When the same event can arrive on either channel (e.g. for both logged-in and guest users):
new RealtimeExtend()
.onBothChannelsEvent('Flarum\\Discussion\\Event\\Started', (data: unknown) => {
app.store.pushPayload(data as any);
})