Get presence of a channel
One script reply has been approved by the moderators Verified

Get presence on a channel

Created by hugo697 138 days ago
Submitted by hugo697 Bun
Verified 138 days ago
1
//native
2
type Ably = {
3
  apiKey: string;
4
};
5
/**
6
 * Get presence of a channel
7
 * Get presence on a channel
8
 */
9
export async function main(
10
  auth: Ably,
11
  channel_id: string,
12
  format: "json" | "jsonp" | "msgpack" | "html" | undefined,
13
  clientId: string | undefined,
14
  connectionId: string | undefined,
15
  limit: string | undefined,
16
  X_Ably_Version: string,
17
) {
18
  const url = new URL(`https://rest.ably.io/channels/${channel_id}/presence`);
19
  for (const [k, v] of [
20
    ["format", format],
21
    ["clientId", clientId],
22
    ["connectionId", connectionId],
23
    ["limit", limit],
24
  ]) {
25
    if (v !== undefined && v !== "" && k !== undefined) {
26
      url.searchParams.append(k, v);
27
    }
28
  }
29
  const response = await fetch(url, {
30
    method: "GET",
31
    headers: {
32
      "X-Ably-Version": X_Ably_Version,
33
      Authorization: "Bearer " + auth.apiKey,
34
    },
35
    body: undefined,
36
  });
37
  if (!response.ok) {
38
    const text = await response.text();
39
    throw new Error(`${response.status} ${text}`);
40
  }
41
  return await response.json();
42
}
43