Usage

In this example we will send a string and the other peer will return its length.

Implementing the Controller

import { Controller, Client } from 'peerly';

class StringLen extends Controller {
    constructor(client: Client, payload: any) {
        super(client, 'StringLen', payload);
    }
    res(message: any): Promise<any> {
        return new Promise((resolve, reject) => {
            resolve({ length: message.data.length })
        })
    }
}

Client 1 (Sending the string)

import { Client, utils } from 'peerly'
import StringLen from './StringLen'

(async() => {
    const client = new Client({
        serverList: await utils.getServerList()
    })
    
    client.on('connection', async () => {
        const request = new StringLen(client, { data: 'HELLO WORLD' });
        const response = await request.send();
        
        console.log(response) // -> { length: 11 }
    })
    
    client.connect('UNIQUE-ID');
})()

Client 2 (Sending the response)

import { Client, utils } from 'peerly'
import StringLen from './StringLen'

(async() => {
    const client = new Client({
        serverList: await utils.getServerList()
    })
    client.registerController(new StringLen(client));
    
    client.connect('UNIQUE-ID');
})()

Last updated