1.3.0 • Published 10 months ago

comify-pubsub-client v1.3.0

Weekly downloads
-
License
MIT
Repository
-
Last release
10 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

10 months ago

1.2.0

10 months ago

1.1.14

10 months ago

1.1.13

10 months ago

1.1.12

10 months ago

1.1.11

10 months ago

1.1.10

10 months ago

1.1.9

10 months ago

1.1.8

10 months ago

1.1.7

10 months ago

1.1.6

10 months ago

1.1.5

10 months ago

1.1.4

10 months ago

1.1.3

10 months ago

1.1.2

10 months ago

1.1.1

10 months ago

1.1.0

10 months ago

1.0.22

10 months ago

1.0.20

10 months ago

1.0.19

10 months ago

1.0.18

10 months ago

1.0.17

10 months ago

1.0.16

10 months ago

1.0.15

10 months ago

1.0.14

10 months ago

1.0.13

10 months ago

1.0.13-s

10 months ago

1.0.12

10 months ago

1.0.11

10 months ago

1.0.10

10 months ago

1.0.9

10 months ago

1.0.8

10 months ago

1.0.7

10 months ago

1.0.6

10 months ago

1.0.5

10 months ago

1.0.4

10 months ago

1.0.3

10 months ago

1.0.2

10 months ago

1.0.1

10 months ago

1.0.0

10 months ago