The Polls feature provides a comprehensive API that enables seamless integration of polling capabilities within your application, enhancing engagement and interaction among users. Through this API, developers can effortlessly create, manage, and utilize polls as part of messages, gather user opinions, and make informed decisions based on real-time feedback.
Easy Poll Creation and Configuration: Developers can create polls with customizable options, descriptions, and configurations, including setting voting visibility (public or anonymous), enforcing unique votes, and specifying the maximum votes a user can cast. Polls can also be designed to allow user-suggested options or open-ended answers, providing flexibility in how you engage with your audience.
Seamless Integration with Activities: Once created, polls can be sent as part of activities, allowing for a seamless user experience. Users can view poll details and participate directly within the context of a conversation.
Dynamic Poll Management: Polls are not static. You can update poll details, add or modify options, and even close polls to further responses. These actions can be performed through full or partial updates, giving you control over the poll's lifecycle.
Robust Voting System: Users can cast votes on options or provide answers to open-ended questions, with the API supporting both single and multiple choice responses. Votes can be changed or removed, ensuring users' opinions are accurately captured.
Comprehensive Query Capabilities: Retrieve detailed information about polls and votes based on various criteria, including poll status, creation time, and user responses. This enables developers to implement rich, data-driven features in their applications.
Customizability and Extensibility: In addition to predefined poll properties, the API supports custom properties, allowing developers to tailor polls and options to their specific needs while maintaining performance and scalability.
Creating a poll is easy. You simply create a poll with your desired configuration, and once created, you post an activity with the poll id.
// Create a polllet poll = try await feed.createPoll( request: CreatePollRequest( name: "Where should we host our next company event?", options: [ PollOptionInput(text: "Amsterdam, The Netherlands"), PollOptionInput(text: "Boulder, CO") ] ), activityType: "poll")// The poll is automatically added as an activity to the feedprint("Poll created with ID: \(poll.id)")
// Create a pollval poll: Result<ActivityData> = feed.createPoll( request = CreatePollRequest( name = "Where should we host our next company event?", options = listOf( PollOptionInput(text = "Amsterdam, The Netherlands"), PollOptionInput(text = "Boulder, CO") ) ), activityType = "poll")// The poll is automatically added as an activity to the feedprintln("Poll created with ID: ${poll.getOrNull()?.id}")
const response = await client.createPoll({ name: "Where should we host our next company event?", options: [{ text: "Amsterdam, The Netherlands" }, { text: "Boulder, CO" }],});// Attach poll to an activityawait feed.addActivity({ type: "post", poll_id: response.poll.id,});
const response = await client.createPoll({ name: "Where should we host our next company event?", options: [{ text: "Amsterdam, The Netherlands" }, { text: "Boulder, CO" }],});// Attach poll to an activityawait feed.addActivity({ type: "post", poll_id: response.poll.id,});
const response = await client.createPoll({ name: "Where should we host our next company event?", options: [{ text: "Amsterdam, The Netherlands" }, { text: "Boulder, CO" }],});// Attach poll to an activityawait feed.addActivity({ type: "post", poll_id: response.poll.id,});
// Create a pollfinal poll = await feed.createPoll( request: const CreatePollRequest( name: 'Where should we host our next company event?', options: [ PollOptionInput(text: 'Amsterdam, The Netherlands'), PollOptionInput(text: 'Boulder, CO'), ], ), activityType: 'poll',);// The poll is automatically added as an activity to the feedprint('Poll created with ID: ${poll.getOrThrow().id}');
const response = await client.createPoll({ name: "Where should we host our next company event?", options: [{ text: "Amsterdam, The Netherlands" }, { text: "Boulder, CO" }], user_id: "<user id>",});// Attach poll to an activityawait client.feeds.addActivity({ fids: [feed.fid], type: "post", poll_id: response.poll.id, user_id: "<user id>",});
ctx := context.Background()// Create a pollpollResponse, err := client.CreatePoll(ctx, &getstream.CreatePollRequest{ Name: "Where should we host our next company event?", UserID: getstream.PtrTo("john"), Options: []getstream.PollOptionInput{ {Text: getstream.PtrTo("Amsterdam, The Netherlands")}, {Text: getstream.PtrTo("Boulder, CO")}, },})if err != nil { log.Fatal("Error creating poll:", err)}// Attach poll to an activityactivityResponse, err := client.Feeds().AddActivity(ctx, &getstream.AddActivityRequest{ Type: "post", PollID: getstream.PtrTo(pollResponse.Data.Poll.ID), UserID: getstream.PtrTo("john"), Feeds: []string{"user:john"},})if err != nil { log.Fatal("Error adding activity:", err)}
# Create a pollpoll_request = GetStream::Generated::Models::CreatePollRequest.new( name: 'Where should we host our next company event?', user_id: 'user123', options: [ GetStream::Generated::Models::PollOptionInput.new( text: 'Amsterdam, The Netherlands' ), GetStream::Generated::Models::PollOptionInput.new( text: 'Boulder, CO' ) ])poll_response = client.common.create_poll(poll_request)# Attach poll to an activityactivity_request = GetStream::Generated::Models::AddActivityRequest.new( type: 'post', poll_id: poll_response.poll.id, user_id: 'user123', feeds: ['user:user123'])activity_response = client.feeds.add_activity(activity_request)
import io.getstream.services.CommonImpl;import io.getstream.models.*;CommonImpl common = new CommonImpl(new StreamHTTPClient("<API key>", "<API secret>"));// Create a pollCreatePollRequest pollRequest = CreatePollRequest.builder() .name("Where should we host our next company event?") .userID("user123") .options(List.of( PollOptionInput.builder().text("Amsterdam, The Netherlands").build(), PollOptionInput.builder().text("Boulder, CO").build() )) .build();PollResponse pollResponse = common.createPoll(pollRequest).execute().getData();String pollId = pollResponse.getPoll().getId();// Attach poll to an activityAddActivityRequest activityRequest = AddActivityRequest.builder() .type("post") .pollID(pollId) .userID("user123") .feeds(List.of("user:user123")) .build();AddActivityResponse activityResponse = feedsClient.addActivity(activityRequest).execute().getData();
// Create a pollvar pollRequest = new CreatePollRequest{ Name = "Where should we host our next company event?", UserID = "user123", Options = new List<PollOptionInput> { new() { Text = "Amsterdam, The Netherlands" }, new() { Text = "Boulder, CO" } }};var pollResponse = await _client.CreatePollAsync(pollRequest);var pollId = pollResponse.Data.Poll.ID;// Attach poll to an activityvar activityRequest = new AddActivityRequest{ Type = "post", PollID = pollId, UserID = "user123", Feeds = new List<string> { "user:user123" }};var activityResponse = await _feedsV3Client.AddActivityAsync(activityRequest);
# Create a pollpoll = { "name": "Where should we host our next company event?", "user_id": "user123", "options": [ {"text": "Amsterdam, The Netherlands"}, {"text": "Boulder, CO"} ]}poll_response = client.create_poll(**poll)poll_id = poll_response.data.get("id")# Attach poll to an activityactivity_response = client.feeds.add_activity( type="post", poll_id=poll_id, user_id="user123", feeds=["user:user123"])
// Create a poll$pollRequest = new GeneratedModels\CreatePollRequest( name: 'Where should we host our next company event?', userID: 'user123', options: [ new GeneratedModels\PollOptionInput(text: 'Amsterdam, The Netherlands'), new GeneratedModels\PollOptionInput(text: 'Boulder, CO') ]);$pollResponse = $client->createPoll($pollRequest);$pollData = $pollResponse->getData();$pollId = $pollData->poll->id;// Attach poll to an activity$activityRequest = new GeneratedModels\AddActivityRequest( type: 'post', pollID: $pollId, userID: 'user123', feeds: ['user:user123']);$activityResponse = $feedsClient->addActivity($activityRequest);
Please take into account that the poll can be sent only by the user who created it in the first place.
It is also possible to supply your own custom properties for both polls and options:
let poll = try await feed.createPoll( request: CreatePollRequest( custom: ["category": "event_planning", "priority": "high"], name: "Where should we host our next company event?", options: [ PollOptionInput( custom: ["country": "Netherlands", "timezone": "CET"], text: "Amsterdam, The Netherlands" ), PollOptionInput( custom: ["country": "USA", "timezone": "MST"], text: "Boulder, CO" ) ] ), activityType: "poll")
val request = CreatePollRequest( custom = mapOf("category" to "event_planning", "priority" to "high"), name = "Where should we host our next company event?", options = listOf( PollOptionInput( custom = mapOf("country" to "Netherlands", "timezone" to "CET"), text = "Amsterdam, The Netherlands" ), PollOptionInput( custom = mapOf("country" to "USA", "timezone" to "MST"), text = "Boulder, CO" ) ))val poll: Result<ActivityData> = feed.createPoll( request = request, activityType = "poll")
await client.createPoll({ name: "Where should we host our next company event?", options: [ { text: "Amsterdam, The Netherlands", custom: { country: "Netherlands", timezone: "CET", }, }, { text: "Boulder, CO", custom: { country: "United States", timezone: "MST", }, }, ], custom: { category: "event_planning", priority: "high", },});
await client.createPoll({ name: "Where should we host our next company event?", options: [ { text: "Amsterdam, The Netherlands", custom: { country: "Netherlands", timezone: "CET", }, }, { text: "Boulder, CO", custom: { country: "United States", timezone: "MST", }, }, ], custom: { category: "event_planning", priority: "high", },});
await client.createPoll({ name: "Where should we host our next company event?", options: [ { text: "Amsterdam, The Netherlands", custom: { country: "Netherlands", timezone: "CET", }, }, { text: "Boulder, CO", custom: { country: "United States", timezone: "MST", }, }, ], custom: { category: "event_planning", priority: "high", },});
final poll = await feed.createPoll( request: const CreatePollRequest( custom: {'category': 'event_planning', 'priority': 'high'}, name: 'Where should we host our next company event?', options: [ PollOptionInput( custom: {'country': 'Netherlands', 'timezone': 'CET'}, text: 'Amsterdam, The Netherlands', ), PollOptionInput( custom: {'country': 'USA', 'timezone': 'MST'}, text: 'Boulder, CO', ), ], ), activityType: 'poll',);
await client.createPoll({ name: "Where should we host our next company event?", options: [ { text: "Amsterdam, The Netherlands", custom: { country: "Netherlands", timezone: "CET", }, }, { text: "Boulder, CO", custom: { country: "United States", timezone: "MST", }, }, ], custom: { category: "event_planning", priority: "high", }, user_id: "<user id>",});
# Close a poll for votingupdate_request = GetStream::Generated::Models::UpdatePollPartialRequest.new( user_id: 'user123', set: { is_closed: true })response = client.common.update_poll_partial('poll_456', update_request)
// Close a poll for voting$updateRequest = new GeneratedModels\UpdatePollPartialRequest( userID: 'user123', set: (object)[ 'is_closed' => true ]);$response = $client->updatePollPartial('poll_456', $updateRequest);
If you know the id of a poll you can easily retrieve the poll by using the getPoll method. If you don't know the id or if you want to retrieve multiple polls, use the query polls method (see below):
let poll = try await activity.getPoll(userId: "john")// userId is optional and can be provided for serverside calls// in case you want to include the votes for the user
val poll: Result<PollData> = activity.getPoll(userId = "john")// userId is optional and can be provided for serverside calls// in case you want to include the votes for the user
const poll = client.pollFromState(activity.poll?.id!);const unsubscribe = poll?.state.subscribe((state) => { // Called every time the poll state changes console.log(state);});// Call unsubscribe when you no longer want/need state updatesunsubscribe?.();
import { useCallback } from "react";const poll = client.pollFromState(activity.poll?.id);const selector = useCallback((state) => state ?? {}, []);const state = useStateStore(poll?.state, selector);// Called every time the poll state changesconsole.log(state);
import { useCallback } from "react";const poll = client.pollFromState(activity.poll?.id);const selector = useCallback((state) => state ?? {}, []);const state = useStateStore(poll?.state, selector);// Called every time the poll state changesconsole.log(state);
// Get poll from feed$response = $feedsClient->feed('user', 'user123')->getOrCreateFeed( new GeneratedModels\GetOrCreateFeedRequest(userID: 'user123'));$poll = $response->getData()->activities[0]->poll;echo json_encode($poll);// Or use poll id directly$response = $client->getPoll($pollId, 'user123');
let updatedPoll = try await activity.updatePoll( request: .init( id: "poll_456", name: "Where should we not go to?", options: [ PollOptionRequest( custom: ["reason": "too expensive"], id: "option_789", text: "Amsterdam, The Netherlands" ), PollOptionRequest( custom: ["reason": "too far"], id: "option_790", text: "Boulder, CO" ) ] ))
val request = UpdatePollRequest( id = "poll_456", name = "Where should we not go to?", options = listOf( PollOptionRequest( custom = mapOf("reason" to "too expensive"), id = "option_789", text = "Amsterdam, The Netherlands" ), PollOptionRequest( custom = mapOf("reason" to "too far"), id = "option_790", text = "Boulder, CO" ) ))val updatedPoll: Result<PollData> = activity.updatePoll( request = request)
await client.updatePoll({ id: "poll_456", name: "Where should we host our next company event?", options: [ { id: "option_789", text: "Amsterdam, The Netherlands", custom: { reason: "too expensive", }, }, { id: "option_123", text: "Boulder, CO", custom: { reason: "too far", }, }, ],});
await client.updatePoll({ id: "poll_456", name: "Where should we host our next company event?", options: [ { id: "option_789", text: "Amsterdam, The Netherlands", custom: { reason: "too expensive", }, }, { id: "option_123", text: "Boulder, CO", custom: { reason: "too far", }, }, ],});
await client.updatePoll({ id: "poll_456", name: "Where should we host our next company event?", options: [ { id: "option_789", text: "Amsterdam, The Netherlands", custom: { reason: "too expensive", }, }, { id: "option_123", text: "Boulder, CO", custom: { reason: "too far", }, }, ],});
final updatedPoll = await activity.updatePoll( const UpdatePollRequest( id: 'poll_456', name: 'Where should we not go to?', options: [ PollOptionRequest( custom: {'reason': 'too expensive'}, id: 'option_789', text: 'Amsterdam, The Netherlands', ), PollOptionRequest( custom: {'reason': 'too far'}, id: 'option_790', text: 'Boulder, CO', ), ], ), );
await client.updatePoll({ id: "poll_456", name: "Where should we host our next company event?", options: [ { id: "option_789", text: "Amsterdam, The Netherlands", custom: { reason: "too expensive", }, }, { id: "option_123", text: "Boulder, CO", custom: { reason: "too far", }, }, ], user_id: "<user id>",});
If allowUserSuggestedOptions is set to true on poll, then user only needs CastVote permission to access this endpoint. Otherwise user needs UpdatePoll permission.
val updatedPollOption = activity.updatePollOption( request = UpdatePollOptionRequest( custom = mapOf("my_custom_property" to "my_custom_value"), id = "option_789", text = "Updated option" ))