Frameworks
Javascript
JavaScript, TypeScript, Astro and Node
[Overview](../) · Framework Integrations
Use the framework-independent client in any application with fetch. This guide shows how to request a one-time snapshot and how to update a browser page without React, Vue or Svelte.
Install
bun add @iconical/presenceSee Installation for npm, pnpm, Yarn and pre-release alternatives.
Fetch a snapshot
import { createPresenceClient } from "@iconical/presence";
const client = createPresenceClient();
const userId = "123456789012345678"; // Opted-in Discord user.
try {
const result = await client.getPresence(userId);
console.log(result.profile.displayName);
console.log(result.presence?.status ?? "Waiting for update");
} catch (error) {
console.error("Presence unavailable", error);
}presence: null indicates a missing current snapshot, not necessarily an offline account. A 404 deliberately does not reveal why a user could not be queried.
Browser lifecycle
Close the subscription when the page or widget is removed to avoid unnecessary network activity.
Render live updates in a browser
Add an element to a page:
<p id="discord-status" role="status">Connecting…</p>Then subscribe in your browser-side script:
import { createPresenceClient } from "@iconical/presence";
const element = document.querySelector<HTMLElement>("#discord-status");
const client = createPresenceClient();
const stop = client.subscribePresence("123456789012345678", {
onUpdate(result) {
if (!element) return;
element.textContent = result.availability === "pending"
? "Waiting for a Discord update"
: (result.presence?.status ?? "Unavailable");
},
onError(error) {
if (element) element.textContent = "Presence unavailable";
console.error(error);
},
});
window.addEventListener("pagehide", stop, { once: true });For a .js file, remove the TypeScript-specific annotation from querySelector. Use the cleanup function whenever the widget is removed without a full page navigation.
Astro
An Astro .astro page can render a placeholder, then subscribe in a browser or an interactive island. If using the SDK in build-time code, getPresence() gives a snapshot at build time — it does not create a live page.
Node.js and Bun
Node 18+ and Bun support fetch; use getPresence() for server jobs or one-time reads. When EventSource is unavailable, an auto subscription uses REST polling. Stop long-running subscriptions during process shutdown.
const controller = new AbortController();
const result = await createPresenceClient().getPresence(
"123456789012345678",
{ signal: controller.signal },
);Next steps
Use the React, Vue, or Svelte adapter for their lifecycle handling. See API reference for subscription options and errors.
Was this page helpful?