Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Stream Chat docs with latest code and instructions #571

Closed
Gautam25Raj opened this issue Jan 7, 2024 · 0 comments
Closed

Update Stream Chat docs with latest code and instructions #571

Gautam25Raj opened this issue Jan 7, 2024 · 0 comments

Comments

@Gautam25Raj
Copy link
Contributor

Description:

The current code examples in Stream Chat documentation section within the Push Protocol repository appear to be outdated, and I'd like to contribute a revised version that aligns with the latest Stream Chat integration practices.

Specific areas for update:

  • Code snippets: The code examples in the docs should be replaced with the most up-to-date syntax and best practices for integrating Stream Chat with Push Protocol.

Proposed code changes:

// Initialize stream to listen for events:
const stream = await userAlice.initStream(
  [
    CONSTANTS.STREAM.CHAT, // Listen for chat messages
    CONSTANTS.STREAM.NOTIF, // Listen for notifications
    CONSTANTS.STREAM.CONNECT, // Listen for connection events
    CONSTANTS.STREAM.DISCONNECT, // Listen for disconnection events
  ],
  {
    // Filter options:
    filter: {
      // **Listen to all channels and chats (default):**
      channels: ["*"],
      chats: ["*"],

      // **Listen to specific channels and chats:**
      // channels: ['channel-id-1', 'channel-id-2'],
      // chats: ['chat-id-1', 'chat-id-2'],

      // **Listen to events with a specific recipient:**
      // recipient: '0x...' (replace with recipient wallet address)
    },

    // Connection options:
    connection: {
      retries: 3, // Retry connection 3 times if it fails
    },

    raw: false, // Receive events in structured format
  }
);

// Chat event listeners:

// Stream connection established:
stream.on(CONSTANTS.STREAM.CONNECT, async (a) => {
  console.log("Stream Connected");

  // Send initial message to PushAI Bot:
  console.log("Sending message to PushAI Bot");

  await userAlice.chat.send(pushAIWalletAddress, {
    content: "Hello, from Alice",
    type: "Text",
  });

  console.log("Message sent to PushAI Bot");
});

// Chat message received:
stream.on(CONSTANTS.STREAM.CHAT, (message) => {
  console.log("Encrypted Message Received");
  console.log(message); // Log the message payload
});

// Chat operation received:
stream.on(CONSTANTS.STREAM.CHAT_OPS, (data) => {
  console.log("Chat operation received.");
  console.log(data); // Log the chat operation data
});

// Connect the stream:
await stream.connect(); // Establish the connection after setting up listeners

// Stream disconnection:
stream.on(CONSTANTS.STREAM.DISCONNECT, () => {
  console.log("Stream Disconnected");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants