Using Trooba
In this chapter we are going to explore Trooba API and build some generic pipelines. Trooba is generic enough and does not impose any restrictions on how it should be used. It does provide a minimal set of artifacts and defines life cycle, a basic request/response and streaming support to demonstrate how it can be used, but it does not assume any structure for the request/response or data objects.
The pipeline lifecycle has three main stages
- Initialization, where one can hook to a different events.
- Initiation and execution of “request” flow, which is not an actual request assumed here, but more a direction in which events flow, from the initiator towards the target.
- Initiation and execution of “response” flow, a direction from the target towards the initiator.
One can view Trooba runtime as a message bus. Trooba defines the directions (request, response); which is used to decide where the messages should go. Every handler becomes a pipe point where the messages can originate, go through, get transformed or change the direction of the flow can change.
The understanding of the above is important when one wants to extend the existing flows or go further and create a new use-case.
Trooba defines two types of handlers, but does not limit to define more. The types are
- Generic is handler that performs some specific tasks and passes control to the next in the pipeline or can reverse the flow by throwing the error or starting the response. For example, oauth handler can get a security token form some token minting service and attach it to the request for the services that require security token.
- Transport is almost the same as a handler, but it usually connects the pipe to the external component through some protocol like http, TCP or more high level as gRPC.
Building a pipe
const Trooba = ;// build the shared pipe objectconst pipe = Trooba;// make a callpipe;
Defining a handler
module {pipe;pipe;pipe;};
Defining an http transport
module {pipe;}
Now, let’s have some fun and build a slot machine pipe:
'use strict';const Trooba = ;const symbols = '🍊' '🍉' '🍈' '🍇' '🍆' '🍅' '🍄';{pipe;}{pipe;}// create a pipelineconst sharablePipe = Trooba// reel 1// reel 2// reel 3;// create a generic client and inject contextconst client = sharablePipe;// make a requestclient;
null 'You lost! 🍉 🍊 🍆'$ node trooba-slot-machine.jsnull 'You lost! 🍆 🍈 🍄'$ node trooba-slot-machine.jsnull 'You lost! 🍆 🍆 🍉'$ node trooba-slot-machine.jsnull 'You won! 🍈 🍈 🍈'
Here’s a more typical example of service invocation pipeline:
const Trooba = ;const sharablePipe = Trooba;// At this moment sharablePipe can be re-used for different requests// create a generic client and inject contextconst client = sharablePipe;// make a requestclient;
In the next chapter we will explore how we can expand the use of pipeline framework.
EDITContributors
Helpful? You can thank these awesome people! You can also edit this doc if you see any issues or want to improve it.