@zenpub/javascript

0.0.4 • Public • Published

JavaScript SDK for WebSockets

codecov

Connection

import { Connection } from "@zenpub/javascript/dist/cjs";

const connection = new Connection({
        authorization: "TOKEN",
        address: "HOST",
        port: 1000,
        secure: true, // SSL
        debug: false, // Skip logging
        secret: "SECRET", // For server applications
});

const instance = await connection.connect();

Subscription

Public Channels

try {
    const channel = await instance.subscribe("notifications");
    channel.on((message: string) => {
        console.log(message);
    })  
} catch (e: any) {
    console.log("Unauthorized");   
}

Private Channels

try {
    const channel = await instance.private("notifications");
    channel.on((message: string) => {
        console.log(message);
    })  
} catch (e: any) {
    console.log("Unauthorized");   
}

Authorization

If secret isn't passed as option then:

const channel = "notifications";
const input = `${instance.session.id}@private.${channel}`;
const secret = "secret";
const hash = Encrypt.make(secret, input);
const channel = await instance.private(channel, hash);

Readme

Keywords

none

Package Sidebar

Install

npm i @zenpub/javascript

Weekly Downloads

1

Version

0.0.4

License

MIT

Unpacked Size

166 kB

Total Files

71

Last publish

Collaborators

  • iantorrescl