homesynck-sdk
TypeScript icon, indicating that this package has built-in type declarations

1.0.7 • Public • Published

thumbnail

Homesynck SDK (Javascript & Typescript)

An easy API to harness the power of your Homesynck server.

Installation

Browser

Replacing <VERSION> by the version you want:

<script src="https://cdn.jsdelivr.net/npm/homesynck-sdk@<VERSION>/browser/bundle.js"></script>
const { init, HomesynckConnection, HomesynckDirectory } = require("homesynck-sdk");

Node.js

npm i homesynck-sdk
// If supported:
import { init, HomesynckConnection, HomesynckDirectory } from "homesynck-sdk";
// Otherwise
const { init, HomesynckConnection, HomesynckDirectory } = require("homesynck-sdk");

Deno

Just do:

import { init, HomesynckConnection, HomesynckDirectory } from "homesynck-sdk";

Tutorial

Step 1: Init connection & login

let connection = await init(URL)
    
await connection.login({
    login: "admin",
    password: "superpassword"
})

Step 2: Create the directory to sync into

let directory = await connection.openOrCreateDirectory("Test")

Step 3: Set message received callback & start syncing

directory.onUpdateReceived(({instructions, rank}, state) => {
    console.log(instructions) //logging the received message body
    console.log(rank) //logging the received message rank

    return state; //returning state unchanged
})

// Not yet receiving updates
await directory.startSyncing()
// Now receiving updates

Step 4: Send updates anytime you want!

await directory.pushInstructions("HELLO HOMESYNCK")

But that's not all, Homesynck SDK can manage state for you!

Optional 1: Set initial state

directory.setInitialState({
    value: 0
})

Optional 2: React to updates by changing state

directory.onUpdateReceived(mySplendidReactiveFunction)

function mySplendidReactiveFunction(update, state) {
    console.log("Received Update: \n" + JSON.stringify(update))
    
    let instructionsDecoded = JSON.parse(update.instructions)

    if(instructionsDecoded.type == "add") {
        state.value += instructionsDecoded.value
    } else if(instructionsDecoded.type == "multiply") {
        state.value *= instructionsDecoded.value
    }
    
    console.log(`[${update.rank}]: ` + JSON.stringify(state))

    return state;
}

//You now need to put objects in your updates
await directory.pushInstructions({
    type: "add",
    value: 3.14
});

await directory.pushInstructions({
    type: "multiply",
    value: -42
});

Readme

Keywords

none

Package Sidebar

Install

npm i homesynck-sdk

Weekly Downloads

3

Version

1.0.7

License

MIT

Unpacked Size

58.7 kB

Total Files

7

Last publish

Collaborators

  • anicetngrt