Quickstart
Everything you will need to get up and running in 2 mins or less!
Installation
- js
- react
- reactnative
// Install Libraries
npm install @pushprotocol/restapi@latest @pushprotocol/socket@latest ethers@^5.7
// Install Libraries
npm install @pushprotocol/restapi@latest @pushprotocol/socket@latest ethers@^5.7
// Install Libraries
npm install @pushprotocol/restapi@latest @pushprotocol/socket@latest ethers@^5.7
Import libraries
- js
- react
- reactnative
// Import restapi for function calls
// Import socket for listening for real time messages
import { PushAPI, CONSTANTS } from '@pushprotocol/restapi';
// Ethers or Viem, both are supported
import { ethers } from 'ethers';
// Import restapi for function calls
// Import socket for listening for real time messages
import { PushAPI, CONSTANTS } from '@pushprotocol/restapi';
// Ethers or Viem, both are supported
import { ethers } from 'ethers';
// Import restapi for function calls
// Import socket for listening for real time messages
import { PushAPI, CONSTANTS } from '@pushprotocol/restapi';
// Ethers or Viem, both are supported
import { ethers } from 'ethers';
Initialize Chat
- js
- react
- reactnative
// Creating a random signer from a wallet, ideally this is the wallet you will connect
const signer = ethers.Wallet.createRandom();
// Initialize wallet user
// 'CONSTANTS.ENV.PROD' -> mainnet apps | 'CONSTANTS.ENV.STAGING' -> testnet apps
const userAlice = await PushAPI.initialize(signer, {
env: CONSTANTS.ENV.STAGING,
});
// Creating a random signer from a wallet, ideally this is the wallet you will connect
const signer = ethers.Wallet.createRandom();
// Initialize wallet user
// 'CONSTANTS.ENV.PROD' -> mainnet apps | 'CONSTANTS.ENV.STAGING' -> testnet apps
const userAlice = await PushAPI.initialize(signer, {
env: CONSTANTS.ENV.STAGING,
});
// Creating a random signer from a wallet, ideally this is the wallet you will connect
const signer = ethers.Wallet.createRandom();
// Initialize wallet user
// 'CONSTANTS.ENV.PROD' -> mainnet apps | 'CONSTANTS.ENV.STAGING' -> testnet apps
const userAlice = await PushAPI.initialize(signer, {
env: CONSTANTS.ENV.STAGING,
});
Send Message
- js
- react
- reactnative
// This will be the wallet address of the recipient
const toWalletAddress = ethers.Wallet.createRandom().address;
// Send a message to Bob
const aliceMessagesBob = await userAlice.chat.send(toWalletAddress, {
content: 'Hello Bob!',
type: 'Text',
});
// This will be the wallet address of the recipient
const toWalletAddress = ethers.Wallet.createRandom().address;
// Send a message to Bob
const aliceMessagesBob = await userAlice.chat.send(toWalletAddress, {
content: 'Hello Bob!',
type: 'Text',
});
// This will be the wallet address of the recipient
const bobWalletAddress = ethers.Wallet.createRandom().address;
// Send a message to Bob
const aliceMessagesBob = await userAlice.chat.send(bobWalletAddress, {
content: 'Hello Bob!',
type: 'Text',
});
Stream / Real time updates / Socket
- js
- react
- reactnative
// Initialize Stream
const stream = await userAlice.initStream([CONSTANTS.STREAM.CHAT]);
// Configure stream listen events and what to do
stream.on(CONSTANTS.STREAM.CHAT, (message) => {
console.log(message);
});
// Connect Stream
stream.connect();
// Initialize Stream
const stream = await userAlice.initStream([CONSTANTS.STREAM.CHAT]);
// Configure stream listen events and what to do
stream.on(CONSTANTS.STREAM.CHAT, (message) => {
console.log(message);
});
// Connect Stream
stream.connect();
// Initialize Stream
const stream = await userAlice.initStream([CONSTANTS.STREAM.CHAT]);
// Configure stream listen events and what to do
stream.on(CONSTANTS.STREAM.CHAT, (message) => {
console.log(message);
});
// Connect Stream
stream.connect();