Skip to main content

Getting started with Polls

Poll objects exist within a meeting and can only be accessed through a meeting object. This object provides methods to create polls, vote, and more.

meeting.polls.items returns an array of all polls created in a meeting, where each element is an object of type Poll which is defined in the following manner.

The Poll object has the following properties:

PropertyDescription
idUnique ID assigned to each poll
questionQuestion of the poll
optionsArray of PollOption object, contains all the options to the poll question
anonymousTo hide the votes of each user even after completion (false by default)
hideVotesHide votes until the voting is complete (enabled if anonymous is enabled)
createdByName of creator of the poll
createdByUserIdID of creator of the poll
votedList of participant IDs who have voted on the poll

The PollOption object has the following properties:

PropertyDescription
textContains the option text
votesList of PollVote object, which contains the id and name of the voters of this option
countInt of number of votes given to this option

Creating a new Poll​

Using the Core SDK​

The Polls object gives a create function to create a new poll. It accepts the following parameters:

ParamTypeDefault ValueRequiredDescription
questionstring-yesThe question that is to be voted for.
optionsstring[]-yesThe options of the poll.
anonymousbooleanfalsenoIf true, the poll votes are anonymous.
hideVotesbooleanfalsenoIf true, the votes on the poll are hidden.

Check the implementation in your desired framework.

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

Here is a snippet of a poll where votes are anonymous.

Using the REST API​

You can also create a poll using the Create a Poll REST API. This can be used to create a new poll in an active meeting session. In the request, make sure to provide the meeting ID of the session.

Request​

Here is a sample request, make sure to replace the meeting ID and the authorization header with your own.

curl --request POST \
--url https://api.dyte.io/v2/meetings/meeting_id/active-session/poll \
--header 'Authorization: Basic undefined' \
--header 'Content-Type: application/json' \
--data '{
"question": "string",
"options": [
"string"
],
"anonymous": true,
"hide_votes": true
}'

Response​

{
"success": true,
"data": {
"action": "string",
"poll": {
"id": "string",
"question": "string",
"options": [
{
"text": "string",
"count": 0,
"votes": [
{
"id": "string",
"name": "string"
}
]
}
],
"anonymous": true,
"hide_votes": true,
"created_by": "string",
"voted": ["string"]
}
}
}

Voting on a Poll​

The Poll object also provides a vote method that can be used to register votes on a poll. It accepts the following parameters:

ParamTypeDefault ValueRequiredDescription
idstring-yesThe ID of the poll that is to be voted on.
indexnumber-yesThe index of the option.
const poll = meeting.polls.items[0];
await meeting.poll.vote(poll.id, 0);

View Poll Results​

The total votes on a poll can be accessed in the following manner where votes is an array of all participant ids.

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

To get total votes on a particular poll option, the list of all options can be fetched, through which you can get the number of votes on each option.

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

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

Get notified when a poll is created or updated​

An event is fired each time items in the Polls Objects are updated or created. User 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.
meeting.polls.on('pollsUpdate', ({ polls, newPoll }) => {
console.log(polls, newPoll);
});