Overview

Installation

To get started, you will need to install MinChat Javascript SDK in your Javascript front-end project.

npm install @minchat/js

The MinChat SDK needs to be initialized with the MINCHAT_API_KEY as a parameter as well as a User object representing the current user logged in. The API Key can be found on the Minchat dashboard.

add to imports section
import MinChat from '@minchat/js'
const currentUser = {
        username: "micheal",
        name: "Micheal Saunders",
        avatar: "urltoavatar.com/avatar.jpg" //optional
}

const minchat = MinChat.getInstance(MINCHAT_API_KEY).connectUser(currentUser)

Event Listeners

Listen for conversations

This is called whenever a new message is received in a conversation, new or old.

minchat.onChat((chat) => {
    //do something with the chat
})

Response

Functions

Create User

You should create users in your MinChat application to initiate conversations with. Users are at the core of all conversations. MinChat applications are made up of users who chat in either group chats or one-to-one conversations.

in MinChat, a User is a person that uses your app. Typically, you will have one MinChat User for each user in your own database.

Usually, you would create users based on the data from your database. A User is represented by a JSON object.

const otherUser = await minchat.createUser({ 
                                  username: "example-username", 
                                  name: "Example Name",
                                  avatar: "urltoavatar.com/avatar.jpg" //optional
                            })
                            

You can view more details about the user parameters here.

note: createUser(...) automatically creates a new user in the system, if a user with the same username already exists then it will reuse that user.

Fetching a user

You can fetch an already existing user using either their username or their id.


const otherUser = await minchat.fetchUser("example-username")

const otherUser = await minchat.fetchUserById("example-id")

Updating a user

You can update an existing user's information


const updatedUser = await minchat.updateUserById("example-user-id",
                              { 
                                  name: "Updated Name",
                                  avatar: "urltoavatar.com/updated-avatar.jpg" 
                              })
                            

You can view more details about the user parameters here.

Get Conversations

You can get a list of the conversations of the current user. conversations retrieved are paginated to 25 messages at a time.


const { chats , page , totalChats , totalPages  } = await minchat.getChats(1 /** page to query **/ )

Parameters

Response

Delete a user

Delete a user by username

const success = await minchat.deleteUser(otherUser.username)

Or delete a user by user id

const success = await minchat.deleteUserById(otherUser.id)

Start One-to-One Conversation

const chat = await minchat.chat(otherUser.username)

Start Group Chat


const chat = await minchat.groupChat({
                 name: "Epic Group Chat",
                 memberUsernames: [ otherUser.username , thirdUser.username ],
                 avatar: "exampleavatar.com" 
 })

Last updated