Skip to main content

Quickstart

Everything you will need to get up and running in 5 mins or less!

Installation

// Install Libraries
npm install @pushprotocol/restapi@latest ethers@^5.7

Import libraries

// Import restapi for function calls
import { PushAPI, CONSTANTS } from '@pushprotocol/restapi';

// Ethers or Viem, both are supported
import { ethers } from 'ethers';

Initialize User

// Creating a random signer from a wallet, ideally this is the wallet you will connect
const signer = ethers.Wallet.createRandom();

// Initialize wallet user, use 'PROD' instead of 'STAGING' for mainnet apps
const userAlice = await PushAPI.initialize(signer, {
env: CONSTANTS.ENV.STAGING,
});

Setup Video Stream

// Initialize Stream
const stream = await userAlice.initStream([CONSTANTS.STREAM.VIDEO]);

// Configure stream listen events and what to do
stream.on(CONSTANTS.STREAM.VIDEO, (data) => {
if (data.event === CONSTANTS.VIDEO.EVENT.REQUEST) {
// handle call request
}

if (data.event === CONSTANTS.VIDEO.EVENT.APPROVE) {
// handle call approve
}

if (data.event === CONSTANTS.VIDEO.EVENT.DENY) {
// handle call denied
}

if (data.event === CONSTANTS.VIDEO.EVENT.CONNECT) {
// handle call connected
}

if (data.event === CONSTANTS.VIDEO.EVENT.DISCONNECT) {
// handle call disconnected
}
});

// Connect Stream
stream.connect();

Setup Video State

// data will store the video call state, we will only use this to read the video call state
// and update the UI accordingly
let data = CONSTANTS.VIDEO.INITIAL_DATA;

// setData is a function that will update the video call state
const setData = (fn) => {
data = fn(data);
};

Initialize Video API

  // Initialising the video API
const aliceVideoCall = await userAlice.video.initialize(setData, {
stream: stream,
config: {
video: true, // to enable video on start
audio: true, // to enable audio on start
},
media?: MediaStream, // to pass your existing media stream(only for backend use)
});

Request Video Call

// request a call to a recipient wallet address
await aliceVideoCall.request([recipientWallet]); // see supported wallet standards - https://push.org/docs/video/supported-wallet-standards

Manage Video Call

// Approve the request, you can also pass the address that we received from the stream listener.
// aliceVideoCall.approve(address?);
await aliceVideoCall.approve();

// or deny the request, you can also pass the address that we received from the stream listener.
// aliceVideoCall.deny(address?);
await aliceVideoCall.deny();

// to disconnect the call
await aliceVideoCall.disconnect();

// to toggle local audio or video
aliceVideoCall.config({ audio: true, video: false });