Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add message handler middleware and fallback #1894

Merged
merged 4 commits into from
Jun 9, 2024

Conversation

TimoGlastra
Copy link
Contributor

@TimoGlastra TimoGlastra commented Jun 8, 2024

This PR adds two things:

  • middleware for the message handler
  • fallback message handler for when no message handler is registered for the incoming message type

You could quite easily solve 2 (fallback message handler) with 1 (middleware), but it's meant to be a convenience method so you can easily handle incoming messages without a handler (i suspect for a lot of use case it should suffice). But the middleware gives that extra power to e.g. modify the incoming message.

It would be nice to also start adopting the middleware in more places, e.g. the message sender (to modify messages before they are sent).

Our use case for adding this at the moment is that we want to handle messages that have no message class supported. I haven't added any top-level APIs yet (only on the dependency manager) as it's good to fiddle around for a bit maybe first. One thing I think that might be worth considering is adding a top level method where you can register a handler for incoming messages. So you can 'extend' the framework without having to write custom classes. E.g. a didcommCustomMessaging module, where you can send and receive messages.

Then this work would be more the internal API to hook into the receiving / sending process but for most cases we can just expose it as an easy to use API.

The API can be used as follows at the moment:

agent.dependecyManager.registerMessageHandlerMiddleware(async (inboundMessageContext: InboundMessageContext, next) => {
// this middleware will just silently ignore any incoming messages without a handler
  if (!inboundMessageContext.messageHandler) {
   console.log('no message handler registered').

   // if returned, and next is not called, other middleware WON'T be called
   return
 }

  await next()
})

And for the fallback handler

agent.dependecyManager.setFallbackMessageHandler(async (messageContext: InboundMessageContext) => {

  await writeMessageToDatabase(inboundMessageContext.message)

  // reply with the same message that we received, because why not? :)
  return getOutboundMessageContext(inboundMessageContext.agentContext, {
    message: inboundMessageContext.message,
   connectionRecord: inboundMessageContext.connection,
 })
})

No breaking changes

Fixes #97

cc @Tommylans

Signed-off-by: Timo Glastra <timo@animo.id>
Signed-off-by: Timo Glastra <timo@animo.id>
Signed-off-by: Timo Glastra <timo@animo.id>
Copy link
Contributor

@genaris genaris left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great job @TimoGlastra ! I think I can use it to ignore duplicated incoming messages: I can define it as a middleware that I'll use initially on my app but can also contribute back as an extension middleware that anybody can use if they want.

Looking forward for outbound message middleware support as well. We can use them to set some optional attributes of DIDComm messages (specially V2) such as creation time.

@TimoGlastra TimoGlastra merged commit 558f877 into openwallet-foundation:main Jun 9, 2024
11 of 12 checks passed
@TimoGlastra TimoGlastra deleted the middleware branch June 9, 2024 06:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

add agent middleware
2 participants