Javascript chat client for MLBD Chat Service.
npm i @mlbd/chat-client
Initialize the client:
import { ChatClient, TokenProvider, PusherProvider } from '@mlbd/chat-client'
const tokenProvider = new TokenProvider({
// This is a POST request configuration for fetching chat token
url: 'https://my-api/auth/chat',
getHeaders: () => {
// Optional
// return a headers object your endpoint requires
return Promise.resolve({ Authorization: 'Bearer <ACCESS_TOKEN>' })
},
getBody: () => {
// Optional
// return a body object your endpoint requires
return Promise.resolve({ key: 'value' })
},
})
const chatClient = new ChatClient({
chatApiEndpoint: 'https://my-chat-api',
tokenProvider: tokenProvider,
pusherProvider: new PusherProvider({
appKey: '<PUSHER_APP_KEY>',
cluster: '<PUSHER_APP_CLUSTER>',
forceTLS: true,
authEndpoint: '<AUTH_END_POINT>',
tokenProvider: tokenProvider,
}),
})
const myGroups = await chatClient.getMyGroups()
const messages = await chatClient.getGroupMessages('<group_id>')
// either message or attachments must be provided.
const messages = await chatClient.sendMessage({
groupId: '<group_id>',
message: 'hello, world!',
attachments: [
{
title: 'Laika - the dog',
mime_type: 'image/jpeg',
url: 'https://domain/laika.jpeg',
},
],
parentMessageId: '<parent_message_id>',
mentions: ['2', '3'],
})
If you want to send files to be uploaded and attached, send an array of files rather than attachments:
// either message or attachments must be provided.
const messages = await chatClient.sendMessage({
groupId: "<group_id>",
message: "hello, world!",
files: File[],
parentMessageId: "<parent_message_id>",
mentions: ["2", "3"]
});
const stats = await chatClient.getMyStats()
await chatClient.markGroupAsRead({ groupId: '<group_id>' })
await chatClient.deleteMessage('<message_id>')
const group = await chatClient.getGroup('<group_id>')
const message = await chatClient.getMessage('<message_id>')
const messages = await chatClient.searchMessages({
keyword: '<search_keyword>',
// groupId is optional, results will be filtered by group if this is passed
groupId: '<group_id>',
// limit is optional, default: 10
limit: 5,
// offset is optional, default: 0
offset: 5,
})
limit
and offset
are to be used for pagination.
const messages = await chatClient.getMessageHistory({
// limit is optional, default: 10
limit: 5,
// optional, default: false
// should the response include messages with latest or oldest set dates
inclusive: true,
// optional, date of the latest message to find
latest: '<latest_date>',
// optional, date of the oldest message to find
oldest: '<oldest_date>',
groupId: '<group_id>',
})
If latest and oldest both are provided or if none are provided, the latest message will be on the top of the response. This will also be the case if only latest date is provided.
If latest is not provided and only oldest is provided, the oldest message will be on the top of the response.
const groupAttachments = await chatClient.getGroupAttachments('<group_id>', '<limit>', '<offset>')
const attachmentDetail = await chatClient.getAttachmentFileDetail('<attachment_id>')
const url = await chatClient.getAttachmentDownloadUrl('<attachment_id>')
await chatClient.addPinnedMessage('<message_id>')
await chatClient.removePinnedMessage('<message_id>')
const messages = await chatClient.getGroupPinnedMessages('<group_id>')
await chatClient.addSavedMessage('<message_id>')
await chatClient.removeSavedMessage('<message_id>')
const messages = await chatClient.getSavedMessages()
const messages = await chatClient.getGroupSavedMessages('<group_id>')
When a user logs in:
await chatClient.connect()
When a user logs out:
await chatClient.disconnect()
chatClient.onMessageRecieved((data: any) => {
// You have a new message!
})
chatClient.onAddedToGroup((data: any) => {
// You have been added to a group!
})
chatClient.onGroupUpdated((data: any) => {
// A group you are in has been updated!
})
chatClient.onGroupDeleted((data: any) => {
// A group you were in has been deleted!
})
chatClient.onGroupMemberAdded((data: any) => {
// A new member has been added to a group where you're in!
})
chatClient.onGroupMemberRemoved((data: any) => {
// A member has been removed from a group where you're in!
})
chatClient.onMessageDeleted((data: any) => {
// A message has been deleted from a group where you're in!
})
chatClient.onMessageRead((data: any) => {
// A message has been read in a group where you're in!
})
chatClient.onMessageUpdated((data: any) => {
// A message has been updated in a group where you're in!
})
chatClient.onPinnedMessageAdded((data: any) => {
// A message has been pinned in a group where you're in!
})
chatClient.onPinnedMessageRemoved((data: any) => {
// A message has been unpinned in a group where you're in!
})
chatClient.onSavedMessageAdded((data: any) => {
// A message has been saved!
})
chatClient.onSavedMessageRemoved((data: any) => {
// A message has been unsaved!
})
npm t
: Run test suitenpm start
: Runnpm run build
in watch modenpm run test:watch
: Run test suite in interactive watch modenpm run test:prod
: Run linting and generate coveragenpm run build
: Generate bundles and typings, create docsnpm run lint
: Lints codenpm run commit
: Commit using conventional commit style (husky will tell you to use it if you haven't π)
On library development, one might want to set some peer dependencies, and thus remove those from the final bundle. You can see in Rollup docs how to do that.
Good news: the setup is here for you, you must only include the dependency name in external
property within rollup.config.js
. For example, if you want to exclude lodash
, just write there external: ['lodash']
.
Prerequisites: you need to create/login accounts and add your project to:
Prerequisite for Windows: Semantic-release uses node-gyp so you will need to install Microsoft's windows-build-tools using this command:
npm install --global --production windows-build-tools
Follow the console instructions to install semantic release and run it (answer NO to "Do you want a .travis.yml
file with semantic-release setup?").
Note: make sure you've setup repository.url
in your package.json
file
npm install -g semantic-release-cli
semantic-release-cli setup
# IMPORTANT!! Answer NO to "Do you want a `.travis.yml` file with semantic-release setup?" question. It is already prepared for you :P
From now on, you'll need to use npm run commit
, which is a convenient way to create conventional commits.
Automatic releases are possible thanks to semantic release, which publishes your code automatically on github and npm, plus generates automatically a changelog. This setup is highly influenced by Kent C. Dodds course on egghead.io
There is already set a precommit
hook for formatting your code with Prettier π
By default, there are two disabled git hooks. They're set up when you run the npm run semantic-release-prepare
script. They make sure:
- You follow a conventional commit message
- Your build is not going to fail in Travis (or your CI server), since it's runned locally before
git push
This makes more sense in combination with automatic releases
TypeScript or Babel only provides down-emits on syntactical features (class
, let
, async/await
...), but not on functional features (Array.prototype.find
, Set
, Promise
...), . For that, you need Polyfills, such as core-js
or babel-polyfill
(which extends core-js
).
For a library, core-js
plays very nicely, since you can import just the polyfills you need:
import "core-js/fn/array/find"
import "core-js/fn/string/includes"
import "core-js/fn/promise"
...