1.3.0 • Published 5 months ago

comify-pubsub-client v1.3.0

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

Comify PubSub Client

A TypeScript client for Node, React, and React Native

Features

  • Publish messages to Channels
  • Subscribe to Channels
  • TypeScript support for Node, React, and React Native

Installation

npm install comify-pubsub-client

Prerequisites

You will need the client token to authenticate your app. Get your keys from the Admin Portal.

Configuration

The client requires a configuration object with the following properties:

interface PubSubConfig {
  userId: string;           // Unique identifier for the client
  clientToken: string;     // API key for authentication
}

Usage Examples

Next.js Usage

// pages/chat.tsx
import { PubSub } from 'commify-pubsub-client';
import { useEffect, useState } from 'react';

export default function ChatPage() {
  const [messages, setMessages] = useState<any[]>([]);

  useEffect(() => {
    const pubsub = new PubSub({
      userId: 'user123',
      clientToken: process.env.NEXT_PUBLIC_API_KEY!
    });

    // Subscribe to messages
    const subscribe = async () => {
      const { unsubscribe } = await pubsub.subscribe('chat-room', (message) => {
        setMessages(prev => [...prev, message]);
      });

      // Send a test message
      await pubsub.publish('chat-room', {
        text: 'Hello from Next.js!',
        timestamp: new Date().toISOString()
      });

      // Cleanup on unmount
      return unsubscribe;
    };

    subscribe().catch(console.error);

    return () => {
      // Cleanup will be handled by the subscribe function
    };
  }, []);

  return (
    <div>
      {messages.map((msg, i) => (
        <div key={i}>{msg.text}</div>
      ))}
    </div>
  );
}

Node.js Usage

import { PubSub } from 'commify-pubsub-client';

async function setupPubSub() {
  const pubsub = new PubSub({
    userId: 'server1',
    clientToken: process.env.API_KEY!
  });

  // Subscribe to system events
  const { unsubscribe } = await pubsub.subscribe('system-events', (event) => {
    console.log('System event received:', event);
  });

  // Publish system status
  await pubsub.publish('system-events', {
    type: 'STATUS',
    status: 'healthy',
    timestamp: new Date().toISOString()
  });

  // Handle graceful shutdown
  process.on('SIGTERM', () => {
    unsubscribe();
    process.exit(0);
  });
}

setupPubSub().catch(console.error);

React Native / Expo Usage

import { PubSub } from 'commify-pubsub-client';
import { useEffect } from 'react';

function ChatScreen() {
  useEffect(() => {
    const pubsub = new PubSub({
      userId: 'mobile1',
      region: 'ap-south-1',
      clientToken: 'your-api-key'
    });

    // Subscribe to messages
    const subscribe = async () => {
      const { unsubscribe } = await pubsub.subscribe('chat-room', (message) => {
        console.log('New message:', message);
      });

      // Send a test message
      await pubsub.publish('chat-room', {
        text: 'Hello from React Native!',
        timestamp: new Date().toISOString()
      });

      return unsubscribe;
    };

    subscribe().catch(console.error);

    return () => {
      // Cleanup will be handled by the subscribe function
    };
  }, []);

  return (/* your component JSX */);
}

Configuration Options

When initializing the PubSub client, you can provide the following configuration options:

  • userId: A unique identifier for your client instance
  • clientToken: Authentication token for secure credential management. Contact support to obtain a token.
1.3.0

5 months ago

1.2.0

5 months ago

1.1.14

5 months ago

1.1.13

5 months ago

1.1.12

5 months ago

1.1.11

5 months ago

1.1.10

5 months ago

1.1.9

5 months ago

1.1.8

5 months ago

1.1.7

5 months ago

1.1.6

5 months ago

1.1.5

5 months ago

1.1.4

5 months ago

1.1.3

5 months ago

1.1.2

5 months ago

1.1.1

5 months ago

1.1.0

5 months ago

1.0.22

5 months ago

1.0.20

5 months ago

1.0.19

5 months ago

1.0.18

5 months ago

1.0.17

5 months ago

1.0.16

5 months ago

1.0.15

5 months ago

1.0.14

5 months ago

1.0.13

5 months ago

1.0.13-s

5 months ago

1.0.12

5 months ago

1.0.11

5 months ago

1.0.10

5 months ago

1.0.9

5 months ago

1.0.8

5 months ago

1.0.7

5 months ago

1.0.6

5 months ago

1.0.5

5 months ago

1.0.4

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

5 months ago