All API Calls
This tutorial is designed to get you up and going by providing a step wise guide to use and explore the Push SDK.
Step 1 - Setup and Installation
Start by setting up the project environment and installing the SDK and its required peer dependencies ( ethers ).
** Note - ** For this tutorial we will be using plain Javascript
- js
# Create Project Directory
mkdir push-sdk-tutorial
# Navigate to Project Directory
cd push-sdk-tutorial
# Initialize Project
npm init -y
# Create index.mjs File
touch index.mjs
# Install Required Libraries
npm install @pushprotocol/restapi@latest @pushprotocol/socket@latest ethers@^5.7
Step 2 - Initialize User
Initialization is a crucial step before utilizing the SDK. It involves creating a Push Profile for new users or re-authenticating and decrypting keys for existing users.
- js
// index.mjs
import { PushAPI, CONSTANTS } from '@pushprotocol/restapi';
import { ethers } from 'ethers';
/**
* Private key of the user can be exported from any wallet such as Metamask
* Note - One should use a .env file to store the private key and make sure it is not committed to the repository
* Ideal code - const signer = new ethers.Wallet(`0x${prcoess.env.PRIVATE_KEY}`)
* For this tutorial we will be using a random private key
*/
const aliceSigner = new ethers.Wallet.createRandom();
const aliceAddress = await aliceSigner.getAddress();
const bobSigner = new ethers.Wallet.createRandom();
const bobAddress = await bobSigner.getAddress();
/** Initialization */
const userAlice = await PushAPI.initialize(aliceSigner, { env: 'prod' });
const userBob = await PushAPI.initialize(bobSigner, { env: 'prod' });
/** Get details of the initialized user */
const userAliceInfo = await userAlice.info();
Step 3 - Navigating User Profile Functionality
Push SDK provides getters and setters related to the user's profile, which can be utilized to display user profile information in your UI and enable users to change their profile information effortlessly.
- js
// index.mjs
...
...
/**
* User Profile Functions
*/
/** Get Profile Info such as name, image etc */
const userAliceProfileInfo = await userAlice.profile.info()
/** Set Profile Info such as name, image etc */
const updatedUserAliceProfileInfo = await userAlice.profile.update({
name: 'Alice',
})
Step 4 - Navigating Chat Functionality
Push SDK provides various functions for sending chat messages, receiving, accepting or rejecting chat invites
- js
// index.mjs
...
...
/**
* Push Chat Functions
*/
/** Send Chat message to another user */
// Note - This message will be sent as a request to the receiver who needs to accept or reject it
const aliceMessagesBob = await userAlice.chat.send(bobAddress, {
content: 'Gm Gm !!!',
type: 'Text',
})
/** Get list of all chats done by user */
const userAliceChats = await userAlice.chat.list('CHATS')
/** Get list of all chat requests received by user */
const userAliceRequests = await userAlice.chat.list('REQUESTS')
/** Get latest chat message of a specific chat */
const aliceLatestChatWithBob = await userAlice.chat.latest(bobAddress)
/** Get historical chat message of a specific chat */
const aliceHistoricalChatWithBob = await userAlice.chat.history(bobAddress)
/** Accept chat request */
const bobAcceptsAliceChatRequest = await userBob.chat.accept(aliceAddress)
/** Reject chat request */
// Setup
const tempSigner = new ethers.Wallet.createRandom()
const tempAddress = await tempSigner.getAddress()
const userTemp = await PushAPI.initialize(tempSigner, { env: 'prod' })
const tempMessageBob = await userTemp.chat.send(bobAddress, {
content: 'Click this link to get scammed',
type: 'Text',
})
// Reject
const bobRejectsChatRequest = await userBob.chat.reject(tempAddress)
/** Block User */
const aliceBlocksBob = await userAlice.chat.block([bobAddress])
/** UnBlock User */
const aliceUnblocksBob = await userAlice.chat.unblock([bobAddress])
Step 5 - Navigating Group Chat Functionality
Push SDK provides various functions for creation, updation , adding and removing users from group.
- js
// index.mjs
...
...
/**
* Push Group Chat Functions
*/
/** Create Push Chat Group */
// Note - By default group creator is the admin of the group
const createdGroup = await userAlice.chat.group.create('Sample Grp', {
description: 'Same Desc',
image: null,
members: [],
admins: [],
private: false,
})
const groupChatId = createdGroup.chatId
/** Update Group Info */
// Note - Only admin can update group info
const updatedGroup = await userAlice.chat.group.update(groupChatId, {
description: 'Updated Description',
})
/** Get Group Info */
const groupInfo = await userAlice.chat.group.info(groupChatId)
/** Get group Permissions */
const groupPermissions = await userAlice.chat.group.permissions(groupChatId)
/** Add Member to Group */
const addMember = await userAlice.chat.group.add(groupChatId, {
role: 'MEMBER',
accounts: ['0x754E2C9f31D7DB279E9d4A9140e33ad8839E1FAd'],
})
/** Add Admin to Group */
const addAdmin = await userAlice.chat.group.add(groupChatId, {
role: 'ADMIN',
accounts: [bobAddress],
})
/** Remove Member from Group */
const removeMember = await userAlice.chat.group.remove(groupChatId, {
role: 'MEMBER',
accounts: ['0x754E2C9f31D7DB279E9d4A9140e33ad8839E1FAd'],
})
/** Remove Admin from Group */
const removeAdmin = await userAlice.chat.group.remove(groupChatId, {
role: 'ADMIN',
accounts: [bobAddress],
})
/** Join Group */
// Note - A user can only join a group if its public or if the user is invited to join the group
const joinGrp = await userBob.chat.group.join(groupChatId)
/** Leave Group */
const leaveGrp = await userBob.chat.group.leave(groupChatId)
/** Reject a Group Joining Invite */
// Setup
const newGroup = await userAlice.chat.group.create('Tmp Grp', {
description: 'Tmp Desc',
image: null,
members: [bobAddress],
admins: [],
private: true,
})
// Reject
const rejectGrpJoiningReq = await userBob.chat.group.reject(newGroup.chatId)
Step 6 - Navigating User Encryption Functionality
Push SDK provides functions for getting info related to the encryption of a Push profile and also allow to change encryption of user keys
- js
// index.mjs
...
...
/**
* Push User Encryption Functions
*/
/** Get User profile encryption info */
const encryptionInfo = await userAlice.encryption.info()
/** Update encryption version of Push Profile */
// Note - This function changes the encryption of push chat encryption keys and need to be called cautiously
const PGP_V3 = 'eip191-aes256-gcm-hkdf-sha256'
const encryptionUpdate = await userAlice.encryption.update(PGP_V3)