Quickstart
Quickstart Guide for pollz-js Node.js SDK
Welcome to the pollz-js Node.js SDK, a powerful tool for integrating Pollz functionality into your applications. This quickstart guide will walk you through the basic steps to get started with the SDK and quickly incorporate polling features into your Node.js project.
Installation
First, install the pollz-js SDK in your Node.js project using npm:
npm install pollz-js
Initialize the SDK
To use the pollz-js SDK, you need to initialize it with your application credentials. Obtain your appId
and appSecret
from the Pollz platform.
const { PollzSDK } = require('pollz-js');
// Initialize pollz-js SDK
const pollz = new PollzSDK();
const initInput = {
appId: 'your-app-id',
appSecret: 'your-app-secret',
};
try {
await pollz.init(initInput);
console.log('pollz-js SDK initialized successfully!');
} catch (error) {
console.error('Failed to initialize pollz-js SDK:', error.message);
}
Create a Poll
Now that the SDK is initialized, you can create a new poll with options.
const createPollInput = {
name: 'Favorite Programming Language',
options: ['JavaScript', 'Python', 'Java', 'TypeScript'],
pollTypeId: PollzSDK.PollTypes.MultipleChoice, // or PollzSDK.PollTypes.SingleChoice
};
try {
const createdPoll = await pollz.polls.create(createPollInput);
console.log('Poll created successfully:', createdPoll);
} catch (error) {
console.error('Failed to create poll:', error.message);
}
Vote in a Poll
Users can now cast their votes in the created poll.
const pollId = 1; // Replace with the ID of the created poll
const optionIds = [1, 2]; // Replace with the IDs of the chosen options
const userId = 'user-123'; // Replace with a unique user identifier
try {
await pollz.polls.vote(
PollzSDK.PollTypes.MultipleChoice,
pollId,
optionIds,
userId
);
console.log('Vote submitted successfully!');
} catch (error) {
console.error('Failed to submit vote:', error.message);
}
Get Poll Information
Retrieve information about a specific poll or a list of all polls.
// Get information about a specific poll
const pollInfo = await pollz.polls.get(1);
console.log('Poll Information:', pollInfo);
// Get a list of all polls
const allPolls = await pollz.polls.getAll();
console.log('All Polls:', allPolls.items);
Real-time Poll Updates
Listen for real-time updates to a specific poll. This is useful for scenarios where you want to keep your application updated with the latest poll data.
const unsubscribe = pollz.polls.listen(1, (updatedPoll) => {
console.log('Poll Updated:', updatedPoll);
});
// To stop listening
// unsubscribe();
Congratulations! You have completed the pollz-js Node.js SDK quickstart guide. Explore the SDK's comprehensive features and customize your integration to suit your application's needs. For detailed information on additional functionalities and options, refer to the full SDK documentation.
Last updated