All documentation
Getting Started

Full JavaScript SDK Example

A full PulseGrid JavaScript example showing setup, token creation, connection, presence, send, and publish.

JavaScript

Overview

This is the best JavaScript copy-paste starting point for backend developers using the PulseGrid npm package.

Setup

1. Create PulseGrid client 2. Test auth 3. Create client token 4. Connect to channel 5. Send websocket message 6. Publish HTTP message 7. Check presence

Code example

Full JavaScript SDK Example
import { createPulseGrid } from "pulsegrid";

const pulsegrid = createPulseGrid({
  baseUrl: process.env.PULSEGRID_BASE_URL,
  publicKey: process.env.PULSEGRID_PUBLIC_KEY,
  secretKey: process.env.PULSEGRID_SECRET_KEY,
  projectId: process.env.PULSEGRID_PROJECT_ID,
});

const token = await pulsegrid.tokens.create({
  identifierId: "external_js_tester_1",
  identifierLabel: "External JS Tester",
  allowedChannels: ["demo-channel"],
  expiresIn: 3600,
});

const channel = pulsegrid.channel("demo-channel");

console.log(await channel.presence());
console.log(await channel.onlineCount());

const chat = await channel.connect({
  clientToken: token,
  onOpen: () => console.log("Connected"),
  onMessage: (payload) => console.log(payload),
});

chat.send("Hello from JavaScript");
chat.requestPresenceSnapshot();

await channel.publish("Hello from server", {
  identifierId: "server_bot",
  identifierLabel: "Server Bot",
});

Notes

This is the best page to start from if you want a full working example quickly.