@chat-e2ee/service
TypeScript icon, indicating that this package has built-in type declarations

1.5.0 • Public • Published

This is a client-side SDK to interact with chat-e2ee service. It allows dev to build own chat client on top of chate2ee service. It uses socket.io for websocket connection.

npm version size

npm i @chat-e2ee/service

@chat-e2ee/service exports the following modules:

  • createChatInstance - core chat ops.
  • utils
    • generateUUID - util func to generate UUID.
    • decryptMessage - to decrypt encrypted messages.
  • setConfig - configuration - set URLs i.e. API endpoints, debugging etc.

Example and flow:

1. Import and initialize the SDK:

import { createChatInstance, utils, setConfig } from '@chat-e2ee/service';
const chatInstance = createChatInstance(config);
await chatInstance.init();

Note that the config is optional.

2. Setup channel:

First, you have to set up a channel. To set up a channel you need to generate a hash, user ID.

const userId = utils.generateUUID(); // you can use your own user id.
const { hash } = await chatInstance.getLink();

await chatInstance.setChannel(hash, userId);

Once you set up a channel, user2 can join the channel by passing the same hash to setChannel with their own userid. Note that userid should be unique.

3. Send message:

When both users have joined the channel, you are ready to send a message.

await chatInstance.encrypt('some message').send();

4. Receive messages:

Setup listener to receive messages from user2 and use your private key to decrypt messages.

const { privateKey } = chatInstance.getKeyPair();
chatInstance.on('chat-message', async () => {
    const msgInPlainText = await utils.decryptMessage(msg.message, privateKey);
    console.log(msgInPlainText);
});

chatInstance.getLink():

One user needs to create a link and share it with other user.
Each instance is unique to each link. To create a separate link, another instance needs to be created.

const linkDescription = chatInstance.getLink();

linkDescription contains basic info:

{
    hash: string;
    link: string;
    expired: boolean;
    deleted: boolean;
    pin: string;
    pinCreatedAt: number;
}

Send message:
1 - Auto encryption by @chat-e2ee/service

@chat-e2ee/service will encrypt message with publicKey before sending to network.

chatInstance.encrypt({ image, text }).send();

2 - Custom encryption / No encryption:

Simply call .sendMessage() with encrypted or plain text.

chatInstance.sendMessage({ image, message: <message> });

Event listeners:

chate2ee.on(events, callback);

Events:
on-alice-join - reveiver joined the link
chat-message - new message received

chate2ee.on('chat-message', (msg) => {
    console.log('message received',msg)
})

msg object:

{
    channel: string,
    sender: string,
    message: string,
    id: number,
    timestamp: number,
    image?: string
}

on-alice-disconnect - receiver left/disconnected from the link
limit-reached - 2 users already join a link
delivered - a message is delivered to the receiver callback returns the ID of the message that's delivered.

chate2ee.on('delivered', (id) => {
    console.log('delivered',id)
})

Config:

Call setConfig with config object to override default config parameters.

config follows:

{
    apiURL: string | null,
    socketURL: string | null,
    settings: {
        disableLog: boolean,
    }
}

Note that @chat-e2ee/service will make request to / in local env and to hosted server in production env by default. If you want to use a custom server, use setConfig({ apiURL, socketURL });


Debugging:

Open the browser console and filter your logs by @chat-e2ee/service

Screenshot 2023-06-06 at 10 11 49

to disable logging set the settings.disableLog to true in configContext:

setConfig({
    settings: {
        disableLog: boolean
    }
})

Readme

Keywords

none

Package Sidebar

Install

npm i @chat-e2ee/service

Weekly Downloads

1

Version

1.5.0

License

ISC

Unpacked Size

549 kB

Total Files

49

Last publish

Collaborators

  • muke1908