Frameworks
Angular
Angular
[Overview](../) · Framework Integrations
Angular applications can use the core @iconical/presence client. No dedicated Angular package is required. This example subscribes after browser initialization and closes the stream when Angular destroys the component.
Install
npm install @iconical/presenceStandalone Angular component
import {
Component, ChangeDetectorRef, DestroyRef,
Inject, OnInit, PLATFORM_ID,
} from "@angular/core";
import { isPlatformBrowser } from "@angular/common";
import { createPresenceClient, type PresenceResult } from "@iconical/presence";
@Component({
standalone: true,
selector: "app-discord-presence",
template: `
@if (error) {
<p>Presence unavailable</p>
} @else if (data) {
<h2>{{ data.profile.displayName }}</h2>
<p>{{ data.presence?.status ?? "Waiting for update" }}</p>
} @else {
<p>Connecting…</p>
}
`,
})
export class DiscordPresenceComponent implements OnInit {
data: PresenceResult | null = null;
error: Error | null = null;
constructor(
@Inject(PLATFORM_ID) private platformId: object,
private cdr: ChangeDetectorRef,
private destroyRef: DestroyRef,
) {}
ngOnInit(): void {
if (!isPlatformBrowser(this.platformId)) return;
const stop = createPresenceClient().subscribePresence(
"123456789012345678", // Replace with an approved ID.
{
onUpdate: (next) => {
this.data = next;
this.error = null;
this.cdr.markForCheck();
},
onError: (cause) => {
this.data = null;
this.error = cause;
this.cdr.markForCheck();
},
},
);
this.destroyRef.onDestroy(stop);
}
}NOTE
This example uses Angular's
@iftemplate syntax (Angular 17+). Older Angular projects can use*ngIfin their templates.
Avoid SSR subscriptions
Start live subscriptions in the browser and register an Angular destroy callback to release them.
How it works
The component starts a subscription only in the browser, avoiding a long-lived EventSource during SSR. onUpdate receives an approved PresenceResult; onError clears stale data. The DestroyRef cleanup prevents leaving a stream open when navigating away.
To fetch only once instead of subscribing, use createPresenceClient().getPresence(id) inside a suitable Angular service and handle the result as a promise.
See API reference, Getting started, and Troubleshooting.
Was this page helpful?