diff --git a/packages/examples/sdk-backend-node/chat/chat.ts b/packages/examples/sdk-backend-node/chat/chat.ts index 4c21f5a72..e1499985b 100644 --- a/packages/examples/sdk-backend-node/chat/chat.ts +++ b/packages/examples/sdk-backend-node/chat/chat.ts @@ -60,7 +60,7 @@ const eventlistener = async ( stream: PushStream, eventName: string ): Promise => { - stream.on(eventName, (data: any) => { + stream.on(eventName, (data: any) => { if (showAPIResponse) { console.log('Stream Event Received'); console.log(data); @@ -72,25 +72,33 @@ const eventlistener = async ( export const runChatClassUseCases = async (): Promise => { const userAlice = await PushAPI.initialize(signer, { env }); - const stream = await userAlice.initStream( - [CONSTANTS.STREAM.CHAT, CONSTANTS.STREAM.CHAT_OPS], - { - // stream supports other products as well, such as STREAM.CHAT, STREAM.CHAT_OPS - // more info can be found at push.org/docs/chat + const stream = await userAlice.initStream( + [CONSTANTS.STREAM.CHAT, CONSTANTS.STREAM.CHAT_OPS], + { + // stream supports other products as well, such as STREAM.CHAT, STREAM.CHAT_OPS + // more info can be found at push.org/docs/chat - filter: { - channels: ['*'], - chats: ['*'], - }, - connection: { - auto: true, // should connection be automatic, else need to call stream.connect(); - retries: 3, // number of retries in case of error - }, - raw: true, // enable true to show all data - } - ); - - await stream.connect(); + filter: { + channels: ['*'], + chats: ['*'], + }, + connection: { + auto: true, // should connection be automatic, else need to call stream.connect(); + retries: 3, // number of retries in case of error + }, + raw: true, // enable true to show all data + } + ); + + stream.on(CONSTANTS.STREAM.CONNECT, (a) => { + console.log('Stream Connected'); + }); + + await stream.connect(); + + stream.on(CONSTANTS.STREAM.DISCONNECT, () => { + console.log('Stream Disconnected'); + }); const userBob = await PushAPI.initialize(secondSigner, { env }); const userKate = await PushAPI.initialize(thirdSigner, { env }); @@ -106,7 +114,9 @@ export const runChatClassUseCases = async (): Promise => { // ------------------------------------------------------------------- console.log('PushAPI.chat.list'); const aliceChats = await userAlice.chat.list(CONSTANTS.CHAT.LIST_TYPE.CHATS); - const aliceRequests = await userAlice.chat.list(CONSTANTS.CHAT.LIST_TYPE.REQUESTS); + const aliceRequests = await userAlice.chat.list( + CONSTANTS.CHAT.LIST_TYPE.REQUESTS + ); if (showAPIResponse) { console.log(aliceChats); console.log(aliceRequests); diff --git a/packages/examples/sdk-backend-node/notification/notification.ts b/packages/examples/sdk-backend-node/notification/notification.ts index 00e8872c6..40d0f213e 100644 --- a/packages/examples/sdk-backend-node/notification/notification.ts +++ b/packages/examples/sdk-backend-node/notification/notification.ts @@ -45,19 +45,38 @@ export const runNotificationClassUseCases = async (): Promise => { // ------------------------------------------------------------------- const userAlice = await PushAPI.initialize(signer, { env }); - const stream = await userAlice.stream([CONSTANTS.STREAM.NOTIF], { - // stream supports other products as well, such as STREAM.CHAT, STREAM.CHAT_OPS - // more info can be found at push.org/docs/chat + const stream = await userAlice.initStream( + [ + CONSTANTS.STREAM.NOTIF, + CONSTANTS.STREAM.CHAT_OPS, + CONSTANTS.STREAM.CHAT, + CONSTANTS.STREAM.CONNECT, + CONSTANTS.STREAM.DISCONNECT, + ], + { + // stream supports other products as well, such as STREAM.CHAT, STREAM.CHAT_OPS + // more info can be found at push.org/docs/chat - filter: { - channels: ['*'], - chats: ['*'], - }, - connection: { - auto: true, // should connection be automatic, else need to call stream.connect(); - retries: 3, // number of retries in case of error - }, - raw: true, // enable true to show all data + filter: { + channels: ['*'], + chats: ['*'], + }, + connection: { + auto: true, // should connection be automatic, else need to call stream.connect(); + retries: 3, // number of retries in case of error + }, + raw: true, // enable true to show all data + } + ); + + stream.on(CONSTANTS.STREAM.CONNECT, (a) => { + console.log('Stream Connected'); + }); + + await stream.connect(); + + stream.on(CONSTANTS.STREAM.DISCONNECT, () => { + console.log('Stream Disconnected'); }); // Listen Stream Events for getting websocket events diff --git a/packages/examples/sdk-backend-node/pushAPI/stream.ts b/packages/examples/sdk-backend-node/pushAPI/stream.ts index 8b00ad1e2..36358115f 100644 --- a/packages/examples/sdk-backend-node/pushAPI/stream.ts +++ b/packages/examples/sdk-backend-node/pushAPI/stream.ts @@ -1,4 +1,4 @@ -import { PushAPI } from '@pushprotocol/restapi'; +import { CONSTANTS, PushAPI } from '@pushprotocol/restapi'; import { config } from '../config'; import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts'; import { createWalletClient, http } from 'viem'; @@ -36,7 +36,7 @@ const eventlistener = async ( pushAPI: PushAPI, eventName: string ): Promise => { - pushAPI._stream.on(eventName, (data: any) => { + pushAPI.stream.on(eventName, (data: any) => { if (showAPIResponse) { console.log(data); } @@ -47,11 +47,46 @@ const delay = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms)); export const runPushAPIStreamCases = async (): Promise => { const userAlice = await PushAPI.initialize(signer, { env }); + + const stream = await userAlice.initStream( + [ + CONSTANTS.STREAM.NOTIF, + CONSTANTS.STREAM.CHAT_OPS, + CONSTANTS.STREAM.CHAT, + CONSTANTS.STREAM.CONNECT, + CONSTANTS.STREAM.DISCONNECT, + ], + { + // stream supports other products as well, such as STREAM.CHAT, STREAM.CHAT_OPS + // more info can be found at push.org/docs/chat + + filter: { + channels: ['*'], + chats: ['*'], + }, + connection: { + auto: true, // should connection be automatic, else need to call stream.connect(); + retries: 3, // number of retries in case of error + }, + raw: true, // enable true to show all data + } + ); + + stream.on(CONSTANTS.STREAM.CONNECT, (a) => { + console.log('Stream Connected'); + }); + + await stream.connect(); + + stream.on(CONSTANTS.STREAM.DISCONNECT, () => { + console.log('Stream Disconnected'); + }); + const userBob = await PushAPI.initialize(secondSigner, { env }); const userKate = await PushAPI.initialize(thirdSigner, { env }); // ------------------------------------------------------------------- // ------------------------------------------------------------------- - console.log(`Listening ${STREAM.PROFILE} Events`); + console.log(`Listening ${CONSTANTS.STREAM.PROFILE} Events`); eventlistener(userAlice, STREAM.PROFILE); console.log(`Listening ${STREAM.ENCRYPTION} Events`); eventlistener(userAlice, STREAM.ENCRYPTION); diff --git a/packages/restapi/tests/lib/pushstream/initialize.test.ts b/packages/restapi/tests/lib/pushstream/initialize.test.ts index 9f761acfc..0e797f415 100644 --- a/packages/restapi/tests/lib/pushstream/initialize.test.ts +++ b/packages/restapi/tests/lib/pushstream/initialize.test.ts @@ -172,9 +172,9 @@ describe('PushStream.initialize functionality', () => { content: "Gm gm! It's a me... Mario", }); }); - + await stream.connect(); - + stream.on(CONSTANTS.STREAM.DISCONNECT, () => { console.log('Stream Disconnected'); });