📊
Pollz
  • Introduction
  • Quickstart
  • Guides
    • Initializing your app
    • Create a Poll
    • Vote for a Poll
    • Retrieve a Poll
    • Manage Poll Options
    • Listen to update
    • Anonymous Poll
  • Components
    • React
    • React Native
    • Embedded
  • API Reference
    • init()
    • pollOptions()
      • addOption()
      • deleteOption()
      • renameOption()
    • anonymous
      • createAnonymousToken()
      • getAnonymousPoll()
      • voteAnonymously()
    • polls
      • create()
      • get()
      • getAll()
      • vote()
      • rename()
      • deleteOne()
      • listen()
    • pollTypes
      • getAll()
  • Tooling
    • Typescript
    • React
      • usePoll()
      • usePolls()
      • usePollz()
      • usePollTypes()
      • useAnonymousPoll()
Powered by GitBook
On this page
  • Guide: Listening to Poll Updates with pollz-js SDK
  • Prerequisites
  • Listen to Poll Updates
  1. Guides

Listen to update

PreviousManage Poll OptionsNextAnonymous Poll

Last updated 1 year ago

Guide: Listening to Poll Updates with pollz-js SDK

Prerequisites

Before you proceed with listening to poll updates using the pollz-js SDK, ensure that you have already and by following the relevant guides in the "Guides" section.

Listen to Poll Updates

Stay informed about real-time changes in a specific poll by setting up a listener using the pollz-js SDK.

Step 1: Import the SDK

In your Node.js application, ensure you have already imported the pollz-js SDK:

const { PollzSDK } = require('pollz-js');

Step 2: Listen to Updates

Use the listen method provided by the polls module of the SDK to set up a listener for a specific poll. Provide the poll ID and a callback function to handle the updates.

const pollz = new PollzSDK();

// Replace 'your-app-id' and 'your-app-secret' with actual credentials
const initInput = {
  appId: 'your-app-id',
  appSecret: 'your-app-secret',
};

// Initialize the SDK
await pollz.init(initInput);

// Replace with the actual poll ID
const pollIdToListen = 1;

// Define a callback function to handle updates
const handlePollUpdate = (updatedPoll) => {
  console.log('Poll Updated:', updatedPoll);
};

// Set up the listener
const unsubscribe = pollz.polls.listen(pollIdToListen, handlePollUpdate);

// To stop listening, call the unsubscribe function
// unsubscribe();

Replace 'your-app-id' and 'your-app-secret' with the actual credentials obtained from the Pollz platform. Adjust the pollIdToListen according to the poll you want to receive updates for.

Congratulations! You have successfully set up a listener to receive real-time updates for a specific poll using the pollz-js SDK. Explore more SDK features and functionalities to enhance your application's polling experience. Refer to the relevant guides for detailed instructions on other SDK capabilities.

initialized the SDK
created a poll