sm-chat
v1767581.686.835
Published
Professional integration for https://supermaker.ai/chat/
Downloads
78
Readme
sm-chat
A lightweight and versatile JavaScript library for seamless integration with chat services. Simplifies chat interactions within your applications.
Installation
Install the sm-chat package using npm:
bash
npm install sm-chat
Usage
Here are several examples demonstrating how to use the sm-chat package in different scenarios:
1. Basic Chat Initialization:
This example shows how to initialize a basic chat instance and send a simple message. javascript const smChat = require('sm-chat');
const chatInstance = new smChat.ChatClient({ apiKey: 'YOUR_API_KEY', // Replace with your actual API key userId: 'user123', // Replace with the user's ID });
chatInstance.sendMessage('Hello, world!');
chatInstance.on('message', (message) => { console.log('Received message:', message); });
chatInstance.connect();
2. Handling Incoming Messages:
This example demonstrates how to listen for and handle incoming messages from the chat service. javascript const smChat = require('sm-chat');
const chatInstance = new smChat.ChatClient({ apiKey: 'YOUR_API_KEY', userId: 'user456', });
chatInstance.on('message', (message) => { console.log('New message from:', message.senderId); console.log('Message content:', message.content); });
chatInstance.connect();
3. Sending Messages to a Specific Recipient:
This example shows how to send a direct message to a specific user. javascript const smChat = require('sm-chat');
const chatInstance = new smChat.ChatClient({ apiKey: 'YOUR_API_KEY', userId: 'user789', });
chatInstance.sendMessage('Hello, John!', 'john123'); // Send to user 'john123'
chatInstance.connect();
4. Joining and Leaving Channels:
This demonstrates how to join and leave specific chat channels. javascript const smChat = require('sm-chat');
const chatInstance = new smChat.ChatClient({ apiKey: 'YOUR_API_KEY', userId: 'userabc', });
chatInstance.joinChannel('support-channel');
chatInstance.on('channelMessage', (message, channelId) => { if (channelId === 'support-channel') { console.log('Message in support channel:', message); } });
// Later, leave the channel setTimeout(() => { chatInstance.leaveChannel('support-channel'); console.log('Left support channel.'); }, 60000); // Leave after 1 minute
chatInstance.connect();
5. Advanced Configuration with Custom Events:
This example shows how to configure the chat client with custom event listeners. javascript const smChat = require('sm-chat');
const chatInstance = new smChat.ChatClient({ apiKey: 'YOUR_API_KEY', userId: 'userdef', options: { reconnectAttempts: 5, }, });
chatInstance.on('connected', () => { console.log('Successfully connected to the chat server.'); });
chatInstance.on('disconnected', () => { console.log('Disconnected from the chat server.'); });
chatInstance.on('error', (error) => { console.error('Chat error:', error); });
chatInstance.connect();
API Summary
ChatClient(config): Creates a new chat client instance.config: An object containing configuration options.apiKey(string, required): Your API key for accessing the chat service.userId(string, required): The unique identifier for the user.options(object, optional): Additional configuration options.reconnectAttempts(number, optional): The number of reconnection attempts. Defaults to 3.
chatInstance.connect(): Establishes a connection to the chat service.chatInstance.disconnect(): Closes the connection to the chat service.chatInstance.sendMessage(message, recipientId): Sends a message.message(string, required): The content of the message.recipientId(string, optional): The ID of the recipient (for direct messages). If omitted, the message is sent to the default channel.
chatInstance.joinChannel(channelId): Joins a specific chat channel.channelId(string, required): The ID of the channel to join.
chatInstance.leaveChannel(channelId): Leaves a specific chat channel.channelId(string, required): The ID of the channel to leave.
chatInstance.on(event, callback): Registers a callback function for a specific event.event(string, required): The name of the event. Possible values:message,channelMessage,connected,disconnected,error.callback(function, required): The function to be called when the event occurs.
License
MIT
This package is part of the sm-chat ecosystem. For advanced features and enterprise-grade tools, visit: https://supermaker.ai/chat/
