This quickstart is for using Sending.Network (SDN) on the client side in the browser, or front-end. Let's explore how to make a simple Sending.Network client with the ability to sync messages, create a chat room, and post messages in the room.
When you complete this quickstart, you’ll have a fully running application that can send and receive messages from the comfort of your own web browser.
Prerequisites
Before you start, make sure you have the following dependencies installed.
Then you are free to import the package in your code:
import sdk from"sendingnetwork-js-sdk";
Step 2. Configure Developer Key
You will need a developer key to access the Edge Network. For further insight into the mechanism and to request a developer key, please consult the provided guide here.
Kindly establish a secure key server for the proper management of your developer key. Additionally, it is necessary to outline the following API, which enables the widget to send a challenge to be signed by the key server.
To successfully complete the key verification, you need to provide a function called signWithDevKey. This function should be attached to the window object and will be called during widget login. Here is a snippet of code for your reference:
// Input Parameters: { message: string }// Output Parameters: signature stringsignWithDevKey=async ({ message }:any) => {if (!message) return;// Make a request to your backend API to sign the message and retrieve the signature.// Note: This example demonstrates the concept; implement this API in your backend.constresponse=awaitfetch('https://YOUR_KEY_SERVER/_api/appservice/sign', { method:'POST', body:JSON.stringify({ message }), }, );const { signature } =awaitresponse.json();return signature;};// Pseudo Code for Login Flowlogin=async () => {// Other code logic, omitted here...const { message: lMessage,updated,random_server } =awaitthis._client.preDiDLogin1(preloginParams);constdevSign=awaitwindow.signWithDevKey({message: lMessage});constloginParams= {// Other parameters, omitted here... identifier: {// Other parameters, omitted here... app_token: devSign } }const { access_token,user_id } =awaitthis._client.DIDLogin(loginParams);// Other code logic, omitted here...}
Step 3. Register
Now, it's time to register a DID, creating a unique identifier in the P2P messaging network.
You can connect to a public node by configuring the baseUrl with https://portal0101.sending.network.
The following code sets up the client that connects to the server. The client will start syncing and then listen for the response to get the latest state from the server:
client.startClient();client.once('sync',function(state, prevState, res) {console.log(state); // state will be 'PREPARED' when the client is ready to use});
Once the sync is complete, you can add listeners for events as follows.:
You can listen to all incoming events, but that would be too much data for our simple application. Instead, let's listen to events that happens after you join a chat room: