Skip to content
This repository has been archived by the owner on May 17, 2024. It is now read-only.

Latest commit

 

History

History
141 lines (105 loc) · 5.34 KB

README.md

File metadata and controls

141 lines (105 loc) · 5.34 KB

Track 2.1 - Task 2: Send greetings

Progress

Description

In the previous task, we learned how to establish e2e-encrypted messaging pipes between agents. Now we send our first messages using this communication pipe.

Agents interact using Hyperledger Aries protocols. There are different protocols for different purposes. Agents send text messages to each other using basic message protocol.

Task sequence

App Overview

In this task:

We will create a new connection according to the steps in task 1. We have already the logic for that in place. In addition, we will add logic to the application to send and receive basic messages:

  1. Once the connection protocol is complete, the application is notified of the new connection.
  2. Application sends a greeting to the new connection.
  3. Application agent initiates the Aries basic message protocol.
  4. Once the protocol is completed, the application is notified of the message sending success.
  5. Once the protocol is completed, the wallet user is notified of the received message.
  6. Wallet user sends a message to the application.
  7. User agent initiates the Aries basic message protocol.
  8. Once the protocol is completed, the wallet user is notified of the message sending success (message is displayed in the chat view).
  9. Once the protocol is completed, the application is notified of the received message.
sequenceDiagram
    autonumber
    participant Client Application
    participant Application Agent
    participant User Agent
    actor Wallet User

    Application Agent->>Client Application: <<New connection!>>
    rect rgb(191, 223, 255)
    Client Application->>Application Agent: Send greeting
    Note right of Application Agent: Aries Basic message protocol
    Application Agent->>User Agent: Send message
    Application Agent->>Client Application: <<Message sent!>>
    User Agent->>Wallet User: <<Message received!>>
    end
    rect rgb(191, 191, 255)
    Wallet User->>User Agent: Send greeting
    Note right of Application Agent: Aries Basic message protocol
    User Agent->>Application Agent: Send message
    User Agent->>Wallet User: <<Message sent!>>
    Application Agent->>Client Application: <<Message received!>>
    end
Loading

1. Use protocol API client to send a text to the other agent

In the previous task, we added a handler for new connection notifications. Modify this handler so that when a new connection gets created, we send a greeting to the other agent.

Open file src/listen.ts.

Add agencyv1 to objects imported from findy-common-ts:

import { agencyv1, AgentClient, ProtocolClient } from '@findy-network/findy-common-ts'

Modify handler DIDExchangeDone to following:

      // New connection is established
      DIDExchangeDone: async (info, didExchange) => {
        console.log(`New connection: ${didExchange.getTheirLabel()} with id ${info.connectionId}`)

        // Greet each new connection with basic message
        const msg = new agencyv1.Protocol.BasicMessageMsg()
        msg.setContent('Hi there 👋!')
        await protocolClient.sendBasicMessage(info.connectionId, msg)
      },

2. Ensure the message is sent to the web wallet

Refresh the /greet-page and create a new connection using the web wallet UI. Check that the greeting is received in the web wallet UI.

Receive message in web wallet

3. Add handler for received messages

Continue editing file src/listen.ts.

Add new handler BasicMessageDone to listener. When receiving messages from other agents, print them to log:

      DIDExchangeDone: async (info, didExchange) => {
        ...
      },

      BasicMessageDone: async (info, basicMessage) => {
        // Print out greeting sent from the other agent
        if (!basicMessage.getSentByMe()) {
          const msg = basicMessage.getContent()
          console.log(`Received basic message ${msg} from ${info.connectionId}`)
        }
      },

4. Ensure the received message is printed to logs

Send a reply from the web wallet UI:

Send message in web wallet

Check that the sent message is visible in the server logs:

Server logs

5. Continue with task 3

Congratulations, you have completed task 2, and now know how to send and receive basic messages with the Hyperledger Aries protocol! To revisit what happened, check the sequence diagram.

You can now continue with task 3.