Skip to content
Cloudflare Docs

Polls

This guide explains how to create, vote on, and interact with polls in a meeting using Cloudflare RealtimeKit.

Introduction

The meetings polls object can be accessed using meeting.polls. It provides methods to create polls, vote, and more.

JavaScript
console.log("Polls object:", meeting.polls);

The meeting.polls.items property returns an array of all polls created in a meeting, where each element is an object of type Poll.

JavaScript
console.log("All polls:", meeting.polls.items);

Poll Type

The Poll type is defined as follows:

TypeScript
interface Poll {
id: string;
question: string;
options: PollOption[];
anonymous: boolean;
hideVotes: boolean;
createdBy: string;
createdByUserId: string;
voted: string[]; // stores participant ID
}
interface PollOption {
text: string;
votes: {
id: string; // stores participant ID
name: string;
}[];
count: number;
}

Creating a Poll

A new poll can be created using the create method from the meeting.polls object. The meeting.polls.create() method accepts the following parameters:

  • question (string) - The poll question
  • options (string[]) - Array of poll options
  • anonymous (boolean) - Whether votes are anonymous
  • hideVotes (boolean, optional) - Whether to hide vote counts

The following snippet creates a poll where votes are anonymous:

JavaScript
await meeting.polls.create(
"Are you an early bird or a night owl?",
["Early bird", "Night owl"],
true,
);

Voting on a Poll

The meeting.polls.vote() method can be used to register a vote on a poll. It accepts the following parameters:

  • pollId (string) - The ID of the poll
  • optionIndex (number) - The index of the selected option

The following snippet votes for the first option on the first poll created in the meeting:

JavaScript
const poll = meeting.polls.items[0];
await meeting.polls.vote(poll.id, 0);

Other Poll Functions

View Poll Results

The total votes on a poll can be accessed in the following manner:

JavaScript
const poll = meeting.polls.items[0];
const votes = poll.voted;

votes is an array of participant IDs (meeting.participant.id).

The total votes on a poll option can be accessed in the following manner:

JavaScript
const poll = meeting.polls.items[0];
const options = poll.options;

options returns an array of objects, where each object is of type PollOption.

Get Notified When a Poll is Created or Updated

An event is fired each time meeting.polls.items is updated or created. You can listen for this to get the updated list of polls. The response object contains the following properties:

  • polls - List of all polls
  • newPoll - A boolean variable which is true when a new poll has been created
JavaScript
meeting.polls.on("pollsUpdate", ({ polls, newPoll }) => {
console.log("Polls updated:", polls);
console.log("Is new poll:", newPoll);
});