SDK di chat e API REST

La chat ti offre due modi per costruire, a seconda di dove gira il tuo codice:

  • L'SDK client viene eseguito nella tua app client. I clienti lo usano per connettersi, unirsi alle stanze, inviare e leggere messaggi e ascoltare gli eventi.
  • L'API REST gira sul tuo server. Usalo per compiti server e amministrativi, come gestire stanze, membri, utenti e ruoli.

Client SDK

L'SDK client JavaScript viene pubblicato come @azure/web-pubsub-chat-client pacchetto npm. Espone un ChatClient che si collega a un URL di accesso client, poi crea stanze, invia e legge messaggi, e genera eventi. Per una guida, vedi Inizia con la chat Azure Web PubSub.

Per l'API completa, il sorgente e i campioni, consulta il repository SDK su GitHub.

REST API

L'API REST di Azure Web PubSub chat data-plane gestisce le risorse di chat dal tuo server: stanze, membri, messaggi, utenti e ruoli. Non esiste ancora un client dedicato per Chat REST, quindi chiami questi endpoint con qualsiasi client HTTP.

L'API REST della chat vive sulla stessa rotta endpoint e hub della Web PubSub data-plane REST API ({endpoint}/api/hubs/{hub}/chat/...), e si autentica allo stesso modo di quell'API: con una chiave di accesso o un token Microsoft Entra ID. Per sapere come ottenere il token e firmare ogni richiesta, vedi Utilizzo dell'API REST.

Note

Per la specifica dettagliata dell'API REST, vedi la definizione dell'API REST della chat Web PubSub.

Con un token portatore di entrambi i metodi, si chiama un endpoint chat con qualsiasi client HTTP. Per esempio:

const endpoint = process.env.WebPubSubEndpoint; // https://contoso.webpubsub.azure.com
const hub = "chat";
const token = "<bearer token>"; // from Microsoft Entra ID or an access key, as described above

async function chatRequest(method, path, body) {
  const res = await fetch(`${endpoint}/api/hubs/${hub}/chat/${path}?api-version=2026-02-01-preview`, {
    method,
    headers: {
      Authorization: `Bearer ${token}`,
      "Content-Type": "application/json",
    },
    body: body ? JSON.stringify(body) : undefined,
  });
  if (!res.ok) {
    throw new Error(`Chat API ${method} ${path} failed: ${res.status}`);
  }
  return res.status === 204 ? undefined : res.json();
}

// Example: create a room, then add a member
await chatRequest("PUT", "rooms/falcon", { title: "Project Falcon" });
await chatRequest("PUT", "rooms/falcon/members/bob", { roleName: "room.member" });

Passaggi successivi