Client
Events
name | type | Parameters Description |
---|---|---|
on | function | Mount an event listener |
off | function | Cancel an event listener |
once | function | Trigger an event once |
emit | function | Trigger an event |
Properties
name | type | Parameters Description |
---|---|---|
keys | KeyPairsType | your PrivateKey and PublicKey |
listeners | {} | Event Center |
channel | Channel Class | The channel correlation |
message | Message Class | The messages correlation |
user | User Class | The user correlation |
contact | Contact Class | The contact correlation |
notify | Notify Class | The notify correlation |
topic | Topic Class | The topic correlation |
Methods
name | type | Parameters Description | response |
---|---|---|---|
init | function | InitOptions | Promise: Web3MQAPiEndpoints |
getInstance | function | KeyPairsType | Client |
getSignClient | function | (SendTempConnectOptions, callbackFn) | void |
initDappConnectClient | function | (SendTempConnectOptions, callbackFn) | void |
Methods
init()
Connect the best Web3MQ's network node.
import { Client } from "@web3mq/client";
// 1. You must initialize the SDK, the init function is asynchronous
const tempPubkey = localStorage.getItem("PUBLIC_KEY") || "";
// 1. tempPubkey
// We will give you the exclusive tempPubkey when you log in successfully;
// Usually this key is kept by you eg: save in localStorage
const didKey = localStorage.getItem("DID_KEY") || "";
// 2. didKey
// A concatenated string of the account and account type to be logged into
// eg: eth:0x0000000000000
await Client.init({
connectUrl: "example url", // The fastURL you saved to local
app_key: "app_key", // Appkey applied from our team
env: "test", // The default is the test environment
tempPubkey, // After login get temp public key
didKey, // did_key:did_value eg: eth:0x00000000123123;
});
getInstance()
Create and get the Client Instance.
import { Client } from "@web3mq/client";
// 1. You must initialize the SDK, the init function is asynchronous
await Client.init({
connectUrl: "example url", // The fastURL you saved to local
app_key: "app_key", // Appkey applied from our team
});
// 2.Login and get keys
const { address } = await Client.register.getAccount(didType);
const { userid, userExist } = await Client.register.getUserInfo({
did_value: address,
did_type: didType,
});
let localMainPrivateKey = "";
let localMainPublicKey = "";
if (!userExist) {
const registerRes = await Client.register.register({
password,
did_value: address,
userid,
did_type: didType,
avatar_url: `https://cdn.stamp.fyi/avatar/${address}?s=300`,
});
localMainPrivateKey = registerRes.mainPrivateKey;
localMainPublicKey = registerRes.mainPublicKey;
}
const {
TempPrivateKey,
TempPublicKey,
pubkeyExpiredTimestamp,
mainPrivateKey,
mainPublicKey,
} = await Client.register.login({
password,
userid,
did_value: address,
did_type: didType,
mainPublicKey: localMainPublicKey,
mainPrivateKey: localMainPrivateKey,
});
// 3. You must ensure that the Client.init initialization is complete and that you have a key pair
const client = Client.getInstance({
PrivateKey: TempPrivateKey,
PublicKey: TempPublicKey,
userid: userid,
});
console.log(client);
getSignClient
Get sign client.
import { Client, SignClientCallBackType } from "@web3mq/client";
// 1. You must initialize the SDK, the init function is asynchronous
await Client.init({
connectUrl: "example url", // The fastURL you saved to local
app_key: "app_key", // Appkey applied from our team
});
// 2. Create the handleEvent function to receive events
const handleEvent = (options: SignClientCallBackType) => {
console.log(options);
};
// 3. get Sign Client, Make sure that init is complete
await Client.getSignClient(
{
dAppID: dapp_id,
topicID: topic_id,
signatureTimestamp: signature_timestamp,
dAppSignature: dapp_signature,
},
handleEvent
);
initDappConnectClient
Create the client that connects to the DApp.
import { Client } from "@web3mq/client";
// 1. You must initialize the SDK, the init function is asynchronous
await Client.init({
connectUrl: "example url", // The fastURL you saved to local
app_key: "app_key", // Appkey applied from our team
});
// 2. Create the handleEvent function to receive events
const handleEvent = (options: SignClientCallBackType) => {
console.log(options);
};
// 3. get DappConnect Client
Client.initDappConnectClient({ dAppID: "SwapChat:im" }, handleEvent);
// 4. Here you can use the client to interact with the wallet
Client.dappConnectClient.getConnectLink();
Events
on()
* on: (eventName: EventTypes, callback: any) => void;
client.on('channel.activeChange', (event) {
console.log(event)
})
off()
* off: (eventName: EventTypes, callback?: any) => void;
client.off('channel.activeChange', (event) {
console.log(event)
})
once()
* once: (eventName: EventTypes, callback?: any) => void;
client.once('channel.activeChange', (event) {
console.log(event)
})
emit()
* emit: (eventName: EventTypes, ...args: any[]) => void;
client.emit("channel.activeChange", {
data: "",
});