Get started with nodejs
Layr8 Messaging Script
This JavaScript script connects to a WebSocket server and sends/receives messages using the phoenix-channels package. It facilitates interactions with the Layr8 platform by sending DIDcomm-compliant messages, making it a useful tool for working with decentralized messaging.
Overview of the Key Components:
- Phoenix Channels: Uses WebSocket for real-time messaging through channels.
- Readline Interface: Captures user input for sending messages dynamically.
- DIDcomm Messaging: Sends messages compliant with the DIDcomm protocol (like
basicmessage/1.0). - Command-line Arguments: The script is configured using command-line parameters for host, port, DID, API key, etc.
Key Functions and Flow:
- Command-line Arguments: Arguments like
host,port,did,api-key, andping-didare used to configure the WebSocket connection. - WebSocket Connection: It establishes a connection to the Layr8 server using WebSockets.
- Joining Channels: Once connected, it joins a specific messaging channel based on the user’s DID.
- Message Sending and Receiving: The script allows the user to send messages to another DID using DIDcomm format and can listen for incoming messages.
How-To Guide: Working with Layr8 Messaging Script
Step 1: Setup Node.js Environment
Ensure you have Node.js installed. If not, download and install it from Node.js website.
Next, install the required dependencies by running:
npm install phoenix-channelsnpm install cryptoStep 2: Command-line Arguments
The script accepts several command-line arguments:
--host: WebSocket server host (Layr8 server address).--port: WebSocket server port.--did: Your DID (Decentralized Identifier) for messaging.--api-key: API key for Layr8 authentication.--ping-did: DID of the recipient to whom you want to send messages.
Example usage:
node msg.js --host layr8.io --port 4000 --did YOUR_DID --api-key YOUR_API_KEY --ping-did TARGET_DIDStep 3: Connect to Layr8
After running the script with the appropriate command-line arguments, it will attempt to connect to the WebSocket server (wss://host:port/plugin_socket).
- You will receive a message when the connection is successful (
socket connected). - In case of an error, the script will log the error.
Step 4: Join the Channel
Once connected, the script joins a channel named after your DID:
topic = `plugins:${my_did}`This channel will be used to send and receive messages.
Step 5: Sending Messages
Once in the channel, you can interact with the script via the terminal. The askQuestion function will prompt you for input:
>> Hello, Layr8!This message will be sent to the ping-did you specified using DIDcomm-compliant messaging.
Step 6: Receiving Messages
When a message is received from the other DID, the script will display it:
<sender-did>: Message content hereYou can continue to interact by typing more messages or type exit to quit the loop.
Step 7: Termination
You can type exit at any time to stop the interaction:
>> exitKey Features to Customize
-
Protocols: The script currently supports basic DIDcomm messages (basicmessage/1.0). You can expand this by adding more protocol types to the
protocolsarray. -
Automated Message Sending: The script includes a commented-out section that can send automated pings periodically. You can enable this feature by uncommenting and modifying the
ping_fnfunction.
By following this guide, you can use the script to interact with the Layr8 messaging platform and test decentralized messaging through DIDcomm channels. This allows for real-time communication between decentralized identities, which is a key aspect of Layr8’s decentralized web approach.