1.0.50 • Published 2 years ago

kwil_messenger v1.0.50

Weekly downloads
-
License
ISC
Repository
github
Last release
2 years ago

API for interacting with Kwil messenger nodes.

It is worth noting that all function inputs that ask for a private key are asking for parsed JWK of an RSA private key.

Installation:

npm i kwil_messenger

Initialization

When initializing, the two parameters are: node_url, and messenger_node_url. Defaults are https://kwil.ngrok.io and ws://kwilmessenger.ngrok.io (These urls may no longer be used by our team, so it is recommended to not use these). Make sure that the second input uses the websocket protocol (ws), and not https.

import KwilMessenger from 'kwil_messenger';

const messenger = new KwilMessenger('https://kwil.ngrok.io', 'ws://kwilmessenger.ngrok.io')

Connecting

Once establishing a websocket connection with a messenger node, you can send and receive messages. Functions that manage invites or chats are asynchronous, as they access regular nodes (which use a REST API instead of websockets). Once you have connected, you must notify the messenger node for which chats you want to be updated for. This can be done by passing an array of chat ID's to the messenger.greet() method.

const client = await messenger.connect()

await messenger.greet(['abc123', 'def456'])

Creating and Inviting to Chats

Chats can be created with the createChat method: createChat(chat_name, your_name, your_private_jwk). The chat_name parameter is not unique. Invites can be sent with the sendInvite method, and can be retrieved with the getInvites method. Parameters: sendInvite(chat_id, receiver, your_username, your_private_jwk). getInvites(your_username, your_private_jwk). The chat_id parameter is a unique chat identifier, and is NOT the same as a chat_name.

await messenger.createChat('My new super secret chat', 'brennanjl', privateKey)
await messenger.sendInvite('abc123', 'satoshi', 'brennanjl', privateKey)

const invites = await messenger.getInvites('satoshi', satoshiPrivateKey)
/*
returns:
[
    {
        data: {
            chatName: ...,
            chatID: ...,
            key: [Object]
        },
        from: ...,
        signature: ...,
        inviteID: ...
    }
]
*/

invites.forEach(invite => {
    let accept = //Some logic for determining whether or not to join
    if (accept) {
        await messenger.acceptInvite(invite, 'satoshi', satoshiPrivateKey)
    } else {
        //This removes the invites
        await messenger.deleteInvite(invite.inviteID, 'satoshi', satoshiPrivateKey)
    }
})

Getting data

The client object is used to send and receive data. In this example, we will get a chat's messages for a user. First, we will use the getChat method to get a user's chats. Then, we will add the event listener. Then, we will pass a chat to getMessages, after which the websocket will send us the data. getMessages has two optional parameters after the chat: the date cursor, and the query limit. The date cursor is just a javascript Date object that tells the messenger node where to start the message query (only messages made before this date will be queried). The limit is the maximum amount of messages to be returned. The maximum limit is 40.

All chats will be decrypted in the event listener using the decryptChats method.

let chats = await messenger.getChats('brennanjl', privateKey)
if (chats.valid) { 
    //getChats returns a valid field, which identifies if decryption was successful
    chats = chats.chats
} else {
    throw new Error('There was an error, decryption likely failed.')
}

let chat;
chats.forEach( _chat => {
    //Some logic to select a chat from the chat list
    ...
    chat = _chat
})

client.on('message', async function (_m) {
        _m = _m.toString()
        try {
            console.log(await messenger.decryptChats(JSON.parse(_m), chat.key))
        } catch(e) {
            //If decryption fails, then it is likely a server greeting and not a message update
            console.log(_m)
        }
    }
)

messenger.getMessages(chat, new Date, 20)
//The output from this is accessible in the event listener above

Sending Messages

Messages can be sent with the sendMessage method. Parameters: sendMessage(chat_id, message_text, your_username, your_private_jwk, chat_private_jwk)

messenger.sendMessage('abc123', 'My super secret message', 'brennanjl', privateKey, abc123PrivateKey)

Leaving Chats

Chats can be left with the clearChat() function. Parameters: clearChat(chat_id, your_username, your_private_jwk).

If you pass an empty string for chat_id, you will leave all chats.

await messenger.clearChat('abc123', 'brennanjl', privateKey)

Closing a connection

The backend will regularly check if there is a sudden disconnect. However, if you're done with the messaging, you can call the close() method to hang up the socket.

await messenger.close()

If you have any questions regarding this documentation, DM me on Twitter @brennan_lamey

1.0.50

2 years ago

1.0.49

2 years ago

1.0.48

2 years ago

1.0.47

2 years ago

1.0.46

2 years ago

1.0.45

2 years ago

1.0.44

2 years ago

1.0.43

2 years ago

1.0.42

2 years ago

1.0.41

2 years ago

1.0.40

2 years ago

1.0.39

2 years ago

1.0.38

2 years ago

1.0.37

2 years ago

1.0.36

2 years ago

1.0.35

2 years ago

1.0.34

2 years ago

1.0.33

2 years ago

1.0.32

2 years ago

1.0.31

2 years ago

1.0.30

2 years ago

1.0.29

2 years ago

1.0.28

2 years ago

1.0.27

2 years ago

1.0.26

2 years ago

1.0.25

2 years ago

1.0.24

2 years ago

1.0.23

2 years ago

1.0.21

2 years ago

1.0.20

2 years ago

1.0.19

2 years ago

1.0.17

2 years ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.11

2 years ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago