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.
- Web Components
- React
- Flutter
- React Native
- Android(Kotlin)
- iOS(Swift)
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:
Property | Description |
---|---|
id | Unique ID assigned to each poll |
question | Question of the poll |
options | Array of PollOption object, contains all the options to the poll question |
anonymous | To hide the votes of each user even after completion (false by default) |
hideVotes | Hide votes until the voting is complete (enabled if anonymous is enabled) |
createdBy | Name of creator of the poll |
createdByUserId | ID of creator of the poll |
voted | List of participant IDs who have voted on the poll |
The PollOption
object has the following properties:
Property | Description |
---|---|
text | Contains the option text |
votes | List of PollVote object, which contains the id and name of the voters of this option |
count | Int of number of votes given to this option |
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:
Property | Description |
---|---|
id | Unique ID assigned to each poll |
question | Question of the poll |
options | Array of PollOption object, contains all the options to the poll question |
anonymous | To hide the votes of each user even after completion (false by default) |
hideVotes | Hide votes until the voting is complete (enabled if anonymous is enabled) |
createdBy | Name of creator of the poll |
createdByUserId | ID of creator of the poll |
voted | List of participant IDs who have voted on the poll |
The PollOption
object has the following properties:
Property | Description |
---|---|
text | Contains the option text |
votes | List of PollVote object, which contains the id and name of the voters of this option |
count | Int of number of votes given to this option |
The meetings polls object can be accessed using dyteClient.polls
. dyteClient.polls.polls
returns an array of all polls created in a meeting, where each element is an object of type DytePollMessage.
The DytePollMessage
has the following properties:
Property | Description |
---|---|
id | Unique ID assigned to each poll |
question | Question of the poll |
anonymous | To hide the votes of each user even after completion (false by default) |
hideVotes | Hide votes until the voting is complete (enabled if anonymous is enabled) |
createdBy | Name of creator the poll |
options | Array of DytePollOption object, contains all the options to the poll question |
The DytePollOption
has the following properties:
Property | Description |
---|---|
text | Contains the option text |
votes | List of DytePollVote object, which contains info about voters of this option |
count | Int of number of votes given to this option |
The DytePollVote
has the following properties:
Property | Description |
---|---|
id | ID of the voter |
name | Name of the voter |
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:
Property | Description |
---|---|
id | Unique ID assigned to each poll |
question | Question of the poll |
options | Array of PollOption object, contains all the options to the poll question |
anonymous | To hide the votes of each user even after completion (false by default) |
hideVotes | Hide votes until the voting is complete (enabled if anonymous is enabled) |
createdBy | Name of creator of the poll |
createdByUserId | ID of creator of the poll |
voted | List of participant IDs who have voted on the poll |
The PollOption
object has the following properties:
Property | Description |
---|---|
text | Contains the option text |
votes | List of PollVote object, which contains the id and name of the voters of this option |
count | Int of number of votes given to this option |
meeting.polls.polls
returns an array of all polls created in a meeting, where
each element is an object of type DytePollMessage
.
The type DytePollMessage
is the main class for any poll in Dyte. It also
contains list of DytePollOption
which are options for a given poll. And every
DytePollOption
has list of votes inside of it. Votes are objects of class
DytePollVote
which internally has id and name of the vote.
One can easily create, vote and view polls by listening to callbacks on
meeting
object.
meeting.polls.polls
returns an array of all polls created in a meeting, where each element is an object of type DytePollMessage
The type DytePollMessage
is the main class for any poll in Dyte. It also contains list of DytePollOption
which are options for a given poll. And every DytePollOption
has list of votes inside of it. Votes are objects of class DytePollVote which internally has id and name of the vote.
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:
Param | Type | Default Value | Required | Description |
---|---|---|---|---|
question | string | - | yes | The question that is to be voted for. |
options | string[] | - | yes | The options of the poll. |
anonymous | boolean | false | no | If true, the poll votes are anonymous. |
hideVotes | boolean | false | no | If true, the votes on the poll are hidden. |
Check the implementation in your desired framework.
- Web Components
- React
- Flutter
- React Native
- Android(Kotlin)
- iOS(Swift)
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.
await meeting.poll.create(
'Are you an early bird or a night owl?',
['Early bird', 'Night owl'],
true
);
dyteClient.polls.create(
question: "Are you an early bird or a night owl?",
options: ["Early bird", "Night owl"],
anonymous: true,
hideVotes: false,
);
await meeting.poll.create(
'Are you an early bird or a night owl?',
['Early bird', 'Night owl'],
true
);
meeting.poll.create(
"Are you an early bird or a night owl?",
listOf("Early bird", "Night owl"),
true,
false
)
meeting.polls.create(
question: "Are you an early bird or a night owl?",
options: ["Early bird", "Night owl"],
anonymous: true,
hideVotes: false
)
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:
Param | Type | Default Value | Required | Description |
---|---|---|---|---|
id | string | - | yes | The ID of the poll that is to be voted on. |
index | number | - | yes | The index of the option. |
- Web Components
- React
- Flutter
- React Native
- Android(Kotlin)
- iOS(Swift)
const poll = meeting.polls.items[0];
await meeting.poll.vote(poll.id, 0);
const poll = meeting.polls.items[0];
await meeting.poll.vote(poll.id, 0);
final poll = dyteClient.polls.polls[0];
final selectedPollOption = poll.options[0];
dyteClient.polls.vote(poll, selectedPollOption);
const poll = meeting.polls.items[0];
await meeting.poll.vote(poll.id, 0);
val poll = meeting.polls.items[0]
val selectedPollOption = poll.options[0]
meeting.poll.vote(poll, selectedPollOption)
let poll = meeting.polls.items.first
let selectedPollOption = poll.options.first
meeting.polls.vote(pollMessage: poll, pollOption: selectedPollOption)
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.
- Web Components
- React
- Flutter
- React Native
- Android(Kotlin)
- iOS(Swift)
const poll = meeting.polls.items[0];
const votes = poll.voted;
const poll = meeting.polls.items[0];
const votes = poll.voted;
final poll = dyteClient.polls.polls[0];
let votes = 0;
for (final option in poll.options) {
votes += option.count
}
const poll = meeting.polls.items[0];
const votes = poll.voted;
val poll = meeting.polls.items[0]
val votes = 0;
for (option in poll.options){
votes += option.votes.siz;
}
let poll = meeting.polls.items.first
let votes = 0;
for option in poll.options {
votes += option.votes.count
}
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.
- Web Components
- React
- Flutter
- React Native
- Android(Kotlin)
- iOS(Swift)
const poll = meeting.polls.items[0];
const options = poll.options;
options
returns an array of objects, where each object is of type PollObject
.
const poll = meeting.polls.items[0];
const options = poll.options;
final poll = dyteClient.polls.polls[0];
final options = poll.option;
const poll = meeting.polls.items[0];
const options = poll.options;
val poll = meeting.polls.items[0]
val options = poll.options;
let poll = meeting.polls.items.first;
let options = poll.option;
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.
- Web Components
- React
- Flutter
- React Native
- Android(Kotlin)
- iOS(Swift)
meeting.polls.on('pollsUpdate', ({ polls, newPoll }) => {
console.log(polls, newPoll);
});
meeting.polls.on('pollsUpdate', ({ polls, newPoll }) => {
console.log(polls, newPoll);
});
To be able to receive new poll messages you need to implement a method onPollUpdates() method from callback DytePollEventsListener
:
To get poll updates, listen to onPollUpdates()
callback:
class PollEventsListeners extends DytePollEventsListener {
...
void onPollUpdates(List<DytePollMessage> polls) {
/// code to handle polls
}
void onNewPoll(DytePollMessage poll) {
/// code to handle new poll
}
...
}
You can subscribe to this events by addPollEventsListener
method:
dyteClient.addPollEventsListener(PollEventsListeners());
meeting.polls.on('pollsUpdate', ({ polls, newPoll }) => {
console.log(polls, newPoll);
});
To be able to receive new poll messages you need to implement a method
onPollUpdates()
method from callback DyteMeetingRoomEventsListener
. You can
subscribe to this events by calling
meeting.addMeetingEventsListener(dyteMeetingRoomEventsListener)
meeting.addMeetingRoomEventsListener(object :
DyteMeetingRoomEventsListener {
override fun onNewPoll(poll: DytePollMessage) {
super.onNewPoll(poll)
// code to handle new poll
}
override fun onPollUpdates(pollMessages: List<DytePollMessage>) {
super.onPollUpdates(pollMessages)
// code to handle polls and their vote updates.
}
})
To receive new poll messages, you must implement the onPollUpdates()
method from the callback DytePollEventsListener
. You can subscribe to this event by using the meeting.addMeetingEventsListener(dytePollEventsListener)
method.
extension MeetingViewModel: DytePollEventsListener {
func onNewPoll(poll: DytePollMessage) {
// code to handle new poll
}
func onPollUpdates(pollMessages: [DytePollMessage]) {
// code to handle polls and their vote updates
}
}