Skip to content

Latest commit

 

History

History
245 lines (178 loc) · 5.75 KB

02-Quickstart-Push-Chat.mdx

File metadata and controls

245 lines (178 loc) · 5.75 KB
id title hide_title slug displayed_sidebar sidebar_position image
docs-chat-quickstart
Quickstart
false
./quickstart
pushChatSidebar
2
/assets/docs/previews/docs_chat--quickstart.png

Quickstart

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

<title>{`Quickstart | Push Chat | Push Documentation`}</title>

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem';

Installation

// 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

// 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';

<TabItem value="react" attributes={{className: "codetab react"}} default>

// 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';

<TabItem value="reactnative" attributes={{className: "codetab reactnative"}} default>

// 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

// 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,
});

<TabItem value="react" attributes={{className: "codetab react"}} default>

// 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,
});

<TabItem value="reactnative" attributes={{className: "codetab reactnative"}} default>

// 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

// 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',
});

<TabItem value="react" attributes={{className: "codetab react"}} default>

// 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',
});

<TabItem value="reactnative" attributes={{className: "codetab reactnative"}} default>

// 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

// 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();

<TabItem value="react" attributes={{className: "codetab react"}} default>

// 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();

<TabItem value="reactnative" attributes={{className: "codetab reactnative"}} default>

// 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();