<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: John Biundo</title>
    <description>The latest articles on Forem by John Biundo (@johnbiundo).</description>
    <link>https://forem.com/johnbiundo</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F196944%2F648e8463-fbb1-4b17-96c4-9baeea9b8be9.png</url>
      <title>Forem: John Biundo</title>
      <link>https://forem.com/johnbiundo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/johnbiundo"/>
    <language>en</language>
    <item>
      <title>Part 5: Completing the Client Component</title>
      <dc:creator>John Biundo</dc:creator>
      <pubDate>Wed, 04 Mar 2020 14:37:21 +0000</pubDate>
      <link>https://forem.com/nestjs/part-5-completing-the-client-component-gdn</link>
      <guid>https://forem.com/nestjs/part-5-completing-the-client-component-gdn</guid>
      <description>&lt;p&gt;&lt;em&gt;John is a member of the NestJS core team&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;This is Part 5 of a six-part series.  If you landed here from Google, you may want to start with &lt;a href="https://dev.to/nestjs/part-1-introduction-and-setup-1a2l"&gt;Part 1&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this article, we build the final iteration of the client component of the Faye Custom Transporter.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reminder&lt;/strong&gt;: Many of the concepts and terminology here are introduced and explained in &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3"&gt;this article series&lt;/a&gt;. That series serves as a good foundation for understanding the more advanced concepts covered in this series.&lt;/p&gt;

&lt;h4&gt;
  
  
  Get the Code
&lt;/h4&gt;

&lt;p&gt;All of the code in these articles is available &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample"&gt;here&lt;/a&gt;.  As always, these tutorials work best if you follow along with the code.  The &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/README.md"&gt;README&lt;/a&gt; covers all the steps you need to get the repository, build the apps, and follow along.  It's easy!  I strongly encourage you to do so.  Note that each article has a corresponding branch in the repository.  For example, this article (Part 5), has a corresponding branch called &lt;code&gt;part5&lt;/code&gt;. Read on, or &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/README.md#part-5-final-client-component"&gt;get more details here&lt;/a&gt; on using the repository.&lt;/p&gt;

&lt;h4&gt;
  
  
  Git checkout the current version
&lt;/h4&gt;

&lt;p&gt;For this article, you should &lt;code&gt;git checkout&lt;/code&gt; the branch &lt;code&gt;part5&lt;/code&gt;.  You can get more information &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/README.md#part-5-final-client-component"&gt;about the git repository branches here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Build the Apps for This Part
&lt;/h4&gt;

&lt;p&gt;For each article in the series, we introduce some new components (and sometimes entirely new &lt;strong&gt;projects&lt;/strong&gt;).  For convenience, at the start of each article, you should run the following command from the top-level directory (where you cloned the repo): *&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="c"&gt;# from root directory of project (e.g., transporter-tutorial, or whatever you chose as the root)&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;sh build.sh
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;This is a shell script that runs on Linux-like systems.  You may have to make an adjustment for non-Linux systems.  Note that the script is simply a convenience that runs &lt;code&gt;npm install &amp;amp;&amp;amp; npm run build&lt;/code&gt;inside each top-level directory, so you can always fall back to that technique if you have trouble with the script.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Overview
&lt;/h3&gt;

&lt;p&gt;I ended the last article with a discussion of the shortcomings of our &lt;em&gt;Take 1&lt;/em&gt; (first iteration) implementation of the Faye Custom Transporter client component.  In this article, we'll address those shortcomings.  Since this chapter is considerably more complex than the previous ones, we'll spend a fair amount of time clarifying the issues and discussing the strategy and approach before we get into the code.  My intention is to clarify the &lt;strong&gt;concepts&lt;/strong&gt; you'll need to address in &lt;strong&gt;any&lt;/strong&gt; custom transporter, rather than focusing entirely on the code for the &lt;strong&gt;Faye-flavored&lt;/strong&gt; Custom Transporter.  Hopefully we're creating re-usable conceptual building blocks, and creating a &lt;em&gt;big picture&lt;/em&gt; you can rely on in building any custom transporter!&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding the Multiplexing Challenge
&lt;/h3&gt;

&lt;p&gt;The &lt;em&gt;big problem&lt;/em&gt; we found with our first implementation is that it's a little naïve about handling multiple simultaneous requests.  Let's make sure we understand why.  The first thing to think through is the premise of multiplexing.  What we're essentially doing is sharing a single pair of channels (per pattern) for sending and receiving unrelated requests.  Let's focus on the behavior of the &lt;em&gt;response&lt;/em&gt; channel, as it seems like the potentially problematic one (after all, the &lt;em&gt;request&lt;/em&gt; channel is just sort of "fire and forget").&lt;/p&gt;

&lt;p&gt;We uncovered the problem in our "race test" at the end of the last chapter. The triggering scenario for our problem, in basic terms, is: &lt;strong&gt;we sent two requests, where the second request finished before the first one&lt;/strong&gt;.  Our shared response channel just isn't prepared to handle this. It's well-behaved in the context of one-at-a-time requests: it subscribes and waits for a response, then unsubscribes. But in the face of multiple overlapping requests, its behavior is clearly incorrect. Let's try to describe why, as this will guide us to a solution.&lt;/p&gt;

&lt;p&gt;The problem is this: we can have only a single active &lt;em&gt;Faye subscription handler&lt;/em&gt;&lt;sup&gt;1&lt;/sup&gt; for each pattern no matter how many messages we send on that pattern (this is by definition, since our design is committed to a single response channel).  Each request for a given pattern causes us to overwrite the previous &lt;em&gt;Faye subscription handler&lt;/em&gt; for that pattern when we re-issue the &lt;code&gt;fayeClient.subscribe(...)&lt;/code&gt; call.  Moreover, that active one, with its built-in Observer, notifies &lt;strong&gt;every&lt;/strong&gt; subscriber for that pattern.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;sup&gt;1&lt;/sup&gt;The generic term "subscription handler" is unfortunately overloaded in our discussion.  In the last chapter we introduced our nifty &lt;em&gt;observable subscriber function&lt;/em&gt;.  Here we're talking about a &lt;em&gt;Faye subscription handler&lt;/em&gt;.  These are two separate concepts, and we need to try to be very careful with our language.  &lt;strong&gt;Especially&lt;/strong&gt; since we have been, and will continue to be, building objects that create a tight relationship between these two!  I'll do my best to keep the terminology straight, and you please do too!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Strategy for Solving the Multiplexing Challenge
&lt;/h3&gt;

&lt;p&gt;Let's start working on a strategy to solve this challenge. Given the complexity of the problem, it's helpful to approach a solution starting from a somewhat abstract level, and successively refine our understanding and implementation.  We'll walk through the details of handling this in our Faye client component, but some of the details will vary from one transport library (e.g., broker) to the next because &lt;strong&gt;each has its own message protocol and API&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you understand the concepts, there is a certain amount of boilerplate material that you can adapt to a particular transport library. Inevitably, if you need to build a new transporter client, you'll have to work through a few knotholes. In the mean time, pay more attention to the concepts than to understanding every line of code.&lt;/p&gt;

&lt;h4&gt;
  
  
  The Observable Part
&lt;/h4&gt;

&lt;p&gt;We know &lt;code&gt;ClientProxy#send()&lt;/code&gt; returns an Observable.  The job of that Observable is to consume incoming Faye messages (boxes in the diagram below) and emit them as a stream (circles in the diagram below) so that our user-land subscriber can deal with them as a nice, well-behaved RxJS Observable.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--n-baYUHV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://raw.githubusercontent.com/johnbiundo/dev.to-articles/master/blog-posts/custom-transporter/assets/client-proxy-observable.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--n-baYUHV--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_66%2Cw_880/https://raw.githubusercontent.com/johnbiundo/dev.to-articles/master/blog-posts/custom-transporter/assets/client-proxy-observable.gif" alt="Convert Messages to Stream" title="Convert Messages to Stream"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;a&gt;&lt;/a&gt;Figure 1: Convert Messages to Stream



&lt;p&gt;One valuable lesson we learned in the last chapter is that we can use the Observable/observer pattern to essentially "wrap our Faye client inside an Observable". This was our &lt;em&gt;observable subscriber function&lt;/em&gt; pattern — implemented in the &lt;code&gt;handleRequest()&lt;/code&gt; method (&lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/part4/nestjs-faye-transporter/src/requestor/clients/faye-client.ts#L27"&gt;review the source code here&lt;/a&gt;) — which builds the &lt;em&gt;observable subscriber function&lt;/em&gt; (the bit of code that actually emits values through our Observable).  We're going to build on this lesson, and take it to the next level, to deal with the complexities of the multiplexing challenge.&lt;/p&gt;

&lt;h4&gt;
  
  
  Handling Multiple Requests: The Union of Observables and Correlation Ids
&lt;/h4&gt;

&lt;p&gt;The problem we have to solve is how to associate a unique &lt;em&gt;observable subscriber function&lt;/em&gt; with each Observable (i.e., with each &lt;code&gt;send()&lt;/code&gt; request that returns that Observable as a response). Furthermore, we need to do this while having only a &lt;strong&gt;single active Faye subscription handler for each pattern&lt;/strong&gt;.  This is going to require a little higher order programming.  Stick with me — this is the hardest part of the tutorial, but we can get through it.&lt;/p&gt;

&lt;p&gt;Let's propose defining our problem as follows: we are binding the &lt;em&gt;observable subscriber function&lt;/em&gt; logic to our &lt;em&gt;Faye subscription handler&lt;/em&gt; too soon/too statically.  Our solution needs to do late/dynamic binding of the &lt;em&gt;observable subscriber function&lt;/em&gt; logic.  To be precise, it needs to delay binding the the &lt;em&gt;observable subscriber function&lt;/em&gt; into the &lt;em&gt;Faye subscription handler&lt;/em&gt; until a request is made so it can associate a unique one to each request. This is tough, because there is only a &lt;strong&gt;single&lt;/strong&gt; &lt;em&gt;Faye subscription handler&lt;/em&gt; for each pattern.  What to do? We have to make a little leap here.&lt;/p&gt;

&lt;p&gt;Conceptually, what we're going to do is the following.  We'll make our single &lt;em&gt;observable subscriber function&lt;/em&gt; logic dynamic by extracting a chunk of what was previously static code and instead have it produced dynamically by a factory function. We'll invoke the factory function for each &lt;code&gt;send()&lt;/code&gt; request.  The code we extract and produce from the factory will be the code that actually translates inbound messages to Observer emits. We'll call that code the &lt;em&gt;response emitter&lt;/em&gt; and the factory that produces it the &lt;em&gt;response emitter factory&lt;/em&gt;. We'll see the factory in a moment.&lt;/p&gt;

&lt;p&gt;We'll store a unique instance of the &lt;em&gt;response emitter&lt;/em&gt; function each time we create an Observable. Later, when we get a response, we'll retrieve that stored function and plug it back into the single static &lt;em&gt;observable subscriber function&lt;/em&gt;, giving us a unique &lt;em&gt;observable subscriber function&lt;/em&gt; for each request.&lt;/p&gt;

&lt;p&gt;Let's get started. The &lt;em&gt;response emitter factory&lt;/em&gt; function is actually provided by the framework (it's called &lt;code&gt;createObserver&lt;/code&gt;, but we're going to stick with our &lt;em&gt;response emitter factory&lt;/em&gt; label in this article):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// from https://github.com/nestjs/nest/blob/master/packages/microservices/client/client-proxy.ts#L82&lt;/span&gt;
  &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nx"&gt;createObserver&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Observer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;WritePacket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isDisposed&lt;/span&gt; &lt;span class="p"&gt;}:&lt;/span&gt; &lt;span class="nx"&gt;WritePacket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;isDisposed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isDisposed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;We've gotten quite meta here! Let's take a quick timeout to clarify all our terms!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Faye subscription handler&lt;/em&gt;: A callback registered with the Faye client library that is invoked whenever a message with a particular topic is received.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Observable subscriber function&lt;/em&gt;: A callback defined during &lt;em&gt;Observable creation&lt;/em&gt; to convert application events (e.g., inbound Faye messages) to Observer callbacks. For example, this is the mechanism that enables us to call the user-land handler with the value emitted by our Observer every time an inbound message is received.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Response emitter factory&lt;/em&gt;: A &lt;a href="https://en.wikipedia.org/wiki/Higher-order_function"&gt;higher order function&lt;/a&gt;, provided by the framework, that &lt;strong&gt;returns&lt;/strong&gt; a &lt;em&gt;response emitter&lt;/em&gt; (defined below).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Response emitter&lt;/em&gt;: A function that delivers the dynamic part of our &lt;em&gt;observable subscriber function&lt;/em&gt;.  It handles the actual Observer emit calls to produce the Observable stream. We will have a unique instance of the &lt;em&gt;response emitter&lt;/em&gt; for each Observable, overcoming the multiplexing problem.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let's head toward implementation.  The approach we'll take to implement this goes something like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;At the time a user-land &lt;code&gt;send()&lt;/code&gt; call is made, we'll call the &lt;em&gt;response emitter factory&lt;/em&gt; to create a unique instance of the &lt;em&gt;response emitter&lt;/em&gt;; we'll also generate a unique identifier* (&lt;code&gt;id&lt;/code&gt; field) for the request, and store an association between the unique id and the &lt;em&gt;response emitter&lt;/em&gt; in a map.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;*At last, we see the genesis of the &lt;code&gt;id&lt;/code&gt; field we've been carrying along with our messages all this time!&lt;/p&gt;
&lt;/blockquote&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We'll subscribe to the response channel.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When a response message is received, we'll extract the &lt;code&gt;id&lt;/code&gt; from the message, and use it to look up the &lt;em&gt;response emitter&lt;/em&gt;. We'll then call this unique instance of the &lt;em&gt;response emitter&lt;/em&gt; from within the single shared &lt;em&gt;observable subscriber function&lt;/em&gt;, and use this to emit the result.  Since the only Observable associated with this &lt;em&gt;response emitter&lt;/em&gt; is the originator of the request, that's the only one that receives the emitted result.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To tie these things together, we need to pass the &lt;code&gt;id&lt;/code&gt; all the way through the request/response flow.  The requesting code generates the &lt;code&gt;id&lt;/code&gt; property, it's copied (by the server side code) to the corresponding response message, and finally retrieved again on the corresponding inbound message. Then we use it as described in step 3 above.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;The pattern we described in step 3 above is often referred to as using &lt;a href="https://www.enterpriseintegrationpatterns.com/patterns/messaging/CorrelationIdentifier.html"&gt;correlation ids&lt;/a&gt; (for example, here's a &lt;a href="https://www.rabbitmq.com/tutorials/tutorial-six-python.html"&gt;tutorial showing how&lt;/a&gt; a similar concept can be used for writing native RabbitMQ client apps).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Other Bookkeeping
&lt;/h4&gt;

&lt;p&gt;One related issue we kind of glossed over is managing the Faye (response channel) subscription process intelligently. We discussed the problem that we are essentially overwriting the &lt;em&gt;Faye subscription handler&lt;/em&gt; each time we handle a new request for a given pattern. So an additional consequence of our design goal of &lt;strong&gt;sharing a single inbound response channel per pattern&lt;/strong&gt; is that we we &lt;strong&gt;must have only a single active subscription&lt;/strong&gt; for the response channel, per pattern.&lt;/p&gt;

&lt;p&gt;When all active requests for a given pattern/topic have completed, we unsubscribe.  When a new request comes in, we subscribe again, and leave the subscription open until the channel quiesces (i.e., there are no more inflight requests) again.  We'll need to add some bookkeeping to improve our current behavior (of simply overwriting the handler with each new request on a pattern).&lt;/p&gt;

&lt;h4&gt;
  
  
  Connection Management
&lt;/h4&gt;

&lt;p&gt;One thing we'll find, as we integrate our code with the framework, is that we need to adhere to its expectations for how we provide a client library connection (i.e., the connection to the broker).  Since we're packaging up a bunch of stuff inside &lt;em&gt;observable subscriber functions&lt;/em&gt;, the framework expects us to provide access to the connection in a particular (RxJS friendly) way.  For the most part, we can just utilize some boilerplate to handle this.  There's only a small bit that is specific to a client library.  This is all packaged up in a &lt;code&gt;connect()&lt;/code&gt; method that we must implement in our &lt;code&gt;ClientFaye&lt;/code&gt; class.&lt;/p&gt;

&lt;h4&gt;
  
  
  Odds and Ends
&lt;/h4&gt;

&lt;p&gt;We'll also see sprinkled in some functions that deal with other loose ends:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;we should return channel names using a function, rather than hard coding them with the &lt;code&gt;_ack&lt;/code&gt; and &lt;code&gt;_res&lt;/code&gt; modifiers to avoid scattering magic strings throughout the code&lt;/li&gt;
&lt;li&gt;since user-land &lt;em&gt;patterns&lt;/em&gt; can be arbitrary objects (we use strings in these articles, but Nest allows constructs like &lt;code&gt;client.send({cmd: 'get-customers'}, {})&lt;/code&gt;), we'll use a built-in inherited function to "normalize" (essentially flatten) the pattern for internal usage&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Implementation
&lt;/h3&gt;

&lt;p&gt;With this strategy in mind, here's the outline of how we'll implement it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note that there's a fair amount of interaction back and forth between our &lt;code&gt;ClientFaye&lt;/code&gt; subclass and the &lt;code&gt;ClientProxy&lt;/code&gt; superclass it extends, so pay close attention to that, and I'll try to give clear signposts to guide you.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ol&gt;
&lt;li&gt;Unlike our &lt;em&gt;Take 1&lt;/em&gt; version, where we created a new standalone class, we're now going to start by extending the &lt;code&gt;ClientProxy&lt;/code&gt; class.  This is, of course, how we "plug in" to the framework.&lt;/li&gt;
&lt;li&gt;We're going to let the framework take over the implementation of &lt;code&gt;send()&lt;/code&gt; for us (at least the &lt;em&gt;shell&lt;/em&gt; of the implementation), rather than creating a concrete implementation ourselves, as we did in &lt;a href="https://dev.to/nestjs/part-4-basic-client-component-16f9"&gt;Part 4&lt;/a&gt;. The superclass &lt;code&gt;send()&lt;/code&gt; method is now the entry point for processing a user-land &lt;code&gt;send()&lt;/code&gt; call, and dictates how we plug in our pieces without disrupting the overall machinery.&lt;/li&gt;
&lt;li&gt;The superclass &lt;code&gt;send()&lt;/code&gt; method calls upon a concrete implementation of &lt;code&gt;publish()&lt;/code&gt;, which is where we'll implement the strategy we've been discussing, dealing with our &lt;em&gt;response emitter factory&lt;/em&gt; construct, unique identifiers, and so forth, as described above.&lt;/li&gt;
&lt;li&gt;We're going to implement a method to &lt;em&gt;unsubscribe&lt;/em&gt; from a Faye topic.&lt;/li&gt;
&lt;li&gt;We're going to handle events (&lt;code&gt;ClientProxy#emit()&lt;/code&gt;) by providing a concrete implementation of &lt;code&gt;dispatchEvent()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;We're going to beef up &lt;em&gt;connection management&lt;/em&gt;, as we mentioned above, to enable the framework to efficiently share a Faye client library connection across multiple calls.&lt;/li&gt;
&lt;li&gt;We'll take care of a few other minor details.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We have our shopping list, so let's get started!&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;em&gt;Take 2&lt;/em&gt; Code Review
&lt;/h3&gt;

&lt;h4&gt;
  
  
  The Superclass &lt;code&gt;send()&lt;/code&gt; Method
&lt;/h4&gt;

&lt;p&gt;Let's begin at a logical place, the &lt;code&gt;send()&lt;/code&gt; method.  As mentioned, we'll now let the superclass handle this for us, and plug in our code appropriately.  Let's take a look:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// From the ClientProxy superclass.  See source code here:&lt;/span&gt;
&lt;span class="c1"&gt;// https://github.com/nestjs/nest/blob/master/packages/microservices/client/client-proxy.ts#L82&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;TResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;TInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;TInput&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;TResult&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isNil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;isNil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;_throw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;InvalidMessageException&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;defer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="nx"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nx"&gt;mergeMap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
          &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="na"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Observer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;TResult&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createObserver&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
          &lt;span class="p"&gt;}),&lt;/span&gt;
      &lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Stripping this method to the bone, here's what it's doing:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Reusing a &lt;code&gt;connection&lt;/code&gt; if it exists, otherwise obtaining a new one.  Note that since it depends on client library particulars to deal with obtaining a connection, it depends on the &lt;code&gt;connect()&lt;/code&gt; method — which we are responsible for implementing and will get to soon.&lt;/li&gt;
&lt;li&gt;Creating and returning the Observable that is effectively the "container" within which we'll implement the strategy we discussed above.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's look at that Observable creation step for a moment.  The first line &lt;code&gt;const callback = this.createObserver(observer)&lt;/code&gt; is the call to the &lt;em&gt;response emitter factory&lt;/em&gt;, wherein we create our request-specific &lt;em&gt;response emitter&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The second line calls &lt;code&gt;publish()&lt;/code&gt;, which is an abstract method on the superclass that we must implement.  This is where we'll implement the custom details of our strategy.  Let's tackle that next. But first, let's note that &lt;code&gt;send()&lt;/code&gt; calls &lt;code&gt;publish()&lt;/code&gt; with two parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the arguments from the user-land &lt;code&gt;client.send()&lt;/code&gt; call (e.g., if the user wrote &lt;code&gt;client.send('/get-customers', {})&lt;/code&gt;, the first argument would contain &lt;code&gt;{pattern: '/get-customers', data: {}}&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;the newly minted &lt;em&gt;response emitter&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  The &lt;code&gt;ClientFaye#publish()&lt;/code&gt; Method
&lt;/h4&gt;

&lt;p&gt;This is where it all comes together.  Let's look at the whole method, then we'll break it down.  For now, I suggest you read the comments and lightly scan the code below.  We'll dive into the code in a moment.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// nestjs-faye-transporter/src/requestor/clients/faye-client.ts&lt;/span&gt;
&lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nx"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;partialPacket&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ReadPacket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;WritePacket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Function&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// prepare the outbound packet, and do other setup steps&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;packet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;assignPacketId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;partialPacket&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pattern&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;normalizePattern&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;partialPacket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;serializedPacket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;responseChannel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getResPatternName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="c1"&gt;// start the Faye subscription handler "bookkeeping"&lt;/span&gt;
      &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;subscriptionsCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscriptionsCount&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;responseChannel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="c1"&gt;// build the call to publish the request; in addition to making the Faye API&lt;/span&gt;
      &lt;span class="c1"&gt;// `publish()` call, when this function is called, it:&lt;/span&gt;
      &lt;span class="c1"&gt;// * updates the bookkeeping associated with the Faye subscription handler&lt;/span&gt;
      &lt;span class="c1"&gt;//   count&lt;/span&gt;
      &lt;span class="c1"&gt;// * stashes our *response emitter* (called `callback` below) in the map&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;publishRequest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;subscriptionsCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscriptionsCount&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;responseChannel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscriptionsCount&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;responseChannel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;subscriptionsCount&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;routingMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getAckPatternName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
          &lt;span class="nx"&gt;serializedPacket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;

      &lt;span class="c1"&gt;// build the Faye subscription handler&lt;/span&gt;
      &lt;span class="c1"&gt;// this function retrieves the *response emitter*&lt;/span&gt;
      &lt;span class="c1"&gt;// and binds it to the rest of the Faye subscription handler logic&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;subscriptionHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createSubscriptionHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

      &lt;span class="c1"&gt;// perform Faye `subscribe()` first (if needed), then Faye `publish()`&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;subscriptionsCount&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;subscription&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="nx"&gt;responseChannel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="nx"&gt;subscriptionHandler&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="nx"&gt;subscription&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;publishRequest&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;publishRequest&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="c1"&gt;// remember, this is an *observable subscriber function*, so it returns&lt;/span&gt;
      &lt;span class="c1"&gt;// the unsubscribe function.&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unsubscribeFromChannel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;responseChannel&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;routingMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With the time we put in on the strategy discussion, this code should hopefully make sense, at least at a high level.  Let's first dispense with a couple of the minor details so they don't distract, then we can focus on the core functionality.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;At the top of the method (remember, this method is called synchronously when a user-land request is made), we prepare the outbound packet (the &lt;em&gt;request&lt;/em&gt; message to be published), including assigning the packet &lt;code&gt;id&lt;/code&gt; and serializing the packet.&lt;/li&gt;
&lt;li&gt;Response channel subscription management should be straightforward to follow. We essentially keep a counter of &lt;em&gt;response channel&lt;/em&gt; subscriptions per pattern. We (logically speaking) do a &lt;code&gt;count++&lt;/code&gt; when publishing a request on that channel (pattern), and a &lt;code&gt;count--&lt;/code&gt; when receiving a response from the response channel. This let's us decide whether or not we need to subscribe to the response channel before publishing a request (solving our "we can only have one active subscription per pattern at a time" issue).&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  The &lt;code&gt;createSubscriptionHandler&lt;/code&gt; Method: Binding the Response Emitter
&lt;/h4&gt;

&lt;p&gt;Finally, let's talk about the call to &lt;code&gt;createSubscriptionHandler()&lt;/code&gt; — first at a high level, and then the details. First, let's recognize that this a factory that returns the &lt;em&gt;actual Faye subscription handler&lt;/em&gt; that gets bound to the Faye &lt;code&gt;subscribe()&lt;/code&gt; call on the response channel (i.e., &lt;code&gt;&amp;lt;message-pattern&amp;gt;_res&lt;/code&gt;).  In it, we &lt;strong&gt;do the late binding&lt;/strong&gt; of the &lt;em&gt;observable subscriber function&lt;/em&gt;. We do the late binding by looking up the &lt;em&gt;response emitter&lt;/em&gt; by &lt;code&gt;id&lt;/code&gt;, matching it with the request Observable, and calling it with the destructured inbound message fields. Here's the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;createSubscriptionHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ReadPacket&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;PacketId&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Function&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rawPacket&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deserializer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deserialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rawPacket&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt;
        &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rawPacket&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;ReadPacket&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;PacketId&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;
      &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isDisposed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;routingMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;

      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isDisposed&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
          &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;isDisposed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nx"&gt;callback&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
        &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's explore a little further. The function returned by this factory, as we said, is the &lt;em&gt;Faye subscription handler&lt;/em&gt;.  As such, it has only one argument (as dictated by the &lt;a href="https://faye.jcoglan.com/browser/subscribing.html"&gt;Faye API&lt;/a&gt;) — an actual Faye inbound message.  Here are the steps the returned function performs:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Deserialize the packet.&lt;/li&gt;
&lt;li&gt;Destructure the message so we can deal with its constituent values: &lt;code&gt;err&lt;/code&gt;, &lt;code&gt;response&lt;/code&gt;, &lt;code&gt;isDisposed&lt;/code&gt; and &lt;code&gt;id&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Use the map to lookup the correct &lt;em&gt;response emitter&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Discard any messages which are &lt;strong&gt;not&lt;/strong&gt; destined for this client&lt;sup&gt;1&lt;/sup&gt;
&lt;/li&gt;
&lt;li&gt;Finally, return the &lt;strong&gt;actual&lt;/strong&gt; &lt;em&gt;observable subscriber function&lt;/em&gt;.  Note: we automatically add &lt;code&gt;isDisposed: true&lt;/code&gt; if there's an error, to force the closure of the Observable.&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;sup&gt;1&lt;/sup&gt;This is the code fragment:&lt;br&gt;
 &lt;code&gt;if (!callback) {&lt;/code&gt;&lt;br&gt;
 &lt;code&gt;  return undefined;&lt;/code&gt;&lt;br&gt;
 &lt;code&gt;}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We take up discussion of this snippet in its own section immediately below.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With this, we've completed the process of converting to a late binding of the &lt;em&gt;observable subscriber function&lt;/em&gt;!  We have a couple more details to work through next.&lt;/p&gt;

&lt;h4&gt;
  
  
  Discarding Messages for Other Clients
&lt;/h4&gt;

&lt;p&gt;Consider that in a distributed microservice-based architecture, we may have &lt;em&gt;multiple&lt;/em&gt; clients (e.g., instances of &lt;code&gt;nestHttpApp&lt;/code&gt; or other &lt;em&gt;requestor&lt;/em&gt; apps), connected via the broker, to the same &lt;em&gt;responder&lt;/em&gt; (e.g., &lt;code&gt;nestMicroservice&lt;/code&gt;). In such a configuration, each client may issue the same requests (i.e., utilize the same message pattern).  When that happens, we'll have multiple clients communicating using the same Faye topic.  This is, of course, completely natural for Faye (and any message broker). However, since we may have multiple Faye subscribers on the same pattern/topic (e.g., &lt;code&gt;'/get-customers'&lt;/code&gt;), &lt;strong&gt;each&lt;/strong&gt; client (each subscriber) will be notified with &lt;strong&gt;any&lt;/strong&gt; response message that matches the response topic.  Only those that come from the originating client will have a matching &lt;code&gt;id&lt;/code&gt; and &lt;em&gt;response emitter&lt;/em&gt;.  We can surmise that non-matching responses are properly destined to be handled by other client instances. We can simply ignore these, returning &lt;code&gt;undefined&lt;/code&gt; to our &lt;em&gt;Faye subscription handler&lt;/em&gt;, which is essentially a &lt;em&gt;no-op&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Loose Ends
&lt;/h3&gt;

&lt;p&gt;Back in our massive discussion of the &lt;code&gt;publish()&lt;/code&gt; method, we briefly mentioned unsubscribing our &lt;em&gt;Faye subscription handler&lt;/em&gt; for a particular message pattern after the response channel quiesces.&lt;/p&gt;

&lt;p&gt;Let's talk about Observable life-cycle hooks briefly.  While it's possible for us to unsubscribe from an Observable in user-land (a &lt;code&gt;subscribe()&lt;/code&gt; call on an Observable returns a function to unsubscribe), that's rarely needed.  The RxJS Observer takes care of this automatically for us when the final event in a stream is emitted, and has a hook for any further cleanup we need to do.  In Nest transporter-land, this life-cycle event happens when we receive the last message in a stream, with the &lt;code&gt;isDisposed&lt;/code&gt; property set to true.&lt;/p&gt;

&lt;p&gt;From the Observable unsubscribe hook, we can run any cleanup code we need upon stream completion, such as determining whether to unsubscribe our &lt;em&gt;Faye subscription handler&lt;/em&gt;.  To do this, we call our custom cleanup code from that unsubscribe hook.  Remember, this is the function &lt;strong&gt;returned from&lt;/strong&gt; the &lt;em&gt;observable subscriber function&lt;/em&gt;, so we can expect to find it back in our &lt;code&gt;publish()&lt;/code&gt; method.  Let's take a look.  Note the function returned at the bottom of the &lt;code&gt;publish()&lt;/code&gt; method body.  That's our &lt;em&gt;unsubscribe hook&lt;/em&gt;.  From it, we can do any cleanup, as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// nestjs-faye-transporter/src/requestor/clients/faye-client.ts&lt;/span&gt;
      &lt;span class="p"&gt;...&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unsubscribeFromChannel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;responseChannel&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;routingMap&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;delete&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;
      &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We do two important things at the termination of each message stream:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We decide whether to unsubscribe for messages on that topic on the Faye response channel&lt;/li&gt;
&lt;li&gt;We delete the relevant &lt;em&gt;response emitter&lt;/em&gt; function from the map&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Deciding whether to unsubscribe requires us to revisit the subscription management topic. You should be able to connect the dots by looking at the &lt;code&gt;unsubscribeFromChannel()&lt;/code&gt; method shown below, and the discussion we had earlier about subscription bookkeeping.  Basically, we unsubscribe when the count of in-flight requests for a given pattern/topic reaches 0.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;  &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nx"&gt;unsubscribeFromChannel&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;subscriptionCount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscriptionsCount&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;subscriptionsCount&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;subscriptionCount&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;subscriptionCount&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;unsubscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Connection Management
&lt;/h3&gt;

&lt;p&gt;The framework follows a specific protocol for accessing the broker client library connection object. This protocol deals with connection establishment and connection errors as Observable events.  Rather than review this code in detail, we can safely say that it is &lt;em&gt;mostly boilerplate&lt;/em&gt;.  Let's call out the parts that are specific to Faye.&lt;/p&gt;

&lt;p&gt;We need to implement a &lt;code&gt;connect()&lt;/code&gt; method, which is tied to a class member called &lt;code&gt;connection&lt;/code&gt;.  Let's take a look:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;deserializer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;faye&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connect$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;ERROR_EVENT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;CONNECT_EVENT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;share&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toPromise&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;handleError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We populate &lt;code&gt;this.connection&lt;/code&gt; by first generating a live connection using the the library-specific protocol.  For Faye, this requires instantiating a &lt;code&gt;Client&lt;/code&gt; object, then calling &lt;code&gt;connect()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Once we have a connection, we run this through the framework by calling &lt;code&gt;this.connect$()&lt;/code&gt; as shown above.  This method takes the connection object (&lt;code&gt;this.fayeClient&lt;/code&gt;), and constants defining the events the client library defines for "success" and "failure". In the case of Faye, we define them in the file &lt;code&gt;nestjs-faye-transporter/src/constants.ts&lt;/code&gt;, and import those constants to this (the &lt;code&gt;faye-client.ts&lt;/code&gt;) file.&lt;/p&gt;

&lt;p&gt;In this manner, the framework uses a connection if it exists, or creates one if needed, and handles the connection lifecycle events in a uniform and efficient way.&lt;/p&gt;

&lt;p&gt;The call to &lt;code&gt;this.handleError()&lt;/code&gt; is optional, and let's us emit a log message in the event the connection fails.&lt;/p&gt;

&lt;p&gt;As mentioned, most of this is boilerplate; if you're integrating another broker, you can mostly just plug in the client library-specific calls where indicated for the Faye library calls above.&lt;/p&gt;

&lt;h3&gt;
  
  
  Event Handling
&lt;/h3&gt;

&lt;p&gt;The final feature to implement is &lt;em&gt;event handling&lt;/em&gt; — handling user-land &lt;code&gt;client.emit(...)&lt;/code&gt; calls.  As we found on the server side in Part 3, this is far simpler than dealing with &lt;em&gt;request/response&lt;/em&gt; style messaging because we are dealing with one-way messages: we simply send a message over the outbound (request) channel, and we're done.  No subscribing to responses necessary.&lt;/p&gt;

&lt;p&gt;Because this is straightforward and follows a predictable pattern, the framework handles most of this for us in the &lt;code&gt;ClientProxy#emit&lt;/code&gt; method.  Here's what that code looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// from https://github.com/nestjs/nest/blob/master/packages/microservices/client/client-proxy.ts&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nx"&gt;emit&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;TResult&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;TInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;TInput&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;TResult&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isNil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;isNil&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;_throw&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;InvalidMessageException&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;defer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;()).&lt;/span&gt;&lt;span class="nx"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nx"&gt;mergeMap&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dispatchEvent&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;})),&lt;/span&gt;
      &lt;span class="nx"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;source&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;ConnectableObservable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;TResult&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;source&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Here, once again, the framework is handling connection management for us, and all we really need to do is provide a concrete implementation for &lt;code&gt;dispatchEvent()&lt;/code&gt;.  The superclass defines an abstract &lt;code&gt;dispatchEvent&lt;/code&gt; method as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// from https://github.com/nestjs/nest/blob/master/packages/microservices/client/client-proxy.ts&lt;/span&gt;
&lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="kd"&gt;abstract&lt;/span&gt; &lt;span class="nx"&gt;dispatchEvent&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ReadPacket&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So our implementation simply needs to accept a request packet and publish the appropriate message using the Faye client API.  We need to wrap that in a promise and return the promise to match the signature above.&lt;/p&gt;

&lt;p&gt;Here's how we implement the &lt;code&gt;dispatchEvent()&lt;/code&gt; method in our Faye client component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;  &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nx"&gt;dispatchEvent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ReadPacket&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pattern&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;normalizePattern&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;serializedPacket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;serializedPacket&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The logic should be easy to follow.  We simply extract the pattern, normalize it, serialize the outbound packet (no need for an &lt;code&gt;id&lt;/code&gt; on this one), and then return a promise that resolves to the results of the &lt;code&gt;fayeClient.publish()&lt;/code&gt; call.  The framework handles the rest (which is really very little, other than efficient connection management).&lt;/p&gt;

&lt;h3&gt;
  
  
  Acceptance Testing
&lt;/h3&gt;

&lt;p&gt;At this point, we should have a complete implementation of the Faye Custom Transporter! 🍺 🍺 🍺!&lt;/p&gt;

&lt;p&gt;To test it, you'll want to start up the &lt;code&gt;nestHttpApp&lt;/code&gt; and &lt;code&gt;nestMicroservice&lt;/code&gt;, and run &lt;strong&gt;any&lt;/strong&gt; of the routes.  See &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/README.md#part-5-final-client-component"&gt;these notes&lt;/a&gt; for more help in running the tests.&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;We've come a long way! Thanks for hanging in there so long!  I hope you found this series useful.  Please hit me up in the comments with any questions!&lt;/p&gt;

&lt;p&gt;For those of you braving the task of writing your own Custom Transporter, please share your experiences in the comments.&lt;/p&gt;

&lt;p&gt;For those just interested in a deeper understanding of this key aspect of Nest Microservices, I hope this was helpful, and feel free to share your thoughts or questions in the comments!&lt;/p&gt;

&lt;h3&gt;
  
  
  What's Next
&lt;/h3&gt;

&lt;p&gt;I have a sixth article planned, in which I'll compare several of Nest's built-in transporter implementations to provide further insight into the nuances of each implementation. I'll gauge from the responses to this series whether that's something people would find useful, so please comment below to encourage me to write that final article! 😄&lt;/p&gt;

&lt;p&gt;Feel free to ask questions, make comments or suggestions, or just say hello in the comments below. And join us at &lt;a href="https://discord.gg/nestjs"&gt;Discord&lt;/a&gt; for more happy discussions about NestJS. I post there as &lt;em&gt;Y Prospect&lt;/em&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Part 4: Basic Client Component</title>
      <dc:creator>John Biundo</dc:creator>
      <pubDate>Wed, 04 Mar 2020 14:06:40 +0000</pubDate>
      <link>https://forem.com/nestjs/part-4-basic-client-component-16f9</link>
      <guid>https://forem.com/nestjs/part-4-basic-client-component-16f9</guid>
      <description>&lt;p&gt;&lt;em&gt;John is a member of the NestJS core team&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;This is Part 4 of a six-part series.  If you landed here from Google, you may want to start with &lt;a href="https://dev.to/nestjs/part-1-introduction-and-setup-1a2l"&gt;Part 1&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this article, we build the first iteration of the client component of the Faye Custom Transporter.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reminder&lt;/strong&gt;: Many of the concepts and terminology here are introduced and explained in &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3"&gt;this article series&lt;/a&gt;. That series serves as a good foundation for understanding the more advanced concepts covered in this series.&lt;/p&gt;

&lt;h4&gt;
  
  
  Get the Code
&lt;/h4&gt;

&lt;p&gt;All of the code in these articles is available &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample" rel="noopener noreferrer"&gt;here&lt;/a&gt;.  As always, these tutorials work best if you follow along with the code.  The &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/README.md" rel="noopener noreferrer"&gt;README&lt;/a&gt; covers all the steps you need to get the repository, build the apps, and follow along.  It's easy!  I strongly encourage you to do so.  Note that each article has a corresponding branch in the repository.  For example, this article (Part 4), has a corresponding branch called &lt;code&gt;part4&lt;/code&gt;. Read on, or &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/README.md#part-4-initial-client-component" rel="noopener noreferrer"&gt;get more details here&lt;/a&gt; on using the repository.&lt;/p&gt;

&lt;h4&gt;
  
  
  Git checkout the current version
&lt;/h4&gt;

&lt;p&gt;For this article, you should &lt;code&gt;git checkout&lt;/code&gt; the branch &lt;code&gt;part4&lt;/code&gt;.  You can get more information &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/README.md#part-4-initial-client-component" rel="noopener noreferrer"&gt;about the git repository branches here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Build the Apps for This Part
&lt;/h4&gt;

&lt;p&gt;For each article in the series, we introduce some new components (and sometimes entirely new &lt;strong&gt;projects&lt;/strong&gt;).  For convenience, at the start of each article, you should run the following command from the top-level directory (where you cloned the repo): *&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="c"&gt;# from root directory of project (e.g., transporter-tutorial, or whatever you chose as the root)&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;sh build.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;This is a shell script that runs on Linux-like systems.  You may have to make an adjustment for non-Linux systems.  Note that the script is simply a convenience that runs &lt;code&gt;npm install &amp;amp;&amp;amp; npm run build&lt;/code&gt;inside each top-level directory, so you can always fall back to that technique if you have trouble with the script.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Overview
&lt;/h3&gt;

&lt;p&gt;It's time to turn our attention to the client component. But let's start by recalling that we spent a lot of effort ensuring that the server component can return an observable stream of results to a requestor.  Let's have a quick visual to cement that idea, since we'll start right out with consuming that stream on the client side.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fcustom-transporter%2Fassets%2Fremote-stream3.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fcustom-transporter%2Fassets%2Fremote-stream3.gif" title="Remote Observable Stream" alt="Remote Observable Stream"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;a&gt;&lt;/a&gt;Figure 1: Remote Observable Stream



&lt;h3&gt;
  
  
  What's Next
&lt;/h3&gt;

&lt;p&gt;In this animated sequence, our &lt;code&gt;nestMicroservice&lt;/code&gt; &lt;em&gt;responder&lt;/em&gt; app responds to a &lt;em&gt;request&lt;/em&gt; message (&lt;code&gt;ClientProxy#send&lt;/code&gt;) by emitting a stream of three results (circles).  We saw in the last article how our transporter server component converts that stream to a sequence of 3 messages (boxes) to the broker, sending those messages on the response channel.  Our first meaningful task with the client component will be to consume those broker messages and convert them back into an Observable stream (circles) in the &lt;code&gt;ClientProxy&lt;/code&gt; layer so that our user-land controllers and services can use them as regular &lt;strong&gt;RxJS&lt;/strong&gt; observables.&lt;/p&gt;

&lt;p&gt;In this iteration (&lt;em&gt;Take 1&lt;/em&gt;), we'll take a similar approach to our first iteration of the server.  We'll get a basic functioning client working so we can focus on its responsibilities and the main code path.  Later, we'll add more robustness.  As with the server side, we'll ignore events (the &lt;code&gt;ClientProxy#emit&lt;/code&gt; path) for now.&lt;/p&gt;

&lt;h3&gt;
  
  
  First Iteration (Take 1) of the Client Component
&lt;/h3&gt;

&lt;p&gt;Let's get started.  First, let's look at how someone would use the client component of our Faye Custom Transporter.  Open the &lt;code&gt;app.controller.ts&lt;/code&gt; file for our &lt;code&gt;nestHttpApp&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// nestHttpApp/src/app.controller.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;Param&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;Body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;ClientFaye&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;InboundResponseIdentityDeserializer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;OutboundMessageIdentitySerializer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@faye-tut/nestjs-faye-transporter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;tap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;filter&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rxjs/operators&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;logger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;AppController&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nl"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ClientFaye&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ClientFaye&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://localhost:8000/faye&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;OutboundMessageIdentitySerializer&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
      &lt;span class="na"&gt;deserializer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;InboundResponseIdentityDeserializer&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We simply instantiate the client in the class constructor.  We could also use &lt;a href="https://docs.nestjs.com/microservices/basics#client" rel="noopener noreferrer"&gt;constructor injection&lt;/a&gt;.  &lt;strong&gt;Note&lt;/strong&gt;: one thing we can &lt;strong&gt;not&lt;/strong&gt; currently do is use the &lt;code&gt;@Client()&lt;/code&gt; decorator.  That's not really a significant limitation, as the decorator is merely a convenience, and is not the preferred way of instantiating a &lt;code&gt;ClientProxy&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The JSON options object we pass to &lt;code&gt;ClientFaye&lt;/code&gt; should be quite similar to the &lt;code&gt;options&lt;/code&gt; object we support on the server side, as it is either configuring the Faye connection, or passing some generic transporter options, like &lt;code&gt;serializer&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Once we instantiate a client like this, we can use it just like a &lt;code&gt;ClientProxy&lt;/code&gt; object to make requests and emit events. Let's take a look at one such request.  Further down in the &lt;code&gt;app.controller.ts&lt;/code&gt; file, notice this call.  Here, we make a call to &lt;code&gt;client.send()&lt;/code&gt; and — since, in this case, we want to manipulate the entire stream of results (what we do with those results isn't important here, we're just showing that we are wanting to access the stream) — we pipe that stream through the RxJS &lt;code&gt;tap()&lt;/code&gt; operator.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// nestHttpApp/src/app.controller.ts&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jobs-stream1/:duration&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Param&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;duration&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;duration&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/jobs-stream1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;duration&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="c1"&gt;// do notification&lt;/span&gt;
      &lt;span class="nf"&gt;tap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;step&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;step&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}),&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Take 1 Requirements
&lt;/h3&gt;

&lt;p&gt;The request above is the one represented in the animation at the top of the article.  Feel free to take a look at the server-side code (&lt;code&gt;nestMicroservice/src/app.controller.ts&lt;/code&gt;) to get familiar with what's happening here.  You can run this now by issuing an HTTP request to the &lt;code&gt;nestHttpClient&lt;/code&gt; app as shown below.  Assuming you've got your multi-terminal setup going, you'll be able to follow the full flow of this request and response through all of the components.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="c"&gt;# HTTPie command to request `/jobs-stream1` with base duration of 1&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;http get localhost:3000/jobs-stream1/1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also read the &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/observable-deepdive.md" rel="noopener noreferrer"&gt;deep dive on Observables&lt;/a&gt; for a thorough treatment of the elegance of streaming server side observable results back to the client.  For now, all we need to know is what we see in the animation above: when we make this request, we get a &lt;em&gt;stream of results&lt;/em&gt; in the form of a sequence of messages.&lt;/p&gt;

&lt;p&gt;Let's define our acceptance criteria for &lt;em&gt;Take 1&lt;/em&gt; as follows.  Our &lt;code&gt;ClientFaye&lt;/code&gt; object should let us send the &lt;code&gt;'/jobs-stream1'&lt;/code&gt; request, and handle the response, converting the message stream back into an Observable.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In &lt;a href="https://dev.to/nestjs/part-5-completing-the-client-component-hlh-temp-slug-2907984?preview=82c11163db963ca01d8d62d3a7b14843b422a6b28f46762d999bbe4b7035ad634d48bbbdd740e36376121aa673354ff5259f8b3028bceb931e800d9e"&gt;part5&lt;/a&gt; we'll complete the implementation and have a fully functioning Faye Custom Transporter — both client and server side.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Take 1 Strategy
&lt;/h3&gt;

&lt;p&gt;Before diving into the code, let's review our overall strategy.  At a top level, it's pretty simple. For each user-land &lt;em&gt;request&lt;/em&gt; (e.g., the &lt;code&gt;this.client.send(...)&lt;/code&gt; call in the &lt;code&gt;app.controller.ts&lt;/code&gt; file shown above), we want to issue the remote request and wait for the results, converting the response message(s) into an Observable stream.&lt;/p&gt;

&lt;p&gt;Issuing the request and waiting for the results using the broker API is familiar territory.  We'll just implement our very familiar &lt;em&gt;STRPTQ&lt;/em&gt; (&lt;em&gt;subscribe-to-the-response-then-publish-the-request&lt;/em&gt;) pattern to accomplish this.&lt;/p&gt;

&lt;p&gt;How do we convert the broker &lt;em&gt;message stream&lt;/em&gt; into an &lt;em&gt;Observable stream&lt;/em&gt;? We can use the Observable/observer pattern to do this. While I won't go into a lot of detail (you can &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/observable-quick-intro.md" rel="noopener noreferrer"&gt;read more, including simple examples, here&lt;/a&gt;), the basic idea is simple, and is the primary use case for Observables.  The easiest way to see this is through the code review below.&lt;/p&gt;

&lt;h3&gt;
  
  
  Take 1 Code Review
&lt;/h3&gt;

&lt;p&gt;Let's dive into the code.  Open the file &lt;code&gt;nestjs-faye-transporter/src/requestor/clients/faye-client.ts&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You might notice that in this iteration, we're not actually extending any framework class.  We'll need to do that in the next iteration, but it makes our code smaller and easier to focus on the core logic if we omit that in this step and just define a new class.&lt;/p&gt;

&lt;h4&gt;
  
  
  Class Definition
&lt;/h4&gt;

&lt;p&gt;Let's start with the class members and constructor:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// nestjs-faye-transporter/src/requestor/clients/faye-client.s&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ClientFaye&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;deserializer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;protected&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serializer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serializer&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;IdentitySerializer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deserializer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deserializer&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;IncomingResponseDeserializer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We should be familiar with the &lt;code&gt;serializer&lt;/code&gt; and &lt;code&gt;deserializer&lt;/code&gt; properties, and how they're initialized, from the server side.  The &lt;code&gt;fayeClient&lt;/code&gt; holds our connection to the Faye broker and is the object we'll use to make Faye client API calls.  The constructor just sets this stuff up.  It defaults to using the built-in (essentially &lt;strong&gt;no-op&lt;/strong&gt;) serializer and deserializer if the user doesn't provide one in the options.&lt;/p&gt;

&lt;h4&gt;
  
  
  Implementing &lt;code&gt;send()&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;With that covered, we can look at the API our class exposes.  First, recall that the user-land &lt;code&gt;send()&lt;/code&gt; call we need to support is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// nestHttpApp/src/app.controller.ts&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jobs-stream1/:duration&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Param&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;duration&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;duration&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/jobs-stream1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;duration&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="c1"&gt;// do notification&lt;/span&gt;
      &lt;span class="nf"&gt;tap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;step&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;step&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}),&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We expose this feature, of course, with the &lt;code&gt;send()&lt;/code&gt; method right at the top of the &lt;code&gt;faye-client.ts&lt;/code&gt; file, as shown here:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// nestjs-faye-transporter/src/requestor/clients/faye-client.ts&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Observable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;observer&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;handleRequest&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  The &lt;code&gt;send()&lt;/code&gt; Method Returns an Observable
&lt;/h4&gt;

&lt;p&gt;As required, &lt;code&gt;send()&lt;/code&gt; returns an &lt;code&gt;Observable&lt;/code&gt;. We need to delve into Observable-land here for a while. This can be confusing/frustrating if you're not familiar with the topic, but by the time you're finished here, I &lt;em&gt;think&lt;/em&gt; the 💡 will go on.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;send()&lt;/code&gt; method body, we construct an Observable using the &lt;em&gt;normal Observable creation pattern&lt;/em&gt;&lt;sup&gt;1&lt;/sup&gt; and return it to user-land.  Applying the normal Observable creation pattern in our context means we pass the constructor a function that &lt;strong&gt;accesses the Faye message stream and emits the values through our Observable&lt;/strong&gt;. Traditionally, this callback is called the &lt;em&gt;observable subscriber function&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Simple Observables (the ones you'll find in a lot of Medium articles 😉) often define this &lt;em&gt;observable subscriber function&lt;/em&gt; body right in place.  For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Observable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;observer&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;world&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But we already know that ours is going to be a little complex because the &lt;strong&gt;source&lt;/strong&gt; of the data we are going to emit comes from the Faye broker.  Specifically, it comes from the callback we're going to register in the Faye client API &lt;code&gt;subscribe()&lt;/code&gt; call which is invoked each time a message comes in on the response channel.  &lt;strong&gt;That&lt;/strong&gt; code — which listens through Faye for inbound messages — is the code that needs to run our &lt;code&gt;observer.next()&lt;/code&gt; (or equivalent) function call.&lt;/p&gt;

&lt;p&gt;In these more complex scenarios, to keep the code clean, we delegate this sophisticated &lt;em&gt;observer subscriber function&lt;/em&gt; to a specialized function. That's what we're doing in the code above by calling &lt;code&gt;handleRequest()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For the moment, you need to know two things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;handleRequest()&lt;/code&gt; &lt;strong&gt;is&lt;/strong&gt; our &lt;em&gt;observable subscriber function&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;handleRequest()&lt;/code&gt; is going to make a Faye client API call to subscribe to the response topic, causing it to register to receive future inbound requests.  As it receives them, it's going to emit the results in an Observable stream, analogous to our &lt;code&gt;observer.next('hello')&lt;/code&gt; statement above.  We'll see that code in a minute, but let's keep it abstract for another minute.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This can all seem kind of strange if you're not familiar with the Observable pattern&lt;sup&gt;1&lt;/sup&gt;. Let's see if it helps to think about it this way.  We're setting up callbacks at several levels in a sort of cascade.  At the "innermost" level, we have the &lt;em&gt;Faye subscription handler&lt;/em&gt; callback that will run when we get an inbound message on the response channel.  When this handler runs, we want to in turn trigger a callback &lt;strong&gt;through the Observer mechanism&lt;/strong&gt; to a higher-level callback — the user-land response handler (the code that looks like &lt;code&gt;pipe(tap(step) =&amp;gt; {...})&lt;/code&gt; in the &lt;code&gt;app.controller.ts&lt;/code&gt; file, shown above).&lt;/p&gt;

&lt;p&gt;Here are the connections you need to make:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;inner callback (broker) called when inbound message received...&lt;/li&gt;
&lt;li&gt;this triggers calling the &lt;em&gt;observable subscription function&lt;/em&gt;...&lt;/li&gt;
&lt;li&gt;which in turn &lt;em&gt;calls back to&lt;/em&gt; our user-land &lt;code&gt;client.send(...)&lt;/code&gt; subscription handler&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's what the &lt;code&gt;send()&lt;/code&gt; method is setting up with the Observable it creates.  One more thing about Observables to keep in mind.  What we just described effectively "sets up the cascade of callbacks", but these functions are &lt;strong&gt;cold&lt;/strong&gt; in the sense that all we've done is register callbacks. The act of &lt;strong&gt;subscribing&lt;/strong&gt; to the Observable kicks things in motion. The act of subscribing is the event that triggers the &lt;em&gt;observable subscriber function&lt;/em&gt; to run; when you think about that, you realize that "Aha! So this in turn causes the Faye API &lt;code&gt;subscribe()&lt;/code&gt; call inside &lt;code&gt;handleRequest()&lt;/code&gt; to run", and we see that this is how we start the stream flowing and turn the process from &lt;strong&gt;cold&lt;/strong&gt; to &lt;strong&gt;hot&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Another thing to know is that if the user-land &lt;code&gt;client.send()&lt;/code&gt; call doesn't &lt;strong&gt;explicitly&lt;/strong&gt; subscribe, but instead just &lt;strong&gt;returns&lt;/strong&gt; the result, Nest &lt;strong&gt;automatically&lt;/strong&gt; subscribes to the result. As a &lt;code&gt;ClientProxy&lt;/code&gt; developer, you don't have to worry about this.  But as a quick aside, note that this behavior is useful as a default, and works great in most situations.&lt;/p&gt;

&lt;p&gt;Now if you've clicked ahead to the next question and you're wondering &lt;em&gt;how does Nest return a potential stream of results&lt;/em&gt; over HTTP?  The answer is that it returns the &lt;strong&gt;last&lt;/strong&gt; result in the stream.  Again, in many cases this is fine.  In our user-land &lt;code&gt;client.send(...)&lt;/code&gt; call (shown again below), however, we want to access the values in the stream, thus we add a &lt;code&gt;pipe()&lt;/code&gt; operator that allows us to operate on the stream itself, once it's subscribed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// nestHttpApp/src/app.controller.ts&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jobs-stream1/:duration&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Param&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;duration&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;duration&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/jobs-stream1&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;duration&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="c1"&gt;// do notification&lt;/span&gt;
      &lt;span class="nf"&gt;tap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;step&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;step&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}),&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If this still isn't completely intuitive, I feel your pain, but I advise you to just let it marinate in the background and proceed.  Trust me, you can still follow most of this even if this still feels &lt;em&gt;strange&lt;/em&gt;.  If you really &lt;strong&gt;must scratch that itch&lt;/strong&gt;, see the footnote below.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;sup&gt;1&lt;/sup&gt;I'm assuming familiarity with this pattern. If you're an RxJS junky, this should be easy to follow.  If you're not, I'll try not to wave my hands too much. I too struggled with this concept for a while, so I've created a small &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/observable-quick-intro.md" rel="noopener noreferrer"&gt;side excursion here&lt;/a&gt; to help with this concept.  This should take you no more than 5-10 minutes to work through, and might be a helpful pre-requisite if some of this section has you a little boggled.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So let's dig into that &lt;code&gt;requestHandler()&lt;/code&gt; method.&lt;/p&gt;

&lt;h4&gt;
  
  
  The Request Handler
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;handleRequest()&lt;/code&gt; method is where we marry up our Observable pattern with our &lt;em&gt;request/response&lt;/em&gt; handling pattern. It's easiest if we decompose this and look at a version that abstracts out the Observable code first.  Then we'll see how it all wires together.&lt;/p&gt;

&lt;p&gt;In the first pseudocode-ish version below, we can focus directly on the &lt;em&gt;request/response&lt;/em&gt; handling pattern.  You'll quickly recognize this as our old friend the &lt;strong&gt;STRPTQ&lt;/strong&gt; pattern. (As a refresher, feel free to review how we implemented this functionality in our native Faye &lt;code&gt;customerApp&lt;/code&gt; way back in &lt;a href="https://dev.to/nestjs/part-1-introduction-and-setup-1a2l"&gt;Episode 1&lt;/a&gt;.  Here's a &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/part4/customerApp/src/customer-app.ts#L20" rel="noopener noreferrer"&gt;link to that code&lt;/a&gt;.  Or just open up &lt;code&gt;customerApp/src/customer-app.ts&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;With that in mind, the pseudocode-ish version of &lt;code&gt;handleRequest()&lt;/code&gt; below should be pretty clear.  I'll make a few explanatory comments below the code sample.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;handleRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;partialPacket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Function&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;packet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;partialPacket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;serializedPacket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;requestChannel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;_ack`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;responseChannel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;_res`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;subscriptionHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="cm"&gt;/* we'll fill this in shortly!  This function
      will provide the code we want to plug in as our *Faye subscription handler*.
      Guess what?  This is where we'll wire in our Observable :)
    */&lt;/span&gt;

    &lt;span class="c1"&gt;// Execute the familiar subscribe-to-response-then-publish-the-request pattern&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;subscription&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nx"&gt;responseChannel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;subscriptionHandler&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;subscription&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;requestChannel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;serializedPacket&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="cm"&gt;/*
      Handle any other Observable requirements
    */&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A few other notes from the code:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;The &lt;code&gt;partialPacket&lt;/code&gt; parameter has most of the elements of our outbound request message.  It's been invoked from &lt;code&gt;send()&lt;/code&gt; with an object like:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;pattern:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="err"&gt;'/jobs-stream&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="err"&gt;/'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;data:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;We complete the formation of our outbound packet by using &lt;code&gt;uuid()&lt;/code&gt; to add that pesky &lt;code&gt;id&lt;/code&gt; parameter (that we &lt;strong&gt;still&lt;/strong&gt; haven't talked about — I promise to address this in &lt;a href="https://dev.to/nestjs/part-5-completing-the-client-component-hlh-temp-slug-2907984?preview=82c11163db963ca01d8d62d3a7b14843b422a6b28f46762d999bbe4b7035ad634d48bbbdd740e36376121aa673354ff5259f8b3028bceb931e800d9e"&gt;Part 5&lt;/a&gt;!).&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;After running our serializer, the request packet is ready to send.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;In Faye, the &lt;code&gt;subscribe()&lt;/code&gt; call returns a promise that resolves when the Faye server returns an acknowledgement.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Upon that acknowledgement (promise resolution), we publish the serialized packet we built earlier.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now let's deal with marrying that up with our Observable.  Here's the final version of &lt;code&gt;handleRequest()&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;handleRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;partialPacket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Function&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;packet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;partialPacket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;serializedPacket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;requestChannel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;_ack`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;responseChannel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;_res`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;subscriptionHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;rawPacket&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deserializer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deserialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rawPacket&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isDisposed&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;isDisposed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isDisposed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;subscription&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nx"&gt;responseChannel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;subscriptionHandler&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="nx"&gt;subscription&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;requestChannel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;serializedPacket&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unsubscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;responseChannel&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's focus in on the newly added part:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;subscriptionHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;rawPacket&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deserializer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deserialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rawPacket&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;isDisposed&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;isDisposed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;isDisposed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;complete&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nx"&gt;observer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;next&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's review the purpose of this chunk of code.  This is (in terms of the concepts we started out with), our &lt;strong&gt;inner callback&lt;/strong&gt; — the function that will be passed in our Faye client API &lt;code&gt;subscribe()&lt;/code&gt; call to handle inbound messages.  As we handle each message in this function, this code converts the message back into a value in the Observable stream.  We do that by taking the following steps &lt;strong&gt;for each inbound message received from Faye&lt;/strong&gt;:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Deserialize it and destructure it, leaving us with an &lt;code&gt;error&lt;/code&gt;, &lt;code&gt;response&lt;/code&gt; and &lt;code&gt;isDisposed&lt;/code&gt; value for each inbound message&lt;/li&gt;
&lt;li&gt;Based on the contents of the message, determine if the &lt;strong&gt;inbound message stream&lt;/strong&gt; has errored or is complete&lt;/li&gt;
&lt;li&gt;Emit an Observable value, based on step 2&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As in any normal &lt;em&gt;observable subscriber function&lt;/em&gt;, we handle these cases with the &lt;code&gt;observer.error()&lt;/code&gt;, &lt;code&gt;observer.next()&lt;/code&gt; and &lt;code&gt;observer.complete()&lt;/code&gt; calls. If this confuses you, remember that all of this plugs into the Observable we created in our &lt;code&gt;send()&lt;/code&gt; method. You might take a minute to &lt;a href="https://en.wikipedia.org/wiki/Grok" rel="noopener noreferrer"&gt;grok&lt;/a&gt; all of this, and if it's still a little fuzzy, you can visit my &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/observable-quick-intro.md" rel="noopener noreferrer"&gt;mini Observable factory tutorial here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Handling Unsubscribe
&lt;/h4&gt;

&lt;p&gt;Any &lt;em&gt;observable subscriber function&lt;/em&gt; must return a way to unsubscribe from the Observable.  This is part of the Observable creation pattern.  The unsubscribe method can be called by the user; more often, it is called automatically when the Observable stream ends (e.g., when the final message is returned from our remote responder).&lt;/p&gt;

&lt;p&gt;The last bit of code in &lt;code&gt;handleRequest()&lt;/code&gt; fulfills this contract for our &lt;em&gt;observable subscriber function&lt;/em&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unsubscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;responseChannel&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The net effect of this is that when the stream completes (e.g., when the inbound message has &lt;code&gt;isDisposed&lt;/code&gt; set to true), we unsubscribe from the response channel.&lt;/p&gt;

&lt;h4&gt;
  
  
  The Rest of the Code
&lt;/h4&gt;

&lt;p&gt;The only other thing left is the connection management to the Faye broker.  That's handled in the &lt;code&gt;connect()&lt;/code&gt; method, which for now we're calling in the constructor.  This should be pretty understandable.  The only slightly funky thing is that we use destructuring to extract out the &lt;code&gt;url&lt;/code&gt; and the &lt;code&gt;options&lt;/code&gt; object from the constructor options so we can easily format our call to &lt;code&gt;fayeClient.connect()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Connection management will become more important and consequently more sophisticated in our next iteration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;deserializer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;faye&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Acceptance Testing
&lt;/h3&gt;

&lt;p&gt;We're ready to test our code.  In previous articles, I gave a lot of suggestions about managing your multiple terminals.  Going forward, I'll simply indicate the steps you should take, and assume you've got a working environment with the appropriate logical terminals set up.  Here are the steps to run our test:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Make sure our Faye server is up and running in one terminal.&lt;/li&gt;
&lt;li&gt;Make sure the &lt;code&gt;nestMicroservice&lt;/code&gt; app is running in one terminal.&lt;/li&gt;
&lt;li&gt;Make sure the &lt;code&gt;nestHttpApp&lt;/code&gt; app is running in one terminal.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, issue a request (expressed below as an HTTPie request, but use your favorite HTTP testing tool):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="c"&gt;# HTTPie request to get the jobs stream with a base duration of 1&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;http get localhost:3000/jobs-stream1/1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If all is working, you should get a coordinated set of logs in each of the terminal windows indicating that we have, indeed, successfully subscribed to the results of our remote request. You'll see our RxJS pipe come into play, making simulated calls to our notifier as each step in the job completes (as each message in the stream arrives). 🍺!&lt;/p&gt;

&lt;h3&gt;
  
  
  Limitations of Take 1
&lt;/h3&gt;

&lt;p&gt;We knowingly deferred a few things to &lt;em&gt;Take 2&lt;/em&gt; to keep focused on the core requirements and implementation of a client. We'll address them in Part 5.  But there's one &lt;strong&gt;big&lt;/strong&gt; issue lurking.  Any guesses on what that might be? I've definitely foreshadowed this along the way (&lt;strong&gt;hint&lt;/strong&gt;: repeatedly deferring the discussion of that pesky message &lt;code&gt;id&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;OK, let's get right to it. Ask yourself this: since our client is embedded in an HTTP app that is itself responding to multiple incoming requests, many of which in turn are triggering these &lt;code&gt;client.send(...)&lt;/code&gt; remote requests, how does the transporter client keep track of all of the outstanding remote requests and route them back to the correct originating (HTTP) request handler (i.e., the code with the original &lt;code&gt;client.send()&lt;/code&gt; call?&lt;/p&gt;

&lt;p&gt;Let's try it out. You'll need one extra terminal window for this to enable launching two simultaneous requests.  You'll notice that in this branch, there is an extra route in &lt;code&gt;nestHttpApp&lt;/code&gt;.  That route makes a new request (&lt;code&gt;'/race'&lt;/code&gt;), which has a corresponding &lt;code&gt;@MessagePattern()&lt;/code&gt; in &lt;code&gt;nestMicroservice&lt;/code&gt;.  The point of these new features is to enable us to launch multiple requests that &lt;strong&gt;overlap&lt;/strong&gt; in their execution.  Feel free to take a look at the code (these new routes/handlers are at the bottom of their respective Controller files), but all you really need to know is this: the &lt;code&gt;race&lt;/code&gt; route in our &lt;code&gt;nestHttpApp&lt;/code&gt; takes two parameters:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a requestId&lt;/li&gt;
&lt;li&gt;a delay period (in seconds) specifying how long the server should wait before responding&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, try making the following request (HTTPie format):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;http get localhost:3000/race/1/10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will return the following response after 10 seconds (&lt;code&gt;cid&lt;/code&gt; in the response corresponds to the first route parameter, and &lt;code&gt;delay&lt;/code&gt; in the response corresponds to the second parameter (expressed in milliseconds)):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"cid"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"customers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
            &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"fake"&lt;/span&gt;&lt;span class="w"&gt;
        &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
    &lt;/span&gt;&lt;span class="nl"&gt;"delay"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;10000&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;

&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this in place, we can run two overlapping requests. Let's go ahead and run the following two requests.  You'll need to do this in two separate terminals, and you should launch the second request within 5 seconds of launching the first (to achieve the desired overlap):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="c"&gt;# run this in terminal 1, and be prepared to run the second one in terminal 2 a few seconds later&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;http get localhost:3000/race/1/10
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="c"&gt;# run this one in terminal 2, within a couple of seconds of running the top 1&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;http get localhost:3000/race/2/5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The results may (well, maybe not with all the foreshadowing 😉) surprise you.  &lt;strong&gt;Both&lt;/strong&gt; requests return the following result:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"cid"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"2"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"customers"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="nl"&gt;"id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
          &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"fake"&lt;/span&gt;&lt;span class="w"&gt;
      &lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"delay"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="mi"&gt;5000&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is obviously incorrect for the first request.  The issue demonstrates what I'll refer to as the &lt;em&gt;&lt;a href="http://250bpm.com/blog:18" rel="noopener noreferrer"&gt;multiplexing&lt;/a&gt; challenge&lt;/em&gt;.  That is, we are multiplexing our requests for each &lt;em&gt;pattern&lt;/em&gt; through a single pair of channels.  When we issue request #1, it subscribes to a response on the &lt;code&gt;'/race_res'&lt;/code&gt; channel. Request #2 does the same thing, and since request #2 finishes first, the request #1 subscription gets &lt;strong&gt;request #2's result&lt;/strong&gt; (the wrong answer!) on the response channel.&lt;/p&gt;

&lt;p&gt;Also, there's another nagging question... why do &lt;strong&gt;both&lt;/strong&gt; subscribers get the result?  Hmmm... clearly something isn't working quite right here. We'll have to dig in and work on this in our next (and final!) revision!&lt;/p&gt;

&lt;h3&gt;
  
  
  What's Next
&lt;/h3&gt;

&lt;p&gt;With these issues in mind, we're ready to build a final version of the Faye Custom Transporter client component, completing our development project.  In &lt;a href="https://dev.to/nestjs/part-4-basic-client-component-16f9"&gt;Part 5&lt;/a&gt; we cover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Solving the multiplexing issue&lt;/li&gt;
&lt;li&gt;Client library connection management&lt;/li&gt;
&lt;li&gt;Event handling on the client side&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Feel free to ask questions, make comments or suggestions, or just say hello in the comments below. And join us at &lt;a href="https://discord.gg/nestjs" rel="noopener noreferrer"&gt;Discord&lt;/a&gt; for more happy discussions about NestJS. I post there as &lt;em&gt;Y Prospect&lt;/em&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Part 3: Completing the Server Component</title>
      <dc:creator>John Biundo</dc:creator>
      <pubDate>Mon, 02 Mar 2020 18:02:41 +0000</pubDate>
      <link>https://forem.com/nestjs/part-3-completing-the-server-component-4a80</link>
      <guid>https://forem.com/nestjs/part-3-completing-the-server-component-4a80</guid>
      <description>&lt;p&gt;&lt;em&gt;John is a member of the NestJS core team&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;This is Part 3 of a six-part series.  If you landed here from Google, you may want to start with &lt;a href="https://dev.to/nestjs/part-1-introduction-and-setup-1a2l"&gt;Part 1&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this article, we build the final iteration of the server component of our Faye Custom Transporter.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reminder&lt;/strong&gt;: Many of the concepts and terminology here are introduced and explained in &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3"&gt;this article series&lt;/a&gt;. That series serves as a good foundation for understanding the more advanced concepts covered in this series.&lt;/p&gt;

&lt;h4&gt;
  
  
  Get the Code
&lt;/h4&gt;

&lt;p&gt;All of the code in these articles is available &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample" rel="noopener noreferrer"&gt;here&lt;/a&gt;.  As always, these tutorials work best if you follow along with the code.  The &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/README.md" rel="noopener noreferrer"&gt;README&lt;/a&gt; covers all the steps you need to get the repository, build the apps, and follow along.  It's easy!  I strongly encourage you to do so.  Note that each article has a corresponding branch in the repository.  For example, this article (Part 3), has a corresponding branch called &lt;code&gt;part3&lt;/code&gt;. Read on, or &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/README.md#part-3-completing-the-server-component" rel="noopener noreferrer"&gt;get more details here&lt;/a&gt; on using the repository.&lt;/p&gt;

&lt;h4&gt;
  
  
  Git checkout the current version
&lt;/h4&gt;

&lt;p&gt;For this article, you should &lt;code&gt;git checkout&lt;/code&gt; the branch &lt;code&gt;part3&lt;/code&gt;.  You can get more information &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/README.md#part-3-completing-the-server-component" rel="noopener noreferrer"&gt;about the git repository branches here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Build the Apps for This Part
&lt;/h4&gt;

&lt;p&gt;For each article in the series, we introduce some new components (and sometimes entirely new &lt;strong&gt;projects&lt;/strong&gt;).  For convenience, at the start of each article, you should run the following command from the top-level directory (where you cloned the repo): *&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="c"&gt;# from root directory of project (e.g., transporter-tutorial, or whatever you chose as the root)&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;sh build.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;This is a shell script that runs on Linux-like systems.  You may have to make an adjustment for non-Linux systems.  Note that the script is simply a convenience that runs &lt;code&gt;npm install &amp;amp;&amp;amp; npm run build&lt;/code&gt;inside each top-level directory, so you can always fall back to that technique if you have trouble with the script.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Overview
&lt;/h3&gt;

&lt;p&gt;I ended the last article with a discussion of the shortcomings of our &lt;em&gt;Take 1&lt;/em&gt; (first iteration) implementation of the Faye Custom Transporter server component.  In this article, we'll address those shortcomings. Most of this work is fairly straightforward, but I want to take a moment to discuss the "Observable issue" a bit further.&lt;/p&gt;

&lt;p&gt;You &lt;strong&gt;could actually ignore&lt;/strong&gt; this issue entirely.  The server component we built in the last chapter is functional, and we'll clean up a few loose ends here to make it typesafe, etc. However, by ignoring proper handling of Observables, you'd be leaving out a feature that you may not miss until you &lt;strong&gt;really&lt;/strong&gt; need it.  Plus, let me hit you with the full commercial:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It's super fun to understand how it works, and if you're not an &lt;a href="https://rxjs-dev.firebaseapp.com/" rel="noopener noreferrer"&gt;RxJS&lt;/a&gt; whiz, don't worry, that won't stop you (&lt;strong&gt;and&lt;/strong&gt; you might even pick up a few nifty tricks about RxJS along the way)&lt;/li&gt;
&lt;li&gt;It builds a deeper appreciation and understanding of the elegance of the NestJS framework&lt;/li&gt;
&lt;li&gt;It's really, really easy to implement, once you get on the happy path&lt;/li&gt;
&lt;li&gt;The feature provides some amazing benefits, virtually for free&lt;/li&gt;
&lt;li&gt;Your transporter really &lt;strong&gt;should&lt;/strong&gt; be plug-and-play compatible with other transporters to be a good Nest citizen (Nestizen? 😃)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I could go on and on, but instead, I've encapsulated all the &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/observable-deepdive.md" rel="noopener noreferrer"&gt;concrete examples and detailed analysis here&lt;/a&gt; to try to keep this tutorial on track.  Suffice to say, I strongly encourage you to follow that link and take that little side trip to both understand why this is an important step — and because you might even get inspired to use Nest microservices in some new and interesting ways.&lt;/p&gt;

&lt;p&gt;With all that said, let's jump in!&lt;/p&gt;

&lt;h3&gt;
  
  
  Addressing Observables
&lt;/h3&gt;

&lt;p&gt;After that build-up, you might think we're about to embark on some form of brain surgery (maybe I should say "🚀 science" in keeping with the trekkie theme? 😉).  Fortunately, that's not really the case.  Let's start by considering the requirements.&lt;/p&gt;

&lt;p&gt;At the highest level, we need to consider the possibility that the user-land handler returns a stream (Observable that emits multiple values). Our earlier version won't handle that (as we saw at the end of the last article).  The problem lies with our response message publishing logic being unaware of a response &lt;strong&gt;stream&lt;/strong&gt;.  It simply awaits a single response and publishes.  Let's take a quick look at the part of the code that publishes to refresh ourselves:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// nestjs-faye-transporter/src/responder/transporters/server-faye.ts&lt;/span&gt;
&lt;span class="c1"&gt;// on branch part2&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;getMessageHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Function&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;inboundPacket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deserializer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deserialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inboundPacket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;outboundRawPacket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;err&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;isDisposed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;outboundPacket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;outboundRawPacket&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;_res`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;outboundPacket&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We could try to handle this streaming ourselves, but it's a tad tricky bit of code, and as such, a great candidate for the framework to handle for us. Which it does! As always with frameworks, if we want to delegate a responsibility, we have to invert our thinking a bit. Rather than publishing directly, we instead need to &lt;strong&gt;build&lt;/strong&gt; a publish function and hand it to the framework&lt;/p&gt;

&lt;p&gt;Now, here's the new, improved, &lt;em&gt;Observable-aware&lt;/em&gt; version. This is what you'll see now (on the current branch, &lt;code&gt;part3&lt;/code&gt;) when you open the &lt;code&gt;nestjs-faye-transporter/src/responder/transporters/server-faye.ts&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//nestjs-faye-transporter/src/responder/transporters/server-faye.ts&lt;/span&gt;
&lt;span class="c1"&gt;// on branch part3&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;getMessageHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Function&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ReadPacket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;inboundPacket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deserializer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deserialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fayeCtx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FayeContext&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;transformToObservable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inboundPacket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fayeCtx&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;publish&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;response&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;outgoingResponse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;_res`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;outgoingResponse&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="nx"&gt;response$&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response$&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's discuss the differences.  For now, ignore the &lt;code&gt;fayeCtx&lt;/code&gt; object — we'll cover that below.&lt;/p&gt;

&lt;p&gt;The reorganized code does two main things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;It converts our handler response to an Observable&lt;/li&gt;
&lt;li&gt;It builds a publish function that it can delegate to the framework (which will then use it to publish a stream if that's what the user-land handler returns)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For the first item, we call a built-in (inherited from &lt;code&gt;Server&lt;/code&gt;) utility to convert the response to an Observable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;transformToObservable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inboundPacket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{}),&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This step is pretty intuitive, and the utility is correspondingly pretty simple.  Feel free to take a moment to review the &lt;a href="https://github.com/nestjs/nest/blob/master/packages/microservices/server/server.ts#L108" rel="noopener noreferrer"&gt;&lt;code&gt;transformToObservable()&lt;/code&gt; method's source code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Building the publish function is also fairly straightforward.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;publish&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;outgoingResponse&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;_res`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;outgoingResponse&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This code is very similar to the way we published in the &lt;em&gt;Take 1&lt;/em&gt; version (from branch &lt;code&gt;part2&lt;/code&gt;), but now packaged as a function we can hand off to the framework.  Study it for a moment and make sure you understand it — it should be pretty straightforward. The &lt;code&gt;Object.assign(...)&lt;/code&gt; code's job is to copy the &lt;code&gt;id&lt;/code&gt; field from the inbound request to the outbound response.  We did that in the earlier version as well, but there we were defining the response object in place, so the technique was a little different.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: We still haven't discussed &lt;strong&gt;why&lt;/strong&gt; we need that &lt;code&gt;id&lt;/code&gt;.  For now, just trust the process 😃.  Don't worry, we'll cover this in &lt;a href="https://dev.to/nestjs/part-4-basic-client-component-16f9"&gt;Part 5&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Finally, we delegate the publishing step to the framework with the final line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;response$&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response$&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;send()&lt;/code&gt; method is inherited from &lt;code&gt;Server&lt;/code&gt;; it &lt;strong&gt;takes care of the details of dealing with streams&lt;/strong&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The significance of the previous sentence should not be overlooked.  To fully appreciate both &lt;strong&gt;why&lt;/strong&gt; this is so useful and how the framework makes this as easy as delegating a &lt;code&gt;publish()&lt;/code&gt; function, you really should &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/observable-deepdive.md" rel="noopener noreferrer"&gt;read this deep dive&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Anyway, let's discuss what the framework is doing for us.  As mentioned, &lt;code&gt;send()&lt;/code&gt; is inherited from &lt;code&gt;Server&lt;/code&gt;.  See &lt;a href="https://github.com/nestjs/nest/blob/master/packages/microservices/server/server.ts" rel="noopener noreferrer"&gt;here&lt;/a&gt; for the full source code of the &lt;code&gt;Server&lt;/code&gt;.  Let's reproduce it below to get a feel for it.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: we &lt;strong&gt;don't need to fully understand&lt;/strong&gt; how this method works, and I won't belabor it here.  It's fair to treat it as a black box, as long as we understand &lt;strong&gt;how to use it&lt;/strong&gt;.  We'll make sure we do!&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// from https://github.com/nestjs/nest/blob/master/packages/microservices/server/server.ts&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;stream$&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;respond&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;WritePacket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Subscription&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;dataBuffer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;WritePacket&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;scheduleOnNextTick&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;WritePacket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;dataBuffer&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;dataBuffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
        &lt;span class="nx"&gt;process&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;nextTick&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;dataBuffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buffer&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;respond&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;buffer&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
          &lt;span class="nx"&gt;dataBuffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isDisposed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;dataBuffer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;dataBuffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;concat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nx"&gt;dataBuffer&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;dataBuffer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;isDisposed&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isDisposed&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;stream$&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pipe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nf"&gt;catchError&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nf"&gt;scheduleOnNextTick&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;response&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
          &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;empty&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}),&lt;/span&gt;
        &lt;span class="nf"&gt;finalize&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;scheduleOnNextTick&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;isDisposed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;})),&lt;/span&gt;
      &lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="nf"&gt;scheduleOnNextTick&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;err&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
      &lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While at first a little obscure, this really isn't &lt;strong&gt;that&lt;/strong&gt; hard to understand if you work through it, though it does require some understanding of observables.  Essentially, we are subscribing to the response coming back from our user-land handler. If the subscription produces multiple values (a stream), we buffer them (&lt;code&gt;dataBuffer&lt;/code&gt;), then publish each value using the &lt;code&gt;publish()&lt;/code&gt; function we built a moment ago (inside this method the &lt;code&gt;publish()&lt;/code&gt; function is accessed as the &lt;code&gt;respond&lt;/code&gt; parameter). When the stream completes, we signify that by setting &lt;code&gt;isDisposed&lt;/code&gt; to true on the final emitted response.&lt;/p&gt;

&lt;h3&gt;
  
  
  Handle Events
&lt;/h3&gt;

&lt;p&gt;Thus far we've skipped handling events — those user-written handlers that are decorated with &lt;code&gt;@EventPattern(...)&lt;/code&gt;.  Let's take care of those now.  They turn out to be a lot easier than &lt;em&gt;request/response&lt;/em&gt; type messages precisely because they don't require any response.  Take a look at the &lt;code&gt;bindHandlers()&lt;/code&gt; method of &lt;code&gt;server-faye.ts&lt;/code&gt;. The comments should pretty much explain the simple change we've made here to handle events.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// nestjs-faye-transporter/src/responder/transporters/server-faye.ts&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;bindHandlers&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/**
   * messageHandlers is populated by the Framework (on the `Server` superclass)
   *
   * It's a map of `pattern` -&amp;gt; `handler` key/value pairs
   * `handler` is the handler function in the user's controller class, decorated
   * by `@MessageHandler()` or `@EventHandler`, along with an additional boolean
   * property indicating its Nest pattern type: event or message (i.e.,
   * request/response)
   */&lt;/span&gt;
  &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;messageHandlers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// In this version (`part3`) we add the handler for events&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isEventHandler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// The only thing we need to do in the Faye subscription callback for&lt;/span&gt;
      &lt;span class="c1"&gt;// an event, since it doesn't return any data to the caller, is read&lt;/span&gt;
      &lt;span class="c1"&gt;// and decode the request, pass the inbound payload to the user-land&lt;/span&gt;
      &lt;span class="c1"&gt;// handler, and await its completion.  There's no response handling,&lt;/span&gt;
      &lt;span class="c1"&gt;// hence we don't need all the complexity of `getMessageHandler()`&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;rawPacket&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ReadPacket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fayeCtx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FayeContext&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;packet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parsePacket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rawPacket&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deserializer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deserialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;});&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fayeCtx&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;_ack`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getMessageHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two additional (unrelated) changes that show up on this branch are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The use of the &lt;code&gt;FayeContext&lt;/code&gt; object. We'll cover that in the next section.&lt;/li&gt;
&lt;li&gt;Passing &lt;code&gt;{ channel: pattern }&lt;/code&gt; to the &lt;code&gt;deserialize()&lt;/code&gt; call.  We'll cover that later in the article.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Subscription Context
&lt;/h3&gt;

&lt;p&gt;Consider what happens when we subscribe to a topic via the broker API.  With Faye, it's straightforward.  We register a &lt;em&gt;Faye subscription handler&lt;/em&gt; on a topic, and when a message matching that topic arrives, our handler is called with the message payload containing &lt;strong&gt;only what the publisher sent&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For some brokers, the subscription handler can receive additional context about the message — sometimes in the callback parameters, and sometimes in the message payload.  For example, with NATS, the message payload packs the actual content sent by the publisher in a top-level &lt;code&gt;data&lt;/code&gt; property, and adds two additional top-level properties with context metadata: one describing the topic the caller was subscribed to, and one containing the optional &lt;code&gt;reply&lt;/code&gt; topic. We refer to this metadata as &lt;code&gt;Context&lt;/code&gt;, and Nest allows you to pass this information through to the user-land handler (method decorated by &lt;code&gt;@MessagePattern()&lt;/code&gt; or &lt;code&gt;@EventPattern()&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Since Faye simply doesn't provide any such metadata, we'll demonstrate this behavior by passing the &lt;em&gt;channel&lt;/em&gt; in this context object.  This is not particularly useful in this case, as we're not actually providing any &lt;strong&gt;new&lt;/strong&gt; information, but with Faye it's the best we can do to demonstrate the concept of the &lt;code&gt;Context&lt;/code&gt; object.&lt;/p&gt;

&lt;h4&gt;
  
  
  Creating the context object
&lt;/h4&gt;

&lt;p&gt;For any given broker, the technique for creating the context object may vary, based on the discussion above. In the end, you'll need to extract any context information (from the message payload, if context is encoded there, or from the &lt;code&gt;subscribe()&lt;/code&gt; callback parameters) and pass it to the user-land handlers. We use a custom object to pass context.  Open the file &lt;code&gt;nestjs-faye-transporter/src/responder/ctx-host/faye-context.ts&lt;/code&gt;.  It's a pretty simple object that extends &lt;code&gt;BaseRpcContext&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// nestjs-faye-transporter/src/responder/ctx-host/faye-context.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;BaseRpcContext&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/microservices/ctx-host/base-rpc.context&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;FayeContextArgs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;FayeContext&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;BaseRpcContext&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;FayeContextArgs&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;FayeContextArgs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="cm"&gt;/**
   * Returns the name of the Faye channel.
   */&lt;/span&gt;
  &lt;span class="nf"&gt;getChannel&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Essentially, it keeps an array of context values — in our case just strings, though you can specify a more complex type if you need to.  For example, &lt;a href="https://github.com/nestjs/nest/blob/master/packages/microservices/ctx-host/mqtt.context.ts#L3" rel="noopener noreferrer"&gt;MQTT&lt;/a&gt; uses:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// from https://github.com/nestjs/nest/blob/master/packages/microservices/ctx-host/mqtt.context.ts&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;MqttContextArgs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MqttContext&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;BaseRpcContext&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;MqttContextArgs&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;args&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MqttContextArgs&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For each context value type (e.g., "channel", in our case, used for carrying the channel/pattern name), you supply a getter function.  Remember, this is a simple data structure as there are generally very few context values, so your getter is just pulling out a particular row of the array.&lt;/p&gt;

&lt;h4&gt;
  
  
  Passing Context to User-land Handlers
&lt;/h4&gt;

&lt;p&gt;With this in place, we then need to simply find the right places to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Instantiate a &lt;code&gt;FayeContext&lt;/code&gt; object with the correct values&lt;/li&gt;
&lt;li&gt;Pass it to the user-land handlers&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;We already encountered this behavior in the previous sections. Let's look again at the &lt;code&gt;getMessageHandler()&lt;/code&gt; method, focusing on the relevant snippet from that method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ReadPacket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;inboundPacket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deserializer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deserialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fayeCtx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FayeContext&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response$&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;transformToObservable&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inboundPacket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fayeCtx&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It should be clear how we're using an instance of &lt;code&gt;FayeContext&lt;/code&gt; here.  We instantiate it with the relevant context, and then pass the instance to our user-land handler method.  The code above takes care of context for &lt;em&gt;request/response&lt;/em&gt; type handlers. We also need to handle this appropriately for &lt;em&gt;events&lt;/em&gt;. We saw where that happened in the last section, in the &lt;code&gt;bindHandlers()&lt;/code&gt; method.  Here's the relevant snippet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isEventHandler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// The only thing we need to do in the Faye subscription callback for&lt;/span&gt;
      &lt;span class="c1"&gt;// an event, since it doesn't return any data to the caller, is read&lt;/span&gt;
      &lt;span class="c1"&gt;// and decode the request, pass the inbound payload to the user-land&lt;/span&gt;
      &lt;span class="c1"&gt;// handler, and await its completion.  There's no response handling,&lt;/span&gt;
      &lt;span class="c1"&gt;// hence we don't need all the complexity of `getMessageHandler()`&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rawPacket&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ReadPacket&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fayeCtx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;FayeContext&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;packet&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parsePacket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rawPacket&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deserializer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deserialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;fayeCtx&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Remember, where and how you gather the context for any particular broker may vary, but the concept remains the same.&lt;/p&gt;

&lt;h4&gt;
  
  
  Testing Context
&lt;/h4&gt;

&lt;p&gt;In this branch (&lt;code&gt;part3&lt;/code&gt;), we provided a new project, &lt;code&gt;nestHttpApp&lt;/code&gt;.   This is a simple Nest HTTP app that includes a few routes for testing our &lt;code&gt;nestMicroservice&lt;/code&gt; over the Faye transporter. It's worth mentioning that this branch also includes a &lt;strong&gt;full final version&lt;/strong&gt; of the &lt;code&gt;ClientFaye&lt;/code&gt; subclass of &lt;code&gt;ClientProxy&lt;/code&gt; — this is necessary for our &lt;code&gt;nestHttpApp&lt;/code&gt; to instantiate a client to be able to run &lt;code&gt;ClientProxy#send()&lt;/code&gt; and &lt;code&gt;ClientProxy#emit()&lt;/code&gt; calls.  We'll actually build that &lt;code&gt;ClientFaye&lt;/code&gt; component from scratch in the next two articles, but it's sitting there in this branch to help you test.  With this in place, you can see the context by following these steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Start up the &lt;code&gt;nestHttpApp&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;If you ran the &lt;code&gt;build.sh&lt;/code&gt; script as described at the beginning of this chapter, all of this project's dependencies have been installed.  If not, simply open a terminal in the top level &lt;code&gt;nestHttpApp&lt;/code&gt; directory and run &lt;code&gt;npm install&lt;/code&gt;.  Then, start the application with &lt;code&gt;npm run start:dev&lt;/code&gt;.&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Issue an HTTP request like:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;GET /customers&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; You can do this in any number of ways — via a browser, browsing to &lt;code&gt;localhost:3000/customers&lt;/code&gt;, via something like &lt;a href="https://httpie.org/" rel="noopener noreferrer"&gt;HTTPie - my favorite&lt;/a&gt;, or with a desktop program like &lt;a href="https://www.postman.com/" rel="noopener noreferrer"&gt;Postman&lt;/a&gt;. See &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/README.md#part-3-completing-the-server-component" rel="noopener noreferrer"&gt;here&lt;/a&gt; for more help on running these requests.&lt;/p&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Keep an eye on the &lt;code&gt;nestMicroservice&lt;/code&gt; app's console log.  You should see a line like the following, demonstrating that we've successfully passed the &lt;code&gt;FayeContext&lt;/code&gt; data to the handler:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;[AppController] Faye Context: {"args":["/get-customers"]}"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Deserialization Options
&lt;/h3&gt;

&lt;p&gt;The transporter main class we inherit from (&lt;code&gt;Server&lt;/code&gt;) has hooks for calling custom serialization and deserialization methods to encode/decode broker messages. I cover this topic extensively &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-3-4m20"&gt;in this article&lt;/a&gt;.  The main use case is for translating between non-Nest (i.e., external) message formats and the internal &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-2-3hgd"&gt;Nest microservice transporter message protocol&lt;/a&gt;. One thing we need to do to fully enable custom deserialization (i.e., let users hook their own custom deserializer class) with our custom transporter is fully implement the call to the &lt;code&gt;Deserializer#deserialize()&lt;/code&gt; method.  Here's its interface:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Deserializer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;TInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;TOutput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;deserialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;TInput&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nb"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;TOutput&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means we can pass in some context via a second argument that the user can access as the &lt;code&gt;options&lt;/code&gt; parameter to utilize in their custom deserialization code.  Usually this context is just the &lt;em&gt;channel&lt;/em&gt; (pattern), but it can also include other contextual information.  For example, the &lt;a href="https://github.com/nestjs/nest/blob/master/packages/microservices/server/server-nats.ts#L94" rel="noopener noreferrer"&gt;NATS transporter&lt;/a&gt; passes the following information (note the &lt;code&gt;replyTo&lt;/code&gt; property):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deserializer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deserialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;rawMessage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;replyTo&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In our case, with Faye, we'll stick to the usual pattern and pass the channel.  This is done in &lt;code&gt;bindHandlers()&lt;/code&gt;, where we added the deserialization context as shown below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deserializer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deserialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this in place, any user-written deserializer will have access to the channel name to make decisions about transforming inbound messages. I encourage you to read this &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-3-4m20"&gt;article&lt;/a&gt; for a deeper understanding of deserialization to help inform what you should pass as deserialization options.&lt;/p&gt;

&lt;h3&gt;
  
  
  Type Safety
&lt;/h3&gt;

&lt;p&gt;In this branch, we added types to several calls to help ensure type safety when end users use the custom transporter.  You can examine the code and explore how we use the following newly imported interfaces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// nestjs-faye-transporter/src/responder/transporters/server-faye.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;FayeClient&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../../external/faye-client.interface&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;FayeOptions&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../../interfaces/faye-options.interface&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's take a quick moment to examine the &lt;code&gt;FayeOptions&lt;/code&gt; interface:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// nestjs-faye-transporter/src/interfaces/faye-options.interface.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Serializer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Deserializer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/microservices&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;FayeOptions&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/**
   * faye server mount point (e.g., http://localhost:8000/faye)
   */&lt;/span&gt;
  &lt;span class="nl"&gt;url&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="cm"&gt;/**
   * time in seconds to wait before assuming server is dead and attempting reconnect
   */&lt;/span&gt;
  &lt;span class="nl"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="cm"&gt;/**
   * time in seconds before attempting a resend a message when network error detected
   */&lt;/span&gt;
  &lt;span class="nl"&gt;retry&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="cm"&gt;/**
   * connections to server routed via proxy
   */&lt;/span&gt;
  &lt;span class="nl"&gt;proxy&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="cm"&gt;/**
   * per-transport endpoint objects; e.g., endpoints: { sebsocket: 'http://ws.example.com./'}
   */&lt;/span&gt;
  &lt;span class="nl"&gt;endpoints&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="cm"&gt;/**
   * backoff scheduler: see https://faye.jcoglan.com/browser/dispatch.html
   */&lt;/span&gt;
  &lt;span class="c1"&gt;// tslint:disable-next-line: ban-types&lt;/span&gt;
  &lt;span class="nl"&gt;scheduler&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="cm"&gt;/**
   * instance of a class implementing the serialize method
   */&lt;/span&gt;
  &lt;span class="nl"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;Serializer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="cm"&gt;/**
   * instance of a class implementing the deserialize method
   */&lt;/span&gt;
  &lt;span class="nl"&gt;deserializer&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nx"&gt;Deserializer&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These options (modulo &lt;code&gt;serializer&lt;/code&gt; and &lt;code&gt;deserializer&lt;/code&gt;, which are consumed by the &lt;code&gt;ServerFaye&lt;/code&gt; class directly) are passed through to the Faye client library in &lt;code&gt;createFayeClient()&lt;/code&gt; to customize the &lt;a href="https://faye.jcoglan.com/browser.html" rel="noopener noreferrer"&gt;Faye connection&lt;/a&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;createFayeClient&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;FayeClient&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// pull out url, and strip serializer and deserializer properties&lt;/span&gt;
    &lt;span class="c1"&gt;// from options so we conform to the `faye.Client()` interface&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;deserializer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;faye&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The other benefit of this interface is that it provides intellisense for users constructing the &lt;code&gt;options&lt;/code&gt; object in the call to &lt;code&gt;NestFactory.createMicroservice(...)&lt;/code&gt; in the &lt;code&gt;main.ts&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fcustom-transporter%2Fassets%2Ffaye-options-intellisense.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fcustom-transporter%2Fassets%2Ffaye-options-intellisense.png" title="Editor Intellisense" alt="Editor Intellisense"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;a&gt;&lt;/a&gt;Figure 1: Editor Intellisense



&lt;h3&gt;
  
  
  Error Handling
&lt;/h3&gt;

&lt;p&gt;What should we do if the server loses its connection to the broker (e.g., the Faye broker goes offline)?  This is a good question that probably deserves deeper discussion than what we'll cover here.  The Faye client library appears to be fairly &lt;a href="https://faye.jcoglan.com/browser.html" rel="noopener noreferrer"&gt;robust at re-establishing a connection&lt;/a&gt;, including &lt;a href="https://faye.jcoglan.com/browser/dispatch.html" rel="noopener noreferrer"&gt;buffering failed attempts&lt;/a&gt;, so we are going to simply defer to it, and log an error message to the console whenever a connection goes down.  To handle this, we simply add a listener for a &lt;a href="https://faye.jcoglan.com/browser/transport.html" rel="noopener noreferrer"&gt;disconnect event&lt;/a&gt; and log the message:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;handleError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;stream&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ERROR_EVENT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Faye Server offline!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Checkpoint
&lt;/h3&gt;

&lt;p&gt;We covered a lot of ground in this chapter. Because we provided the &lt;code&gt;nestHttpApp&lt;/code&gt; and a full implementation of the Faye flavored &lt;code&gt;ClientProxy&lt;/code&gt;, you now have a &lt;strong&gt;fully working Faye Custom Transporter&lt;/strong&gt;.  You can feel free to experiment at large with the &lt;code&gt;nestHttpApp&lt;/code&gt; and &lt;code&gt;nestMicroservice&lt;/code&gt;.  Try issuing HTTP requests to the &lt;code&gt;nestHttpApp&lt;/code&gt; like:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;GET /customers&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;and&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;POST /customer  // &amp;lt;-- with a payload&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;See &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/README.md#part-3-completing-the-server-component" rel="noopener noreferrer"&gt;here&lt;/a&gt; for more help on running these and similar tests..&lt;/p&gt;

&lt;p&gt;As mentioned earlier in this article, there's also a series of routes related to exploring how microservices handle promises and observables when they are returned from a &lt;strong&gt;request&lt;/strong&gt;.  You can &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/observable-deepdive.md" rel="noopener noreferrer"&gt;read more here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  What's Next
&lt;/h3&gt;

&lt;p&gt;In &lt;a href="https://dev.to/nestjs/part-4-basic-client-component-16f9"&gt;Part 4&lt;/a&gt;), we turn our attention to the "client" side of the transporter.  We'll build the Faye-flavored &lt;code&gt;ClientProxy&lt;/code&gt; from scratch.  Like the server construction we've done, we'll break this into two parts so we can work through the main concepts first, then handle some edge cases and details in the subsequent part.&lt;/p&gt;

&lt;p&gt;Feel free to ask questions, make comments or suggestions, or just say hello in the comments below. And join us at &lt;a href="https://discord.gg/nestjs" rel="noopener noreferrer"&gt;Discord&lt;/a&gt; for more happy discussions about NestJS. I post there as &lt;em&gt;Y Prospect&lt;/em&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Part 2: Basic Server Component</title>
      <dc:creator>John Biundo</dc:creator>
      <pubDate>Mon, 02 Mar 2020 17:40:05 +0000</pubDate>
      <link>https://forem.com/nestjs/part-2-basic-server-component-onk</link>
      <guid>https://forem.com/nestjs/part-2-basic-server-component-onk</guid>
      <description>&lt;p&gt;&lt;em&gt;John is a member of the NestJS core team&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;This is Part 2 of a six-part series.  If you landed here from Google, you may want to start with &lt;a href="https://dev.to/nestjs/part-1-introduction-and-setup-1a2l"&gt;Part 1&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In this article, we build the first iteration of the server component of our Faye Custom Transporter.  We'll then test it with a simple Nest responder (microservice) app.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reminder&lt;/strong&gt;: Many of the concepts and terminology here are introduced and explained in &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3"&gt;this article series&lt;/a&gt;. That series serves as a good foundation for understanding the more advanced concepts covered in this series.&lt;/p&gt;

&lt;h4&gt;
  
  
  Get the Code
&lt;/h4&gt;

&lt;p&gt;All of the code in these articles is available &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample" rel="noopener noreferrer"&gt;here&lt;/a&gt;.  As always, these tutorials work best if you follow along with the code.  The &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/README.md" rel="noopener noreferrer"&gt;README&lt;/a&gt; covers all the steps you need to get the repository, build the apps, and follow along.  It's easy!  I strongly encourage you to do so.  Note that each article has a corresponding branch in the repository.  For example, this article (Part 2), has a corresponding branch called &lt;code&gt;part2&lt;/code&gt;. Read on, or &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/README.md#part-2-basic-server-component" rel="noopener noreferrer"&gt;get more details here&lt;/a&gt; on using the repository.&lt;/p&gt;

&lt;h4&gt;
  
  
  Git checkout the current version
&lt;/h4&gt;

&lt;p&gt;For this article, you should &lt;code&gt;git checkout&lt;/code&gt; the branch &lt;code&gt;part2&lt;/code&gt;.  You can get more information &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/README.md#repository-structure" rel="noopener noreferrer"&gt;about the git repository branches here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Build the Apps for This Part
&lt;/h4&gt;

&lt;p&gt;For each article in the series, we introduce some new components (and sometimes entirely new &lt;strong&gt;projects&lt;/strong&gt;).  For convenience, at the start of each article, you should run the following command from the top-level directory (where you cloned the repo): *&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="c"&gt;# from root directory of project (e.g., transporter-tutorial, or whatever you chose as the root)&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;sh build.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;This is a shell script that runs on Linux-like systems.  You may have to make an adjustment for non-Linux systems.  Note that the script is simply a convenience that runs &lt;code&gt;npm install &amp;amp;&amp;amp; npm run build&lt;/code&gt;inside each top-level directory, so you can always fall back to that technique if you have trouble with the script.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Organizing the Code
&lt;/h3&gt;

&lt;p&gt;Even though the scope of this tutorial is relatively small — at the end of the day we'll just be creating a few main classes — we're going to be accumulating a number of assets along the way to test and exercise our code. It's time to think a little bit about code organization.  First, let's take a look at what we'll end up with:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Our Faye Custom Transporter is going to consist of some interfaces and constants, some serializers/deserializers (discussed extensively in the &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3"&gt;previous article series&lt;/a&gt;), and the main components: a &lt;strong&gt;client&lt;/strong&gt; component (class) providing the &lt;code&gt;ClientProxy&lt;/code&gt; subclass we can use in any &lt;em&gt;Nest requestor&lt;/em&gt; apps and a &lt;strong&gt;server&lt;/strong&gt; component (class) we can use in any &lt;em&gt;Nest responder&lt;/em&gt; apps.&lt;/li&gt;
&lt;li&gt;The native apps we wrote in the &lt;a href="https://dev.to/nestjs/build-a-custom-transporter-for-nestjs-microservices-dc6-temp-slug-6007254?preview=d3e07087758ff2aac037f47fb67ad5b465f45272f9c5c9385037816b139cf1ed089616c711ed6452184f7fb913aed70028a73e14fef8e3e41ee7c3fc#requestresponse"&gt;previous article&lt;/a&gt; that let us explore the Faye client API and give us some test drivers.&lt;/li&gt;
&lt;li&gt;A pair of regular Nest apps that will host and exercise our transporter code as we build it: a &lt;em&gt;Nest requestor&lt;/em&gt; that is a simple Nest HTTP app called &lt;code&gt;nestHttpApp&lt;/code&gt; and a &lt;em&gt;Nest responder&lt;/em&gt; that is a classic Nest &lt;strong&gt;microservice&lt;/strong&gt;, called &lt;code&gt;nestMicroservice&lt;/code&gt;.  (&lt;strong&gt;Note&lt;/strong&gt;: see &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3"&gt;the previous article series&lt;/a&gt; for more on this terminology, but think of a &lt;em&gt;Nest requestor&lt;/em&gt; as an app that makes remote requests over some non-HTTP transport (like Faye) and a &lt;em&gt;Nest responder&lt;/em&gt; as a Nest application that listens for inbound requests over that transport).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Where to keep all of these assets?&lt;/p&gt;

&lt;p&gt;The main thing to think about now is where to keep the Faye Custom Transporter components.  Since we'll want to easily re-use them across different Nest apps, the logical place is an NPM package.  To that end, when we work on any of those components, they'll go into that package.&lt;/p&gt;

&lt;h4&gt;
  
  
  The Custom Transporter Package
&lt;/h4&gt;

&lt;p&gt;At this point, you should be on the &lt;code&gt;part2&lt;/code&gt; branch of the repo that you cloned (&lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/README.md#part-2-basic-server-component" rel="noopener noreferrer"&gt;read more here&lt;/a&gt; for details).  Once you've checked out that branch, you'll notice a new folder called &lt;code&gt;nestjs-faye-transporter&lt;/code&gt;.  This is organized as an NPM package (you can learn more about creating NestJS-friendly NPM packages in my article &lt;a href="https://dev.to/nestjs/publishing-nestjs-packages-with-npm-21fm"&gt;Publishing NestJS Packages with npm&lt;/a&gt;, including how the &lt;code&gt;tsconfig.json&lt;/code&gt; file, &lt;code&gt;package.json&lt;/code&gt; scripts and properties, and file organization all interact to create a reusable NPM package).&lt;/p&gt;

&lt;p&gt;Here's a run-down on the contents of the &lt;code&gt;nestjs-faye-transporter&lt;/code&gt; package, and how we'll use each:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The scripts (&lt;code&gt;package.json&lt;/code&gt;) let you build (&lt;code&gt;npm run build&lt;/code&gt; or &lt;code&gt;npm run build:watch&lt;/code&gt;) and, if you want, publish (&lt;code&gt;npm publish&lt;/code&gt;) the package.&lt;/li&gt;
&lt;li&gt;Our workflow, in dev, will be to build the package and then use &lt;code&gt;npm link&lt;/code&gt; to install it in our Nest apps.  More on this soon.&lt;/li&gt;
&lt;li&gt;The code is organized in a directory structure as follows:

&lt;ul&gt;
&lt;li&gt;Code common to our &lt;strong&gt;client&lt;/strong&gt; and &lt;strong&gt;server&lt;/strong&gt; is in top level files or folders, like &lt;code&gt;src/external&lt;/code&gt; (holds things like interfaces to the Faye client library which are &lt;em&gt;external&lt;/em&gt; to the Nest environment), &lt;code&gt;src/interfaces&lt;/code&gt; (holds interfaces used within the Nest constructs), and &lt;code&gt;src/constants&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;All code defining the &lt;strong&gt;client&lt;/strong&gt; is in the &lt;code&gt;src/requestor&lt;/code&gt; folder. Ultimately, this will consist of a sub-class of &lt;code&gt;ClientProxy&lt;/code&gt; from the &lt;code&gt;@nestjs/microservices&lt;/code&gt; package — the class which provides methods like &lt;code&gt;client.send(...)&lt;/code&gt; and &lt;code&gt;client.emit(...)&lt;/code&gt;) — and a variety of related classes.&lt;/li&gt;
&lt;li&gt;All code defining the &lt;strong&gt;server&lt;/strong&gt; is in the &lt;code&gt;src/responder&lt;/code&gt; folder.  Ultimately, this will consist of a sub-class of &lt;code&gt;Server&lt;/code&gt; from the &lt;code&gt;@nestjs/microservices&lt;/code&gt; package — the class which implements a transporter's server-side strategy — and a variety of related classes.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  First Iteration (Take 1) of the Server Component
&lt;/h3&gt;

&lt;p&gt;Let's get started building the "server" side of the equation — the part of the custom transporter that you'll use to help build Nest microservices (AKA &lt;em&gt;Nest responders&lt;/em&gt;).&lt;/p&gt;

&lt;h4&gt;
  
  
  Using a Custom Transporter Strategy
&lt;/h4&gt;

&lt;p&gt;Before we start writing transporter code, let's first take a look at how we're going to use a custom transporter.  To see this in action, let's look at the &lt;code&gt;nestMicroservice&lt;/code&gt; app (this app has been added in this branch). The code for it is in the top-level &lt;code&gt;nestMicroservice&lt;/code&gt; folder.&lt;/p&gt;

&lt;h5&gt;
  
  
  Instantiating the Custom Transporter
&lt;/h5&gt;

&lt;p&gt;Open the &lt;code&gt;src/main.ts&lt;/code&gt; file.  Notice the structure of the &lt;code&gt;createMicroservice&lt;/code&gt; call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// nestMicroservices/src/main.ts&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;NestFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createMicroservice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AppModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;strategy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ServerFaye&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http://localhost:8000/faye&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;OutboundResponseIdentitySerializer&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="na"&gt;deserializer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;InboundMessageIdentityDeserializer&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="p"&gt;}),&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is very similar to the structure used for any built-in Nest transporter.  For example, with MQTT it looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;NestFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createMicroservice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AppModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;transport&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Transport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;MQTT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;localhost&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1883&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;OutboundResponseIdentitySerializer&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="na"&gt;deserializer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;InboundMessageIdentityDeserializer&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It should be clear what we're doing: instead of passing the normal &lt;a href="https://docs.nestjs.com/microservices/basics#getting-started" rel="noopener noreferrer"&gt;transporter options object&lt;/a&gt; with &lt;code&gt;transport&lt;/code&gt; and &lt;code&gt;options&lt;/code&gt; properties, we pass a single property, &lt;code&gt;strategy&lt;/code&gt;, whose value is an &lt;strong&gt;instance&lt;/strong&gt; of our custom transporter class.  This class takes a single options object in its constructor, pretty much mirroring the options you pass to the &lt;code&gt;options&lt;/code&gt; property with the built-in transporters.&lt;/p&gt;

&lt;h5&gt;
  
  
  Using the Custom Transporter
&lt;/h5&gt;

&lt;p&gt;Let's briefly review how our &lt;code&gt;nestMicroservice&lt;/code&gt; app works.  Its only real job is to take in a &lt;code&gt;'/get-customers'&lt;/code&gt; message and return a list of customers.  It performs the exact same function as the &lt;code&gt;customerApp&lt;/code&gt; we built in Part 1. The action is in the &lt;code&gt;nestMicroservices/src/app.controller.ts&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// nestMicroservices/src/app.controller.ts&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;logger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;AppController&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="cm"&gt;/**
   * Register a message handler for '/get-customers' requests
   */&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;MessagePattern&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/get-customers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;getCustomers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;customers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
      &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;customerId&lt;/span&gt;
        &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;customerList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cust&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;cust&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nf"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;customerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
        &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;customerList&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;customers&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All it really does is check for the existence of a &lt;code&gt;customerId&lt;/code&gt; in the inbound message, and if it exists, uses it to filter the customer list it returns.&lt;/p&gt;

&lt;h4&gt;
  
  
  Take 1 Requirements
&lt;/h4&gt;

&lt;p&gt;Alright, we're finally ready to look at our &lt;code&gt;ServerFaye&lt;/code&gt; class — the one we'll instantiate and pass as the value of that &lt;code&gt;strategy&lt;/code&gt; property above.  We'll take this in a couple of steps.&lt;/p&gt;

&lt;p&gt;This iteration (&lt;em&gt;Take 1&lt;/em&gt;) is going to be the absolute bare-bones class needed to implement the server component of the Faye transporter.  We're going to keep our first version basic so that we can focus on the core flow.  Our first cut will:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;minimize error handling&lt;/li&gt;
&lt;li&gt;not yet utilize some of the nice features of the framework that make our code really robust&lt;/li&gt;
&lt;li&gt;not handle events (e.g., inbound messages coming from &lt;code&gt;client.emit(...)&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;not really be type safe (we omit a bunch of typing to declutter the code)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To state it in terms of requirements: the goal is to respond to a well-formed inbound &lt;strong&gt;request&lt;/strong&gt; (i.e., a request from a &lt;strong&gt;request-response&lt;/strong&gt; style message).  We'll test this requirement by replacing our native &lt;code&gt;customerService&lt;/code&gt; responder app from the last article with our &lt;code&gt;nestMicroservices&lt;/code&gt; app running our new Faye Custom Transporter, and sending it the same &lt;code&gt;'/get-customers'&lt;/code&gt; request from our native &lt;code&gt;customerApp&lt;/code&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In Part 3, we'll complete the implementation and have a fully functioning Faye Custom Transporter (server component).  At that point, you'll also have all of the concepts in place to write your own custom transporter server component, as well as the ability to look inside the built-in transporters (like &lt;a href="https://github.com/nestjs/nest/blob/master/packages/microservices/server/server-mqtt.ts" rel="noopener noreferrer"&gt;the MQTT transporter server&lt;/a&gt;) and understand what's going on.  That will prepare you for even more adventures, like customizing the Nest built-in transporters to add features— the subject of my next NestJS microservice tutorial (already underway, and coming very soon 💥)!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  Take 1 Code Review
&lt;/h4&gt;

&lt;p&gt;Let's dive into the code.  Open the file &lt;code&gt;nestjs-faye-transporter/src/responder/transporters/server-faye.ts&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;An important concept is that this class extends &lt;code&gt;Server&lt;/code&gt; (view this parent class here: &lt;a href="https://github.com/nestjs/nest/blob/master/packages/microservices/server/server.ts" rel="noopener noreferrer"&gt;@nestjs/microservices/server/server.ts&lt;/a&gt;). We won't walk through all the details of the &lt;code&gt;Server&lt;/code&gt; superclass, but the main things to know are:&lt;/p&gt;

&lt;p&gt;1) Our &lt;code&gt;ServerFaye&lt;/code&gt; class inherits some properties and methods from the built-in &lt;code&gt;Server&lt;/code&gt; class&lt;br&gt;
2) After being instantiated in the &lt;code&gt;main.ts&lt;/code&gt; file, the &lt;code&gt;ServerFaye&lt;/code&gt; instance is essentially &lt;em&gt;managed for us&lt;/em&gt; as part of the Nest lifecycle. This means that when the application bootstraps, and our custom &lt;code&gt;ServerFaye&lt;/code&gt; class is instantiated, its inherited properties are populated with data, and the framework calls the entry point of our class.  That entry point is the &lt;code&gt;listen()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;So let's dig into the &lt;code&gt;ServerFaye&lt;/code&gt; class, starting with the &lt;code&gt;listen()&lt;/code&gt; entry point. Its job is to make a connection to the broker, and then run &lt;code&gt;start()&lt;/code&gt;, which is where the fun begins.&lt;/p&gt;

&lt;p&gt;The interesting thing in &lt;code&gt;start()&lt;/code&gt; is the call to &lt;code&gt;this.bindHandlers()&lt;/code&gt;.  Take a look at that method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// nestjs-faye-transporter/src/responder/transporters/server-faye.ts&lt;/span&gt;
  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;bindHandlers&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/**
     * messageHandlers is populated by the Framework (on the `Server` superclass)
     *
     * It's a map of `pattern` -&amp;gt; `handler` key/value pairs
     * `handler` is the handler function in the user's controller class, decorated
     * by `@MessageHandler()` or `@EventHandler`, along with an additional boolean
     * property indicating its Nest pattern type: event or message (i.e.,
     * request/response)
     */&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;messageHandlers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="c1"&gt;// only handling `@MessagePattern()`s for now&lt;/span&gt;
      &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;isEventHandler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;_ack`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getMessageHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;getMessageHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Function&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;inboundPacket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;deserializer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;deserialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;handler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;inboundPacket&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;outboundRawPacket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;err&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;isDisposed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;outboundPacket&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;outboundRawPacket&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;fayeClient&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;_res`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;outboundPacket&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The comments should help with understanding what's happening, but at a high level, the strategy should be pretty clear:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Iterate over all of our "message handlers"*. These are the user-land Controller methods decorated with &lt;code&gt;@MessagePattern()&lt;/code&gt;; a list of them is made available to us  (in &lt;code&gt;this.messageHandlers&lt;/code&gt;) by the framework, which discovers them using introspection with the &lt;code&gt;Reflect&lt;/code&gt; API during the bootstrap process.&lt;/li&gt;
&lt;li&gt;For each one, subscribe to the inbound channel (the &lt;code&gt;_ack&lt;/code&gt; form of the topic).  Remember, a Faye client's &lt;code&gt;subscribe()&lt;/code&gt; call registers a &lt;em&gt;Faye subscription handler&lt;/em&gt; to be invoked whenever the Faye client library receives an inbound message matching this topic.  So this is the step where we map &lt;em&gt;patterns&lt;/em&gt; to &lt;em&gt;handlers&lt;/em&gt;. In short, this is our "router".&lt;/li&gt;
&lt;li&gt;The &lt;em&gt;Faye subscription handler&lt;/em&gt; in step 2, when invoked, runs the &lt;em&gt;actual&lt;/em&gt; user supplied handler (the user-land handler method decorated with something like &lt;code&gt;@MessagePattern('/get-customer')&lt;/code&gt;), and &lt;em&gt;returns&lt;/em&gt; the result it gets from that method.  When we say "returns", we mean &lt;strong&gt;publishes a reply&lt;/strong&gt; on the outbound channel (the &lt;code&gt;_res&lt;/code&gt; form of the topic).&lt;/li&gt;
&lt;li&gt;Along the way, we run our deserializer on the inbound message, our serializer on the outbound response, and we package up the data produced by the user's pattern handler in an appropriately shaped standard Nest transporter message object.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;*For &lt;em&gt;Take 1&lt;/em&gt;, we are omitting &lt;em&gt;event handlers&lt;/em&gt; (methods decorated with &lt;code&gt;@EventPattern(...)&lt;/code&gt;).  We'll handle these in &lt;em&gt;Take 2&lt;/em&gt; of our server component, in the next article.&lt;/p&gt;

&lt;p&gt;The only slightly tricky part is the call to &lt;code&gt;getMessageHandler()&lt;/code&gt;.  This is just a higher-order function that is returning us the &lt;strong&gt;actual&lt;/strong&gt; &lt;em&gt;Faye subscription handler&lt;/em&gt; function.&lt;/p&gt;

&lt;p&gt;If all this looks somewhat familiar, it's because we're basically following the same &lt;strong&gt;STRPTQ&lt;/strong&gt; (&lt;em&gt;subscribe-to-the-response-then-publish-the-request&lt;/em&gt;) pattern we used in the native &lt;code&gt;customerService&lt;/code&gt; app in the previous article (&lt;a href="https://dev.to/nestjs/part-1-introduction-and-setup-1a2l"&gt;Part 1&lt;/a&gt;).&lt;/p&gt;

&lt;h3&gt;
  
  
  Acceptance Testing
&lt;/h3&gt;

&lt;p&gt;We should be ready to test out our code.  We need to do a tiny bit of setup first.  This is going to run best if you have four separate terminals open so you can watch things unfold across the whole process as the various pieces communicate.&lt;/p&gt;

&lt;p&gt;Now's a good time to mention a couple of things about the development setup:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;I run this kind of stuff on an Ubuntu machine.  There's &lt;strong&gt;nothing&lt;/strong&gt; platform-specific anywhere in the Nest-related code, &lt;strong&gt;but&lt;/strong&gt;, I find running builds, starting and stopping processes, running multiple terminal sessions, etc., to be much smoother on Linux.  You can run this wherever you want, but if you're not on Linux, you may have to make slight adjustments to your &lt;code&gt;package.json&lt;/code&gt;, or other actual build steps.&lt;/li&gt;
&lt;li&gt;Since you kind of &lt;strong&gt;need&lt;/strong&gt; to run multiple terminal sessions to see the full impact, I strongly recommend &lt;a href="https://github.com/tmux/tmux" rel="noopener noreferrer"&gt;Tmux&lt;/a&gt;.  I mentioned this briefly at the end of the last article, so you now know I feel strongly about it 😃. You can instead start multiple terminal programs, or use a tabbed console program if you prefer, but if you want what I think is the &lt;strong&gt;best&lt;/strong&gt; DX for this kind of work, checkout Tmux.  I covered some detailed Tmux recommendations &lt;a href="https://github.com/johnbiundo/nest-nats-sample#pro-tip-use-tmux-optional" rel="noopener noreferrer"&gt;in the last article series&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the following steps, I'll reference these (logical) terminals as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Terminal 1&lt;/strong&gt;: run the Faye broker here&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Terminal 2&lt;/strong&gt;: run live builds (&lt;code&gt;npm build:watch&lt;/code&gt;) of the transporter server code we're working on here&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Terminal 3&lt;/strong&gt;: run the "requestor code" here.  This is usually the customerApp; in the future, we'll also interact with &lt;code&gt;nestHttpApp&lt;/code&gt; (making &lt;strong&gt;it&lt;/strong&gt; the requestor) using HTTPie commands from the OS prompt (you can also use something like Postman or curl to issue HTTP requests, of course). We can use one terminal for this since we don't typically run both the &lt;code&gt;customerApp&lt;/code&gt; and the &lt;code&gt;nestHttpApp&lt;/code&gt; at the same time&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Terminal 4&lt;/strong&gt;: run the &lt;code&gt;nestMicroservice&lt;/code&gt; &lt;em&gt;Nest responder&lt;/em&gt; application here (this is the plain old Nest microservice app that will be &lt;strong&gt;using&lt;/strong&gt; our new Faye Custom Transporter)&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Primary Acceptance Test
&lt;/h4&gt;

&lt;p&gt;Going back to our requirements, our main acceptance test is simple: send a well-formed message from our native &lt;code&gt;customerApp&lt;/code&gt; (acting as a requestor) to our &lt;code&gt;nestMicroservice&lt;/code&gt; (acting as a responder), and get back a proper response.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fcustom-transporter%2Fassets%2Facceptance-test.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fcustom-transporter%2Fassets%2Facceptance-test.png" title="Faye Transporter Acceptance Test" alt="Faye Transporter Acceptance Test"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;a&gt;&lt;/a&gt;Figure 1: Faye Transporter Acceptance Test



&lt;h5&gt;
  
  
  Using &lt;code&gt;npm link&lt;/code&gt; in Our Development Environment
&lt;/h5&gt;

&lt;p&gt;Before we can run the test, we need to cover one more preliminary.  Ask yourself this: how does our &lt;code&gt;nestMicroservice&lt;/code&gt; app know how to find the code in our &lt;code&gt;nestjs-faye-transporter&lt;/code&gt; package?  The answer is pretty simple (though it belies the awesomeness of NPM!).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: If you ran the &lt;code&gt;build.sh&lt;/code&gt; script at the beginning of this article, you &lt;strong&gt;probably do not&lt;/strong&gt; need to run the &lt;code&gt;npm link&lt;/code&gt; commands below. Doing so again is harmless, and it's useful to understand what's happening so I include the steps below.  Also, &lt;strong&gt;sometimes&lt;/strong&gt; things just get flakey with the links, and you need to re-run them.  I'd actually suggest you go ahead and do so even if you &lt;em&gt;did&lt;/em&gt; already run the &lt;code&gt;build.sh&lt;/code&gt; script, just to see the behavior.  These steps are prety quick anyway.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We're going to use the &lt;code&gt;npm link&lt;/code&gt; command (&lt;a href="https://medium.com/dailyjs/how-to-use-npm-link-7375b6219557" rel="noopener noreferrer"&gt;read details here&lt;/a&gt;, but the following is all you need to know for this tutorial).  There are two simple steps:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;In terminal 2, make sure you're in the folder &lt;code&gt;nestjs-faye-transporter&lt;/code&gt; (our NPM package folder).  Then run &lt;code&gt;npm link&lt;/code&gt; at the OS level.  You should see output like this (I use &lt;code&gt;nvm&lt;/code&gt;; if you don't your output will look &lt;em&gt;slightly&lt;/em&gt; different):&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="c"&gt;# from directory nestjs-faye-transporter&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;link&lt;/span&gt;

@faye-tut/nestjs-faye-transporter@1.0.1 prepare /home/john/code/nest-micro/nestjs-faye/nestjs-faye-transporter
npm run build

@faye-tut/nestjs-faye-transporter@1.0.1 build /home/john/code/nest-micro/nestjs-faye/nestjs-faye-transporter
tsc

 &lt;span class="o"&gt;(&lt;/span&gt;... some lines omitted ...&lt;span class="o"&gt;)&lt;/span&gt;

/home/john/.nvm/versions/node/v10.18.1/lib/node_modules/@faye-tut/nestjs-faye-transporter -&amp;gt;
/home/john/code/nest-micro/nestjs-faye/nestjs-faye-transporter
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;In terminal 4, make sure you're in the folder &lt;code&gt;nestMicroservice&lt;/code&gt;.  Then run &lt;code&gt;npm link @faye-tut/nestjs-faye-transporter&lt;/code&gt; at the OS level.  This command references the NPM package name (found in &lt;code&gt;nestjs-faye-transporter/package.json&lt;/code&gt;) for our custom transporter.  You should see output like this:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="c"&gt;# from directory nestMicroservice&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;npm &lt;span class="nb"&gt;link&lt;/span&gt; @faye-tut/nestjs-faye-transporter
/home/john/code/nest-micro/nestjs-faye/nestMicroservice/node_modules/@faye-tut/nestjs-faye-transporter -&amp;gt;
/home/john/code/nest-micro/nestjs-faye/nestjs-faye-transporter
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;At this point, our &lt;code&gt;nestMicroservice&lt;/code&gt; testing app is live-linked to the custom transporter code in the &lt;code&gt;@faye-tut/nestjs-faye-transporter&lt;/code&gt; NPM package we're working on.  Any changes we make to that code will trigger a rebuild, and because we linked, those changes are &lt;em&gt;immediately&lt;/em&gt; visible.  Very cool stuff there.&lt;/p&gt;

&lt;h5&gt;
  
  
  Running the Test
&lt;/h5&gt;

&lt;p&gt;We're ready to rock and roll 🎵!&lt;/p&gt;

&lt;p&gt;In terminal 1, make sure the Faye broker is running. First make sure you're in the &lt;code&gt;faye-server&lt;/code&gt; directory, then run &lt;code&gt;npm run start&lt;/code&gt;.  You should see something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="c"&gt;# from directory faye-server&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;npm run start
simple-faye-server@1.0.0 start /home/john/code/nest-micro/nestjs-faye/faye-server
node server.js

listening on http://localhost:8000/faye
&lt;span class="o"&gt;========================================&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In terminal 4, start the &lt;code&gt;nestMicroservice&lt;/code&gt;.  First make sure you're in the &lt;code&gt;nestMicroservice&lt;/code&gt; directory, then run &lt;code&gt;npm run start:dev&lt;/code&gt;.  You should see the usual Nest startup logs, followed by the message &lt;code&gt;Microservice is listening...&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In terminal 3, we'll run the native &lt;code&gt;customerApp&lt;/code&gt; to make the request.  Earlier, we used this app to send requests to the native &lt;code&gt;customerService&lt;/code&gt; app.  Now we're sending those exact same messages (via the Faye broker, of course) to the &lt;code&gt;nestMicroservice&lt;/code&gt;.  Make sure you're in the &lt;code&gt;customerApp&lt;/code&gt; directory, then...&lt;/p&gt;

&lt;p&gt;... keep your eyes on all three terminal windows so you don't miss the magic, and...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# in terminal 3 window...&lt;/span&gt;
&lt;span class="c"&gt;# in folder customerApp&lt;/span&gt;
npm run get-customers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If all went well, you should see a flurry of log messages.  I strongly encourage you to take the time to look at them and make sure you can follow the flow of what's happening.  In terminal 3, we should get a nice response that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;npm run get-customers

&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; faye-customer-app@1.0.0 get-customers /home/john/code/nest-micro/nestjs-faye/customerApp
&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; node dist/customer-app get

Faye customer app starts...
&lt;span class="o"&gt;===========================&lt;/span&gt;
&amp;lt;&lt;span class="o"&gt;==&lt;/span&gt; Sending &lt;span class="s1"&gt;'get-customers'&lt;/span&gt; request with payload:
&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"pattern"&lt;/span&gt;:&lt;span class="s2"&gt;"/get-customers"&lt;/span&gt;,&lt;span class="s2"&gt;"data"&lt;/span&gt;:&lt;span class="o"&gt;{}&lt;/span&gt;,&lt;span class="s2"&gt;"id"&lt;/span&gt;:&lt;span class="s2"&gt;"415fdcef-88e8-42e7-a464-ebdb3b10ff57"&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="o"&gt;==&amp;gt;&lt;/span&gt; Receiving &lt;span class="s1"&gt;'get-customers'&lt;/span&gt; reply:
&lt;span class="o"&gt;{&lt;/span&gt;
  &lt;span class="s2"&gt;"customers"&lt;/span&gt;: &lt;span class="o"&gt;[&lt;/span&gt;
    &lt;span class="o"&gt;{&lt;/span&gt;
      &lt;span class="s2"&gt;"id"&lt;/span&gt;: 1,
      &lt;span class="s2"&gt;"name"&lt;/span&gt;: &lt;span class="s2"&gt;"nestjs.com"&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
  &lt;span class="o"&gt;]&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Terminals 1 and 4 show the corresponding message flow in the Faye broker and &lt;code&gt;nestMicroservice&lt;/code&gt; app respectively.  They all should weave together nicely to let you trace the entire saga through the layers of the system.  By the way, if you're wondering where the message level logging in terminal 4 (&lt;code&gt;nestMicroservice&lt;/code&gt;) comes from, take a look at the serializer/deserializer implementations in &lt;code&gt;nestjs-faye-transporter/src/responder/serializers/outbound-response-identity-serializer.ts&lt;/code&gt; and &lt;code&gt;nestjs-faye-transporter/src/responder/deserializers/inbound-message-identity-deserializer.ts&lt;/code&gt; respectively.  These are our so-called "identity" serializer/deserializer from the &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-3-4m20"&gt;previous article series&lt;/a&gt;, which are very helpful for visualizing message flow.&lt;/p&gt;

&lt;p&gt;Hooray! We're done right?  🍺 🍺 🍺?&lt;/p&gt;

&lt;p&gt;Not so fast... did you forget this is a six-part series 😃? Read on to see where we've still got work to do.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding the Limitations of Take 1
&lt;/h3&gt;

&lt;p&gt;We already know we have to clean up a few things, like adding &lt;strong&gt;event handling&lt;/strong&gt; (e.g., handlers decorated with &lt;code&gt;@EventPattern(...)&lt;/code&gt;), adding TypeScript types, and plugging in more cleanly to the framework.  But the biggest limitation of our Take 1 implementation is its (lack of) handling of &lt;a href="http://reactivex.io/" rel="noopener noreferrer"&gt;RxJS Observables&lt;/a&gt;.  To fully appreciate this, we'll have to take a bit deeper dive into the overall flow and handling of requests through the Nest system.&lt;/p&gt;

&lt;p&gt;We'll explore this in greater detail in the next article, but let's start with a picture.  The following animation shows the path a hypothetical inbound HTTP request would take through our application. Inner boxes with a red background are part of the Nest infrastructure. User supplied code lives in the user-land space with a white background.  Controllers are in light blue, and "Services" are in yellow.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fcustom-transporter%2Fassets%2Ftransporter-request2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fcustom-transporter%2Fassets%2Ftransporter-request2.gif" title="Nest Request Handling" alt="Nest Request Handling"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;a&gt;&lt;/a&gt;Figure 2: Nest Request Handling



&lt;p&gt;An inbound HTTP request kicks off the following sequence of events.  Bolded words represent Nest system responsibilities.  Underlined words represent user code. There's probably nothing terribly surprising going on here, but let's just briefly walk through it.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The request is &lt;strong&gt;routed&lt;/strong&gt; to a &lt;u&gt;route handling method&lt;/u&gt; (like &lt;code&gt;getCustomers&lt;/code&gt; in our controller).&lt;/li&gt;
&lt;li&gt;The &lt;u&gt;route handling method&lt;/u&gt; can either make a remote request directly, or &lt;u&gt;call a service that makes a remote request&lt;/u&gt;.&lt;/li&gt;
&lt;li&gt;The remote request is handled by &lt;strong&gt;the broker client library&lt;/strong&gt; and sent to the broker.&lt;/li&gt;
&lt;li&gt;The remote request is received by &lt;strong&gt;the broker client library&lt;/strong&gt; in the &lt;code&gt;nestMicroservice&lt;/code&gt; app.&lt;/li&gt;
&lt;li&gt;The request is &lt;strong&gt;routed&lt;/strong&gt; to the correct handler (e.g., a method decorated with &lt;code&gt;@MessagePattern('/get-customers')&lt;/code&gt;) based on matching the pattern in the request with the pattern in the method decorator.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The &lt;u&gt;request handling method&lt;/u&gt; may make a call to other services (which in turn, could make their own internal calls or remote calls).  Let's say it does make such a call, to the &lt;code&gt;custService.getCustomers()&lt;/code&gt; method, and that method has a signature like this:&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nf"&gt;getCustomers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;integer&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Customer&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Once the &lt;code&gt;getCustomers&lt;/code&gt; method returns, we start the &lt;em&gt;return trip&lt;/em&gt;, where things get more interesting. This "more interesting" part is mainly because Nest is very &lt;strong&gt;Observable-aware&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fcustom-transporter%2Fassets%2Ftransporter-response3.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fcustom-transporter%2Fassets%2Ftransporter-response3.gif" title="Nest Response Handling" alt="Nest Response Handling"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;a&gt;&lt;/a&gt;Figure 3: Nest Response Handling



&lt;p&gt;In this sequence, I'll introduce a part of the transporter infrastructure responsible for what I'm informally calling "Marshalling" (there's no such official term or single component inside Nest that does Marshalling).  Conceptually, it's the part(s) of the system that handle(s) dealing with transferring Observable streams over the network.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In &lt;a href="https://dev.to/nestjs/part-3-completing-the-server-component-4a80"&gt;Part 3&lt;/a&gt; we'll go through a few use cases for &lt;strong&gt;why Observables are so cool, why they're a perfect fit in this flow, AND how they're actually really easy to use&lt;/strong&gt;.  I know this diagram doesn't make it seem that way, but hey, we're building our own transporter (my inner &lt;a href="http://sfi.org/" rel="noopener noreferrer"&gt;trekky&lt;/a&gt; can't help but giggle over that 🚀).  The beauty of it is that once we handle this case properly — and the framework will make this easy as we'll see in the next chapter — everything we might want to do with Observables (and their potential is just, well... &lt;em&gt;mind bending&lt;/em&gt;) &lt;strong&gt;just works&lt;/strong&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here's the walk through of the return trip flow:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;u&gt;Our handler&lt;/u&gt; in &lt;code&gt;nestMicroservice&lt;/code&gt; (perhaps directly, or perhaps by calling a service) returns the result. In terms of our example, it's returning a list of customers.&lt;/li&gt;
&lt;li&gt;Now, if the response is a "plain" value (JavaScript primitive, array or object), there's not much work to be done other than send it back to the broker. But, if the response is an Observable stream, &lt;strong&gt;Nest steps in and makes this extremely easy&lt;/strong&gt; for everything downstream to work with.  That's where &lt;strong&gt;marshalling&lt;/strong&gt; comes in.&lt;/li&gt;
&lt;li&gt;Once the response is prepared from &lt;code&gt;nestMicroservice&lt;/code&gt;, it's &lt;strong&gt;delivered to the Faye broker&lt;/strong&gt; via the broker client library.&lt;/li&gt;
&lt;li&gt;The broker takes care of &lt;strong&gt;publishing the response&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;On the &lt;code&gt;nestHttpApp&lt;/code&gt; side, the broker client library (which has previously subscribed to the broker on the &lt;em&gt;response channel&lt;/em&gt; — though we haven't coded that part yet), &lt;strong&gt;receives the response message&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;The &lt;code&gt;ClientProxy&lt;/code&gt; class (again, we haven't built this yet) takes care of &lt;strong&gt;routing&lt;/strong&gt; (and &lt;strong&gt;marshalling&lt;/strong&gt;, if it's dealing with a stream of responses).&lt;/li&gt;
&lt;li&gt;This routes the response to the &lt;u&gt;originating service&lt;/u&gt;, then back to the &lt;u&gt;originating controller&lt;/u&gt;.&lt;/li&gt;
&lt;li&gt;Finally, the Nest HttpAdapter software again, if needed, &lt;strong&gt;marshalls responses&lt;/strong&gt; to a suitable form for HTTP transport.  For example, if the response is an Observable or a promise, it &lt;strong&gt;converts it to an appropriate form&lt;/strong&gt; for return over HTTP.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So what exactly is the issue?&lt;/p&gt;

&lt;p&gt;As you might expect from the above, things work fine if our handlers return plain objects.  For example, we currently return an object in our &lt;code&gt;nestMicroservices&lt;/code&gt; &lt;code&gt;getCustomers()&lt;/code&gt; handler (last line below):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// nestMicroservice/src/app.controller.ts&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;MessagePattern&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/get-customers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;getCustomers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;customers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;customerId&lt;/span&gt;
      &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;customerList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;cust&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;cust&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nf"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;customerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
      &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;customerList&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;customers&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But what happens if our handler returns an Observable? The framework enables this for all built-in transporters, so we should handle it too.  Let's test this really quickly.  Replace that last line in &lt;code&gt;app.controller.ts&lt;/code&gt; with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;customers&lt;/span&gt;&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You'll also have to add the following line to the top of the file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;of&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;rxjs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This construct uses the RxJS &lt;code&gt;of&lt;/code&gt; operator to convert our &lt;code&gt;getCustomers()&lt;/code&gt; method handler response to an &lt;strong&gt;Observable&lt;/strong&gt; — a stream (containing only a single value in our case, but still, a stream) of values.&lt;/p&gt;

&lt;p&gt;If you make this change, then re-issue the &lt;code&gt;/get-customers&lt;/code&gt; message (run &lt;code&gt;npm run get-customers&lt;/code&gt; in terminal 3), you'll get a rather ugly failure in the &lt;code&gt;nestMicroservice&lt;/code&gt; window.  This is our fault! If you think about it, we aren't doing any marshalling anywhere in our code. So we aren't handling this case, which, again, is expected of any Nest microservice transporter.&lt;/p&gt;

&lt;h3&gt;
  
  
  What's Next
&lt;/h3&gt;

&lt;p&gt;With these issues in mind, we're ready to step up our game and make the Faye Custom Transporter server component much more robust.  We'll tackle that in the next article.  In &lt;a href="https://dev.to/nestjs/part-3-completing-the-server-component-4a80"&gt;Part 3&lt;/a&gt; we cover:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A little side expedition on how and why you should care about the "Observables issue" we just uncovered&lt;/li&gt;
&lt;li&gt;Addressing that issue&lt;/li&gt;
&lt;li&gt;Handling events (e.g., &lt;code&gt;@EventPattern(...)&lt;/code&gt; decorated methods) in our responder app&lt;/li&gt;
&lt;li&gt;Adding TypeScript types&lt;/li&gt;
&lt;li&gt;A few other minor items to clean up our app and make it production worthy&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Feel free to ask questions, make comments or suggestions, or just say hello in the comments below. And join us at &lt;a href="https://discord.gg/nestjs" rel="noopener noreferrer"&gt;Discord&lt;/a&gt; for more happy discussions about NestJS. I post there as &lt;em&gt;Y Prospect&lt;/em&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Part 1: Introduction and Setup</title>
      <dc:creator>John Biundo</dc:creator>
      <pubDate>Mon, 02 Mar 2020 17:18:40 +0000</pubDate>
      <link>https://forem.com/nestjs/part-1-introduction-and-setup-1a2l</link>
      <guid>https://forem.com/nestjs/part-1-introduction-and-setup-1a2l</guid>
      <description>&lt;p&gt;&lt;em&gt;John is a member of the NestJS core team&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;This article series covers the topic of building a &lt;strong&gt;custom transporter&lt;/strong&gt; for the &lt;a href="https://docs.nestjs.com/microservices/basics" rel="noopener noreferrer"&gt;NestJS microservices subsystem&lt;/a&gt;. Is this article right for you? It is if you fit into one of the following two audiences:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You're trying to build or modify a NestJS microservices transporter.  That one was probably pretty obvious 😄.&lt;/li&gt;
&lt;li&gt;You're a NestJS application developer (someone who writes code in what I'll often refer to as "user-land"), and want a deeper understanding of how NestJS microservices work.  Candidly, this is where I came from, and a main motivator for doing the research to write this article series.  Even if you never intend to build your own transporter, you can still benefit from this deeper understanding, as I have done.  Along the way, I'll try to share insights on how you can become a better user of Nest microservices.&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Recommended Reading
&lt;/h4&gt;

&lt;p&gt;If you haven't already read it, please check out my &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3"&gt;NestJS Microservices in Action&lt;/a&gt; series, where I cover many of the basics of the NestJS microservices subsystem architecture, and establish the terminology used in all my Nest Microservices articles, including answering the question &lt;em&gt;"what the heck is a transporter?"&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fcustom-transporter%2Fassets%2Ftrek-transporter.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fcustom-transporter%2Fassets%2Ftrek-transporter.gif" title="Star Trek Transporter" alt="Star Trek Transporter"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;a&gt;&lt;/a&gt;Close, but no cigar!



&lt;p&gt;Anyway, while not strictly required, this series assumes a good understanding of the material in that article series.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why Custom Transporters?
&lt;/h3&gt;

&lt;p&gt;Let's start with the &lt;em&gt;Why?&lt;/em&gt; question.  The &lt;a href="https://docs.nestjs.com/microservices/basics" rel="noopener noreferrer"&gt;Nest microservices subsystem&lt;/a&gt; provides a communications layer abstraction that makes it easy for applications (both Nest and non-Nest) to communicate over a wide variety of what are called &lt;strong&gt;transporters&lt;/strong&gt;.  This provides several key benefits, covered fully in the &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3"&gt;previous article series&lt;/a&gt;. Nest comes with a variety of built-in transporters, including NATS, RabbitMQ, Kafka, and others. But what if you want to build your own transporter — say for ZeroMQ, Google Cloud Pub/Sub, Amazon Kinesis or Apache ActiveMQ? If that's your desire, you've landed on the right article!  Or, if you just want to understand more about how this key part of the Nest infrastructure works, read on!&lt;/p&gt;

&lt;p&gt;A related question is &lt;em&gt;"How can I extend the capabilities of existing Nest transporters?"&lt;/em&gt;  For example, maybe you'd like to use MQTT's &lt;a href="https://github.com/mqttjs/MQTT.js/blob/master/README.md#qos" rel="noopener noreferrer"&gt;QoS feature&lt;/a&gt; with the &lt;a href="https://docs.nestjs.com/microservices/mqtt" rel="noopener noreferrer"&gt;MQTT transporter&lt;/a&gt;.  That's a topic I'll cover in another upcoming series (already under development 💥), but this series provides a baseline for addressing that requirement.&lt;/p&gt;

&lt;h3&gt;
  
  
  Article Series Overview
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Part 1 (this article): Sets the background for the entire series.  We'll begin by examining the simple &lt;a href="https://faye.jcoglan.com/" rel="noopener noreferrer"&gt;Faye message broker&lt;/a&gt; to set the stage for building a custom Nest transporter that sits on top of it.  By the time we're done with Part 5, we'll have a fully functional Faye Transporter!&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dev.to/nestjs/part-2-basic-server-component-onk"&gt;Part 2&lt;/a&gt;: Tackles building an initial version of the &lt;strong&gt;server side&lt;/strong&gt; of our custom transporter. The result is a working version that should familiarize you with the main concepts needed to understand how to build the server component of &lt;strong&gt;any&lt;/strong&gt; custom transporter. It defers implementation of a few important details to the next article so as to keep us focused on the main path.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dev.to/nestjs/part-3-completing-the-server-component-4a80"&gt;Part 3&lt;/a&gt;: Cleans up some loose ends, and dives deep on a few of the more advanced and impressive features of the framework, and shows you how to enable these features in your custom transporter.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dev.to/nestjs/part-4-basic-client-component-16f9"&gt;Part 4&lt;/a&gt;: Switches to the client side. As with the server side, this article walks through a &lt;strong&gt;basic functional implementation for Faye&lt;/strong&gt;, deferring a few of the more complex features to the next article.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dev.to/nestjs/part-4-basic-client-component-16f9"&gt;Part 5&lt;/a&gt;: Completes the client-side journey, resulting in a finished, fully functional Custom Transporter for Faye.&lt;/li&gt;
&lt;li&gt;Part 6: (Coming soon!) Surveys a few of the built-in NestJS transporters and compares their implementation to the Faye implementation to shed light on some broker-specific nuanced implementation details.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's get started!&lt;/p&gt;

&lt;h4&gt;
  
  
  Get the Code
&lt;/h4&gt;

&lt;p&gt;All of the code in these articles is available &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample" rel="noopener noreferrer"&gt;here&lt;/a&gt;.  As always, these tutorials work best if you follow along with the code.  The &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/README.md" rel="noopener noreferrer"&gt;README&lt;/a&gt; covers all the steps you need to get the repository, build the apps, and follow along.  It's easy!  I strongly encourage you to do so.  Note that each article has a corresponding branch in the repository.  For example, this article (Part 1), has a corresponding branch called &lt;code&gt;part1&lt;/code&gt;. Read on, or &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/README.md#repository-structure" rel="noopener noreferrer"&gt;get more details here&lt;/a&gt; on using the repository.&lt;/p&gt;

&lt;h4&gt;
  
  
  Git checkout the current version
&lt;/h4&gt;

&lt;p&gt;For this article, you should &lt;code&gt;git checkout&lt;/code&gt; the branch &lt;code&gt;part1&lt;/code&gt;.  You can get more information &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/README.md#repository-structure" rel="noopener noreferrer"&gt;about the git repository branches here&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Build the Apps for This Part
&lt;/h4&gt;

&lt;p&gt;For each article in the series, we introduce some new components (and sometimes entirely new &lt;strong&gt;projects&lt;/strong&gt;).  For convenience, at the start of each article, you should run the following command from the top-level directory (where you cloned the repo):*&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="c"&gt;# from root directory of project (e.g., transporter-tutorial, or whatever you chose as the root)&lt;/span&gt;
&lt;span class="nv"&gt;$ &lt;/span&gt;sh build.sh
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*&lt;em&gt;This is a shell script that runs on Linux-like systems.  You may have to make an adjustment for non-Linux systems.  Note that the script is simply a convenience that runs &lt;code&gt;npm install &amp;amp;&amp;amp; npm run build&lt;/code&gt;inside each top-level directory, so you can always fall back to that technique if you have trouble with the script.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Overview for Part 1
&lt;/h3&gt;

&lt;p&gt;For this case study, we'll build a custom transporter to work with the &lt;a href="https://faye.jcoglan.com/" rel="noopener noreferrer"&gt;Faye&lt;/a&gt; message broker.  Faye is a simple OSS JavaScript publish/subscribe message broker that runs nicely on Node.js.  Getting it up and running and understanding its API are simple, making it a convenient target. At the same time, it provides &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3#broker-message-protocol"&gt;all the features Nest needs&lt;/a&gt; to build a transporter.&lt;/p&gt;

&lt;p&gt;Faye uses a very simple — almost &lt;em&gt;canonical&lt;/em&gt; — publish/subscribe protocol, as depicted in Figure 1 below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fcustom-transporter%2Fassets%2Ffaye-protocol2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fcustom-transporter%2Fassets%2Ffaye-protocol2.png" title="Faye Message Protocol" alt="Faye Message Protocol"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;a&gt;&lt;/a&gt;Figure 1: Faye Message Protocol



&lt;h3&gt;
  
  
  Building Basic Faye Client Apps
&lt;/h3&gt;

&lt;p&gt;As we did in the &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3"&gt;NestJS Microservices in Action&lt;/a&gt; series, we'll start by building simple native requestor and responder apps. These are TypeScript apps that exercise the Faye client API directly.  This will help us make sure we understand the Faye client API, and give us a convenient test bench.&lt;/p&gt;

&lt;p&gt;As covered in the &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3"&gt;previous article series&lt;/a&gt;, one of the challenges the Nest microservices package deals with is to layer a &lt;em&gt;request/response&lt;/em&gt; message style &lt;strong&gt;on top of&lt;/strong&gt; &lt;em&gt;publish/subscribe&lt;/em&gt; semantics.  In other words, Nest requestors need to be able to run code like this, which issues a request and returns a response from a remote microservice:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;customers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;getCustomers&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;client#send -&amp;gt; topic: "get-customers"&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="c1"&gt;// the following request returns a response as an Observable&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/get-customers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{});&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Supporting Request/Response
&lt;/h4&gt;

&lt;p&gt;Since Faye (as well as most brokers) only supports &lt;em&gt;publish/subscribe&lt;/em&gt;, we need to follow a simple recipe to provide &lt;em&gt;request/response&lt;/em&gt; semantics.  Here's a recap of the &lt;em&gt;generic pattern&lt;/em&gt; for how this is often done.&lt;/p&gt;

&lt;p&gt;Let's say component A wishes to "get customers" from component B, which has access to a customer DB. Component A can publish a &lt;code&gt;'/get-customers'&lt;/code&gt; message, and (assuming it has subscribed to that topic) component B receives it, queries the customer DB for a list of customers, and sends a response message which is delivered back to A. Handling the response message is where the magic happens. In order for B to respond to A, they both must do a few things, agreed upon by convention:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A chooses a &lt;em&gt;response topic&lt;/em&gt; (sometimes called a &lt;em&gt;reply subject&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;subscribes&lt;/strong&gt; to the response topic&lt;/li&gt;
&lt;li&gt;A &lt;strong&gt;publishes&lt;/strong&gt; a request, passing the &lt;em&gt;response topic&lt;/em&gt; as part of the message&lt;/li&gt;
&lt;li&gt;B &lt;strong&gt;publishes&lt;/strong&gt; a response, using the &lt;em&gt;response topic&lt;/em&gt; as the topic of its message&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 We've seen this &lt;em&gt;subscribe-to-the-response-then-send-the-request&lt;/em&gt; pattern a few times now, and we'll see it again. It's a vital pattern, and worth permanently etching into your synapses.  To that end, let's invent a simple mnemonic.  That way we can mentally conjure this pattern quickly whenever we need it.  I'll refer to it as &lt;strong&gt;STRPTQ&lt;/strong&gt;.  The letters come from "&lt;strong&gt;S&lt;/strong&gt;ubscribe &lt;strong&gt;T&lt;/strong&gt;o the &lt;strong&gt;R&lt;/strong&gt;esponse, then &lt;strong&gt;P&lt;/strong&gt;ublish &lt;strong&gt;T&lt;/strong&gt;he Re&lt;b&gt;Q&lt;/b&gt;uest".  I use &lt;strong&gt;Q&lt;/strong&gt; to make it easier to remember re&lt;b&gt;Q&lt;/b&gt;uest (vs. &lt;strong&gt;R&lt;/strong&gt;esponse), in the proper order.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;As we'll see in developing our Faye transporter, Nest makes this a bit easier by automatically choosing the response topic name for you. In fact, Nest builds &lt;strong&gt;two&lt;/strong&gt; topics from each &lt;strong&gt;pattern&lt;/strong&gt; you declare.  For example, assume you define a Nest microservice responder &lt;em&gt;message pattern&lt;/em&gt; like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;MessagePattern&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/get-customers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;(&lt;strong&gt;Note&lt;/strong&gt;: Nest uses the term &lt;strong&gt;channel&lt;/strong&gt; as a generic internal name for topics (also called subjects and channels by some brokers), and to help disambiguate them from the user-land concept of a &lt;strong&gt;pattern&lt;/strong&gt;, from which they are derived.)&lt;/p&gt;

&lt;p&gt;Internally, Nest builds two channels from the above pattern:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;'/get-customers_ack'&lt;/code&gt; - this is the physical channel name we'll use in the Faye transporter to publish/subscribe to &lt;strong&gt;request&lt;/strong&gt; messages on the topic &lt;code&gt;'/get-customers'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;'/get-customers_res'&lt;/code&gt; - this is the physical channel name we'll use in the Faye transporter to publish/subscribe to &lt;strong&gt;response&lt;/strong&gt; messages resulting from &lt;code&gt;'/get-customers'&lt;/code&gt; requests&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's build those native apps we mentioned.  &lt;strong&gt;Note&lt;/strong&gt;: later we'll want to use these native apps to test our Nest transporter, so to make them interact seamlessly with Nest, we'll utilize the Nest channel names described above.  We'll start with the responder app.&lt;/p&gt;

&lt;h4&gt;
  
  
  Native Responder App (customerService)
&lt;/h4&gt;

&lt;p&gt;This is the app that responds to inbound messages (requests) and generates responses.  Assuming you're &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/README.md#part-1-introduction-and-setup" rel="noopener noreferrer"&gt;following along&lt;/a&gt; on the &lt;code&gt;part1&lt;/code&gt; branch, open the file &lt;code&gt;customerService/src/service.ts&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here's the implementation of the &lt;code&gt;getCustomers&lt;/code&gt; &lt;em&gt;Faye subscription handler&lt;/em&gt;.  This is the code we register to run when our app receives an inbound message on the &lt;code&gt;'/get-customers_ack'&lt;/code&gt; channel (see the &lt;code&gt;subscribe()&lt;/code&gt; call lower in the file):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// customerService/src/service.ts&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getCustomers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;parsePacket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;packet&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s2"&gt;`\n========== &amp;lt;&amp;lt;&amp;lt; 'get-customers' message &amp;gt;&amp;gt;&amp;gt; ==========\n&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;\n=============================================\n`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="c1"&gt;// filter customers list if there's a `customerId` param&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;customers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
    &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;customerId&lt;/span&gt;
      &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;customerList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="nx"&gt;cust&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;cust&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nf"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;customerId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;customerList&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/get-customers_res&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;getPayload&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;customers&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The logic should be easy to understand. Refer to the full listing and make sure you see how we are using the &lt;code&gt;'/get-customers_ack'&lt;/code&gt; and &lt;code&gt;'/get-customers_res'&lt;/code&gt; channels here.  We'll be seeing a lot of that pattern, so make sure it makes sense to you.&lt;/p&gt;

&lt;p&gt;One detail to point out is the call to &lt;code&gt;getPayload()&lt;/code&gt;.  Let's discuss that.  As mentioned, we're building these native apps to conform to Nest protocols.  This includes using the Nest-standard channel names, as well as matching the internal Nest message format (message format is covered extensively in the &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3"&gt;NestJS Microservices in Action&lt;/a&gt; series).  Because we take this approach, the apps provide a convenient test bench for the Nest transporter &lt;code&gt;Server&lt;/code&gt; and &lt;code&gt;ClientProxy&lt;/code&gt; we're building in this article series.  The message format requirement means we need to wrap responses in a standard object we can depict like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;   &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="cm"&gt;/**
      *  Request error message, if any
      */&lt;/span&gt;
     &lt;span class="nl"&gt;err&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="cm"&gt;/**
      *  The response message payload (return value of a
      *  message pattern handler)
      */&lt;/span&gt;
     &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;
     &lt;span class="cm"&gt;/**
      *  Status of an Observable response. `true` once the final
      *  stream value has been retrieved.
      */&lt;/span&gt;
     &lt;span class="nx"&gt;isDisposed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;
     &lt;span class="cm"&gt;/**
      *  Unique identifier, corresponding to the id field received
      *  from the initial request
      */&lt;/span&gt;
     &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So &lt;code&gt;getPayload()&lt;/code&gt; is a helper that wraps payloads in this standard Nest message structure. If you're paying close attention, you'll notice that &lt;code&gt;getPayload()&lt;/code&gt; (not shown here — look at the &lt;code&gt;service.ts&lt;/code&gt; source code to see it) copies the &lt;code&gt;message.id&lt;/code&gt; field from the inbound request to the outbound message.  We haven't discussed these &lt;code&gt;id&lt;/code&gt; fields yet, but they'll become very important when we dive into the "client side" of our transporter starting in Part 5.&lt;/p&gt;

&lt;h4&gt;
  
  
  Native Requestor App (customerApp)
&lt;/h4&gt;

&lt;p&gt;This is the user-facing app that makes outbound requests. The requestor is in &lt;code&gt;customerApp/src/customer-app.ts&lt;/code&gt;.  Below is the implementation of the &lt;code&gt;getCustomers()&lt;/code&gt; function from that app.&lt;/p&gt;

&lt;p&gt;There's a little bit of boilerplate, but the main logic should be easy to see.  It implements the &lt;strong&gt;requestor&lt;/strong&gt; side of that &lt;strong&gt;STRPTQ&lt;/strong&gt; request/response protocol we walked through a couple of minutes ago.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// customerApp/src/customer-app.ts&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getCustomers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;customerId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// build Nest-shaped message&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getPayload&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/get-customers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;customerId&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nf"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// subscribe to the response message&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;subscription&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/get-customers_res&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s2"&gt;`==&amp;gt; Receiving 'get-customers' reply: \n&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;\n`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="c1"&gt;// once response is subscribed, publish the request&lt;/span&gt;
    &lt;span class="nx"&gt;subscription&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s2"&gt;`&amp;lt;== Sending 'get-customers' request with payload:\n&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;\n`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;pub&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/get-customers_ack&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;pub&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// wait .5 second to ensure subscription handler executes&lt;/span&gt;
        &lt;span class="c1"&gt;// then unsubscribe and resolve&lt;/span&gt;
        &lt;span class="nf"&gt;setTimeout&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="nx"&gt;subscription&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cancel&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
          &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="mi"&gt;500&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="p"&gt;});&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We &lt;strong&gt;first subscribe&lt;/strong&gt; to &lt;code&gt;'/get-customers_res'&lt;/code&gt; (the &lt;strong&gt;response channel&lt;/strong&gt;).  We pass it a simple &lt;em&gt;Faye subscription handler&lt;/em&gt; that prints out the result when a message on this channel comes back. This output becomes the "final response" to an end-user command (request) that's been issued from the command line (received by way of the basic command line processing logic at the bottom of the file).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Then we publish&lt;/strong&gt; the request on &lt;code&gt;'/get-customers_ack'&lt;/code&gt;.  Note that the call to &lt;code&gt;subscribe()&lt;/code&gt; returned a &lt;code&gt;subscription&lt;/code&gt; which is a Promise that resolves once the Faye server acknowledges that the subscribe operation has completed.&lt;/p&gt;

&lt;p&gt;Because we're running this as a standalone Node.js command line program, and thus want to exit after sending a request (which let's us use it as a command processor to conveniently send requests), we wait 500ms after publishing the request, then cancel the subscription and exit.  Cancelling the subscription is important hygiene — if we don't, subscriptions hang around (though Faye will eventually detect them and clean them up).  This is a bit brute force, and we'll want to do more sophisticated subscription management in our transporter client, but suffices for now.&lt;/p&gt;

&lt;p&gt;Finally, you'll notice that much like the &lt;code&gt;customerService&lt;/code&gt; responder app, we have a helper &lt;code&gt;getPayload()&lt;/code&gt; utility to wrap requests in the Nest message protocol. You'll also notice that we are generating the &lt;code&gt;id&lt;/code&gt; field — the same &lt;code&gt;id&lt;/code&gt; field we are handling on our responder app above.  For now, just think of the &lt;code&gt;id&lt;/code&gt; field as something we need to have in order to speak native Nest message protocol.  Later, we'll dive into this in more detail.&lt;/p&gt;

&lt;p&gt;For completeness, let's take a very quick look at how we run the Faye server. Though you can treat it as a black box, it's worth a moment to look at how it runs, and appreciate its native node.js-ness and simplicity.  Open up &lt;code&gt;faye-server/server.js&lt;/code&gt;.  The code is shown below.  You can read more about running the Faye server &lt;a href="https://faye.jcoglan.com/node.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;, but we're basically just starting it up, and for instrumentation purposes, listening for various events that we can log to the console to make it easy to trace the handshaking and message exchange. You shouldn't have to mess with this at all, but if you do, it's pretty basic stuff.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// faye-server/server.js&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;http&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;faye&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;faye&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mountPath&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/faye&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;port&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;8000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;server&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;http&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createServer&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bayeux&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;faye&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NodeAdapter&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;mount&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;mountPath&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;45&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;bayeux&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;attach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;server&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s2"&gt;`listening on http://localhost:&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;}${&lt;/span&gt;&lt;span class="nx"&gt;mountPath&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;\n========================================`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;bayeux&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;handshake&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;clientId&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;^^ client connect (#&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;substring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;)&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;bayeux&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;disconnect&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;clientId&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vv client disconnect (#&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;substring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;)&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;bayeux&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;publish&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s2"&gt;`&amp;lt;== New message from &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;substring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt; on channel &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s2"&gt;`\n    ** Payload: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;bayeux&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;subscribe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s2"&gt;`++ New subscription from &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;substring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt; on &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;bayeux&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;unsubscribe&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s2"&gt;`-- Unsubscribe by &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;clientId&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;substring&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt; on &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nx"&gt;channel&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Testing the Apps
&lt;/h3&gt;

&lt;p&gt;We've now completed our requestor and responder, and can test them out.  While I only reviewed the &lt;code&gt;'/get-customers'&lt;/code&gt; message flow above, the code in this branch also implements the &lt;code&gt;'/add-customer'&lt;/code&gt; message.  Take a minute to run the code now by following &lt;a href="https://github.com/johnbiundo/nestjs-faye-transporter-sample/blob/master/README.md#running-the-code" rel="noopener noreferrer"&gt;these simple instructions&lt;/a&gt;.  Here's what it looks like. In this video loop, the Faye broker is running in the top pane, the &lt;code&gt;customerService&lt;/code&gt; app is running in the middle pane, and the &lt;code&gt;customerApp&lt;/code&gt; app is running in the lower pane (and if you're curious, this is running in the &lt;a href="https://github.com/tmux/tmux" rel="noopener noreferrer"&gt;tmux&lt;/a&gt; virtual terminal; you can get my setup &lt;a href="https://github.com/johnbiundo/nest-nats-sample#pro-tip-use-tmux-optional" rel="noopener noreferrer"&gt;here&lt;/a&gt; if you're interested).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fcustom-transporter%2Fassets%2Ffaye-basic-demo2.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fcustom-transporter%2Fassets%2Ffaye-basic-demo2.gif" title="Native App Demo" alt="Native App Demo"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;a&gt;&lt;/a&gt;Screen Capture: Native App Demo



&lt;h3&gt;
  
  
  What's Next
&lt;/h3&gt;

&lt;p&gt;In &lt;a href="https://dev.to/nestjs/part-2-basic-server-component-onk"&gt;Part 2&lt;/a&gt;, we'll write and test a basic version of the Faye transporter's &lt;strong&gt;server&lt;/strong&gt; component.  This is the component that runs in the context of a &lt;strong&gt;microservice listener&lt;/strong&gt; to enable apps to function as Nest responders (these terms and concepts are discussed extensively in the &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3"&gt;NestJS Microservices in Action&lt;/a&gt; series).  We'll test this server component by using it in a simple Nest responder (microservice) app. That Nest microservice app will replace the native &lt;code&gt;customerApp&lt;/code&gt; we just built, and we'll make sure it responds to the exact same customer-related messages.&lt;/p&gt;

&lt;p&gt;Feel free to ask questions, make comments or suggestions, or just say hello in the comments below. And join us at &lt;a href="https://discord.gg/nestjs" rel="noopener noreferrer"&gt;Discord&lt;/a&gt; for more happy discussions about NestJS. I post there as &lt;em&gt;Y Prospect&lt;/em&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Part 3: Techniques for Application Integration with NestJS and NATS</title>
      <dc:creator>John Biundo</dc:creator>
      <pubDate>Wed, 12 Feb 2020 14:25:30 +0000</pubDate>
      <link>https://forem.com/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-3-4m20</link>
      <guid>https://forem.com/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-3-4m20</guid>
      <description>&lt;p&gt;&lt;em&gt;John is a member of the NestJS core team&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Part 3: Solving the Message Incompatibility Problem
&lt;/h3&gt;

&lt;p&gt;In &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-2-3hgd"&gt;Part 2&lt;/a&gt; of this series, we described the challenges of integrating Nest apps with non-Nest apps using NATS as an intermediary.&lt;/p&gt;

&lt;p&gt;In this article, we explore solutions to that problem.&lt;/p&gt;

&lt;h3&gt;
  
  
  Get the Code
&lt;/h3&gt;

&lt;p&gt;Reminder: all the code for these articles is available &lt;a href="https://github.com/johnbiundo/nest-nats-sample" rel="noopener noreferrer"&gt;here&lt;/a&gt;, with complete instructions &lt;a href="https://github.com/johnbiundo/nest-nats-sample#sample-repository-for-nestnatsmicroservice-article-series" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Serialization and Deserialization
&lt;/h3&gt;

&lt;p&gt;Serialization and deserialization are the steps Nest uses to translate between message formats. In other words, Nest receives inbound messages (from the broker), &lt;strong&gt;deserializes&lt;/strong&gt; them (converts arbitrary message formats into Nest message formats), and then processes them. For outbound messages, the process is reversed: the last step before sending a message to the broker is to (optionally) &lt;strong&gt;serialize&lt;/strong&gt; it (convert from Nest message format to some other format).&lt;/p&gt;

&lt;p&gt;The nifty part is that while Nest does all this automatically, it also provides hooks where you can easily plug in your own serializers and deserializers to handle arbitrary message formats. Serializers and deserializers are simply classes that implement a defined interface.&lt;/p&gt;

&lt;p&gt;Since requestors and responders perform their own unique roles (each with their own context) and each are both message senders and message receivers, &lt;strong&gt;there are four hooks where serialization/deserialization take place&lt;/strong&gt;. These are examined in the recipes below. If there's one section of this article series that you should bookmark for future reference, this is it. Each recipe describes exactly where you should look to handle a serialization/deserialization task, including the full context you need to understand why and how to do it.&lt;/p&gt;

&lt;h4&gt;
  
  
  Serialization/Deserialization Hooks and Recipes
&lt;/h4&gt;

&lt;p&gt;Let's examine the two different contexts (requestors and responders) in which we need to serialize/deserialize. For each, we'll examine handling inbound and outbound messages.&lt;/p&gt;

&lt;h5&gt;
  
  
  Nest as Requestor
&lt;/h5&gt;

&lt;p&gt;Nest requestors live in the context of a &lt;code&gt;ClientProxy&lt;/code&gt; instance. It follows that we customize serialization/deserialization behavior by modifying the &lt;code&gt;ClientProxy&lt;/code&gt; behavior. For example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// app.controller.ts&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;transport&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Transport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NATS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nats://localhost:4222&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;OutboundRequestSerializer&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="na"&gt;deserializer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;InboundResponseDeserializer&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ClientProxy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the code fragment above, &lt;code&gt;OutboundRequestSerializer&lt;/code&gt; is the class that answers the question &lt;em&gt;"how do does my Nest requestor format outgoing requests so that an external app can process them?"&lt;/em&gt;, while &lt;code&gt;InboundResponseDeserializer&lt;/code&gt; is the class that addresses &lt;em&gt;"how does my requestor translate incoming external responses so that Nest can process them?"&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: you can choose any name for these classes, but trust me, it's &lt;strong&gt;highly recommended&lt;/strong&gt; that you choose a naming convention similar to the ones above to keep things straight!&lt;/p&gt;

&lt;h5&gt;
  
  
  Nest as Responder
&lt;/h5&gt;

&lt;p&gt;Nest responders live in the context of a microservice instance. It follows that we customize serialization/deserialization behavior by modifying the microservice instance behavior. For example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// main.ts&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;bootstrap&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;NestFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createMicroservice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AppModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;transport&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Transport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NATS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nats://localhost:4222&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;deserializer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;InboundMessageDeserializer&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
      &lt;span class="na"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;OutboundResponseSerializer&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Microservice is listening...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;So &lt;code&gt;InboundMessageDeserializer&lt;/code&gt; is the class that answers the question &lt;em&gt;"how does my responder translate incoming external messages so that Nest can process them?"&lt;/em&gt;, while &lt;code&gt;OutboundResponseSerializer&lt;/code&gt; is the class that handles &lt;em&gt;"how does my responder format responses so that the external app can understand them?"&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;This feature results in the following diagrams — ones that remove all those ugly red X's in the &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-2-3hgd#figure4"&gt;last diagram&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;Here's the diagram to keep in mind when writing &lt;strong&gt;serializers/deserializers for Nest responders&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fmicro-nats-3%2Fassets%2Fresponder-serialization.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fmicro-nats-3%2Fassets%2Fresponder-serialization.png" title="Nest Responder (De)serialization" alt="Nest Responder (De)serialization"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a&gt;&lt;/a&gt;Figure 1: Nest Responder (De)serialization&lt;/p&gt;

&lt;p&gt;In the above diagram, the Nest responder serializer and deserializer are configured in the &lt;code&gt;NestFactory.creatMicroservice()&lt;/code&gt; call.&lt;/p&gt;

&lt;p&gt;And here's the corresponding diagram for &lt;strong&gt;Nest requestors&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fmicro-nats-3%2Fassets%2Frequestor-serialization.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fmicro-nats-3%2Fassets%2Frequestor-serialization.png" title="Nest Requestor (De)serialization" alt="Nest Requestor (De)serialization"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a&gt;&lt;/a&gt;Figure 2: Nest Requestor (De)serialization&lt;/p&gt;

&lt;p&gt;In the above diagram, the Nest requestor serializer and deserializer are configured in the &lt;code&gt;ClientProxy&lt;/code&gt; configuration (decorator or injectable, depending on &lt;a href="https://docs.nestjs.com/microservices/basics#client" rel="noopener noreferrer"&gt;which method&lt;/a&gt; you use).&lt;/p&gt;

&lt;h4&gt;
  
  
  Implementing Serializers and Deserializers
&lt;/h4&gt;

&lt;p&gt;Before we start writing serializer/deserializer classes, let's take a look at their interfaces.&lt;/p&gt;

&lt;h5&gt;
  
  
  Serializer Interface
&lt;/h5&gt;

&lt;p&gt;A serializer class needs to implement the &lt;code&gt;serialize()&lt;/code&gt; method, which takes a single argument — the outbound payload to be serialized — and returns the serialized payload.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Serializer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;TInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;TOutput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;TInput&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;TOutput&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We'll now implement an &lt;strong&gt;identity serializer&lt;/strong&gt; for our Nest responder as an exercise. An identity serializer simply returns the same message it receives. In addition to exercising the interface, we can use it to "spy on" the otherwise silent serialization function that Nest always performs on our behalf.&lt;/p&gt;

&lt;h5&gt;
  
  
  Responder Identity Serializer Implementation
&lt;/h5&gt;

&lt;p&gt;Here, we'll be working in the &lt;em&gt;nestMicroservice&lt;/em&gt; project (i.e., nest-nats-sample/nestMicroservice directory). Take a look at the &lt;code&gt;src/common/serializers/outbound-response-identity.serializer.ts&lt;/code&gt; file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/common/serializers/outbount-response-identity.serializer.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Serializer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;OutgoingResponse&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/microservices&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Logger&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OutboundResponseIdentitySerializer&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;Serializer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;logger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;OutboundResponseIdentitySerializer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;OutgoingResponse&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;debug&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="s2"&gt;`--&amp;gt;&amp;gt; Serializing outbound response: \n&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now simply plug this serializer into the &lt;code&gt;main.ts&lt;/code&gt; file for the Nest responder app.  If you're following along with the github repository, the &lt;code&gt;main.ts&lt;/code&gt; file will have all of this code (and more that we'll get to soon) in place, but commented out.  Simply uncomment the line shown below.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/main.ts&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;NestFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createMicroservice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AppModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;transport&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Transport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NATS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;customers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nats://localhost:4222&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="cm"&gt;/**
      * Use the "Identity" (de)serializers for observing messages for
      * nest-only deployment.
      */&lt;/span&gt;
    &lt;span class="na"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;OutboundResponseIdentitySerializer&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you now send some requests to the &lt;em&gt;nestMicroservice&lt;/em&gt; app from the &lt;em&gt;nestHttpApp&lt;/em&gt; (&lt;a href="https://github.com/johnbiundo/nest-nats-sample#running-the-all-nest-configuration" rel="noopener noreferrer"&gt;here's how&lt;/a&gt;), you'll see logging information that looks something like this, showing you the internal layout of the outbound message:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="o"&gt;[&lt;/span&gt;Nest] 8786   - 02/04/2020, 10:19:04 AM   &lt;span class="o"&gt;[&lt;/span&gt;OutboundResponseIdentitySerializer] &lt;span class="nt"&gt;--&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt; Serializing outbound response:
&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"err"&lt;/span&gt;:null,&lt;span class="s2"&gt;"response"&lt;/span&gt;:[],&lt;span class="s2"&gt;"isDisposed"&lt;/span&gt;:true,&lt;span class="s2"&gt;"id"&lt;/span&gt;:&lt;span class="s2"&gt;"41da72e8-09d0-4720-9404-bd0977b034a0"&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h5&gt;
  
  
  Deserializer Interface
&lt;/h5&gt;

&lt;p&gt;Now let's take a look at the required interface for a deserializer.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Deserializer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;TInput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;TOutput&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;deserialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;TInput&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nb"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;TOutput&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;A deserializer class needs to implement the &lt;code&gt;deserialize()&lt;/code&gt; method, which takes a two arguments — the payload to be deserialized and an optional &lt;code&gt;options&lt;/code&gt; object — and returns the deserialized payload.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;options&lt;/code&gt; object contains metadata about the incoming message. For NATS, the object contains the value of the &lt;code&gt;replyTo&lt;/code&gt; message property, if one exists.&lt;/p&gt;

&lt;p&gt;Let's implement a deserializer. To start with, we'll build an "identity deserializer" that just logs out the contents of any incoming message without doing any transformation on it.&lt;/p&gt;

&lt;h5&gt;
  
  
  Responder Identity Deserializer Implementation
&lt;/h5&gt;

&lt;p&gt;We're still working on the Nest responder — the &lt;em&gt;nestMicroservice&lt;/em&gt; project (i.e., nest-nats-sample/nestMicroservice directory). Take a look at the &lt;code&gt;src/common/deserializers/inbound-message-identity.deserializer.ts&lt;/code&gt; file:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/common/serializers/inbound-message-identity.deserializer.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ConsumerDeserializer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;IncomingRequest&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/microservices&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Logger&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;InboundMessageIdentityDeserializer&lt;/span&gt;
  &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;ConsumerDeserializer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;logger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;InboundMessageIdentityDeserializer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;deserialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nb"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;IncomingRequest&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;verbose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="s2"&gt;`&amp;lt;&amp;lt;-- deserializing inbound message:\n&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nx"&gt;value&lt;/span&gt;
      &lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;\n\twith options: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Like our identity serializer, we can quickly plug in an identity deserializer to spy on the incoming messages from our requestor.  Again, if you're following along using the github repository, simply comment out the lines so the active (de)serializers match those shown below.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/main.ts&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;NestFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createMicroservice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AppModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;transport&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Transport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NATS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;customers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nats://localhost:4222&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="cm"&gt;/**
      * Use the "Identity" (de)serializers for observing messages for
      * nest-only deployment.
      */&lt;/span&gt;
    &lt;span class="na"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;OutboundResponseIdentitySerializer&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="na"&gt;deserializer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;InboundMessageIdentityDeserializer&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you again send some requests to the &lt;em&gt;nestMicroservice&lt;/em&gt; app from the &lt;em&gt;nestHttpApp&lt;/em&gt; (&lt;a href="https://github.com/johnbiundo/nest-nats-sample#running-the-all-nest-configuration" rel="noopener noreferrer"&gt;like this&lt;/a&gt;), you'll see logging information that looks something like this, showing you the layout of the inbound message:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="o"&gt;[&lt;/span&gt;Nest] 8786   - 02/04/2020, 10:19:04 AM   &lt;span class="o"&gt;[&lt;/span&gt;InboundMessageIdentityDeserializer] &amp;lt;&amp;lt;&lt;span class="nt"&gt;--&lt;/span&gt; deserializing inbound message:
&lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"pattern"&lt;/span&gt;:&lt;span class="s2"&gt;"get-customers"&lt;/span&gt;,&lt;span class="s2"&gt;"data"&lt;/span&gt;:&lt;span class="o"&gt;{}&lt;/span&gt;,&lt;span class="s2"&gt;"id"&lt;/span&gt;:&lt;span class="s2"&gt;"41da72e8-09d0-4720-9404-bd0977b034a0"&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;
        with options: &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"channel"&lt;/span&gt;:&lt;span class="s2"&gt;"get-customers"&lt;/span&gt;,&lt;span class="s2"&gt;"replyTo"&lt;/span&gt;:&lt;span class="s2"&gt;"_INBOX.IDML09N4JB6W0MF4P6GBPB.IDML09N4JB6W0MF4P6GCX2"&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Notice the value for the &lt;code&gt;replyTo&lt;/code&gt; field in the logging output. You can probably guess what that is. This is a detail that's going to come in handy down the line, so file that away for future reference.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementing the Integration Use Cases
&lt;/h3&gt;

&lt;p&gt;We've nearly arrived at our final destination. With the understanding we've gained, we can specify what we need to complete our integration use cases from &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3"&gt;Figure 1 in Part 1&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Here are the requirements:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;The &lt;em&gt;nestHttpApp&lt;/em&gt;, in its role as a requestor, must implement:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A) &lt;a&gt;&lt;/a&gt;an &lt;strong&gt;outbound message external serializer&lt;/strong&gt; that translates a Nest formatted request into a request understood by our external service. For example:

&lt;ul&gt;
&lt;li&gt;From Nest format

&lt;ul&gt;
&lt;li&gt;topic: &lt;code&gt;'get-customers'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;reply topic: &lt;code&gt;'_INBOX.XVRL...'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;payload: &lt;code&gt;{pattern: 'get-customers', data: {}, id: 'abc...'}&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;To external format

&lt;ul&gt;
&lt;li&gt;topic: &lt;code&gt;'get-customers'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;reply topic: &lt;code&gt;'_INBOX.XVRL...'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;payload: &lt;code&gt;{}&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;B) &lt;a&gt;&lt;/a&gt;an &lt;strong&gt;inbound response external deserializer&lt;/strong&gt; that translates an external response into a format understood by Nest. For example:

&lt;ul&gt;
&lt;li&gt;From external format

&lt;ul&gt;
&lt;li&gt;topic: &lt;code&gt;'_INBOX.XVRL...'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;payload: &lt;code&gt;{customers: [{id: 1, name: 'nestjs.com'}]}&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;To Nest format

&lt;ul&gt;
&lt;li&gt;topic:&lt;code&gt;'_INBOX.XVRL...'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;payload: &lt;code&gt;{err: undefined, response: {customers: [{id: 1, name: 'nestjs.com'}]}, isDisposed: true}&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;The &lt;em&gt;nestMicroservice&lt;/em&gt; app, in its role as a responder, must implement:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A) &lt;a&gt;&lt;/a&gt;an &lt;strong&gt;inbound message external deserializer&lt;/strong&gt; that translates an external request into a format understood by Nest.  For example:

&lt;ul&gt;
&lt;li&gt;From external format

&lt;ul&gt;
&lt;li&gt;topic: &lt;code&gt;'get-customers'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;reply: &lt;code&gt;'_INBOX.XVRL...'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;payload: &lt;code&gt;{}&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;To Nest format

&lt;ul&gt;
&lt;li&gt;topic &lt;code&gt;'get-customers'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;reply topic: &lt;code&gt;'_INBOX.XVRL...'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;payload: &lt;code&gt;{pattern: 'get-customers', data: {}, id: 'abc...'}&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;B) &lt;a&gt;&lt;/a&gt;an &lt;strong&gt;outbound response external serializer&lt;/strong&gt; that translates a Nest formatted response into a response understood by our external service.  For example:

&lt;ul&gt;
&lt;li&gt;From Nest format

&lt;ul&gt;
&lt;li&gt;topic: &lt;code&gt;'_INBOX.XVRL...'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;payload: &lt;code&gt;{err: undefined, response: {customers: [{id: 1, name: 'nestjs.com'}]}, isDisposed: true}&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;To external format

&lt;ul&gt;
&lt;li&gt;topic: &lt;code&gt;'_INBOX.XVRL...'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;payload: &lt;code&gt;{customers: [{id: 1, name: 'nestjs.com'}]}&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;With that in mind, let's get busy!  The &lt;strong&gt;bolded&lt;/strong&gt; descriptions above inform the names of our classes.&lt;/p&gt;

&lt;p&gt;For requirement 1-A, we have &lt;code&gt;src/common/serializers/outbound-message-external.serializer.ts&lt;/code&gt; in our &lt;em&gt;nestHttpApp&lt;/em&gt; project.  Here's the code.  The comments explain its intent.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/common/serializers/outbound-message-external.serializer.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Logger&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Serializer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/microservices&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OutboundMessageExternalSerializer&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;Serializer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;logger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;OutboundMessageExternalSerializer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;debug&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="s2"&gt;`--&amp;gt;&amp;gt; Serializing outbound message: \n&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="cm"&gt;/**
     * Here, we are merely "unpacking" the request payload from the Nest
     * message structure and returning it as a "plain" top-level object.
     */&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;For requirement 1-B, we have &lt;code&gt;src/common/deserializers/inbound-response-external.deserializer.ts&lt;/code&gt; in our &lt;em&gt;nestHttpApp&lt;/em&gt; project.  Here's the code.  The comments explain its intent.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/common/deserializers/inbound-response-external.deserializer.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;WritePacket&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Deserializer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/microservices&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Logger&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;InboundResponseExternalDeserializer&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;Deserializer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;logger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;InboundResponseExternalDeserializer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nf"&gt;deserialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;WritePacket&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;verbose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="s2"&gt;`&amp;lt;&amp;lt;-- deserializing inbound response:\n&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="cm"&gt;/**
     * Here, we wrap the external payload received in a standard Nest
     * response message.  Note that we have omitted the `id` field, as it
     * does not have any meaning from an external responder.  Because of this,
     * we have to also:
     *   1) implement the `Deserializer` interface instead of the
     *      `ProducerDeserializer` interface used in the identity deserializer
     *   2) return an object with the `WritePacket` interface, rather than
     *      the`IncomingResponse` interface used in the identity deserializer.
     */&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;err&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;response&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;isDisposed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;For requirement 2-A, we have &lt;code&gt;src/common/deserializers/inbound-message-external.deserializer.ts&lt;/code&gt; in our &lt;em&gt;nestMicroservice&lt;/em&gt; project.  Here's the code.  The comments explain its intent.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/common/serializers/inbound-message-external.deserializer.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Logger&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;uuid&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;uuid/v4&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ConsumerDeserializer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/microservices&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;InboundMessageExternalDeserializer&lt;/span&gt;
  &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;ConsumerDeserializer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;logger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;InboundMessageExternalDeserializer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;deserialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;?:&lt;/span&gt; &lt;span class="nb"&gt;Record&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;verbose&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="s2"&gt;`&amp;lt;&amp;lt;-- deserializing inbound external message:\n&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;\n\twith options: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="cm"&gt;/**
     * Here, we merely wrap our inbound message payload in the standard Nest
     * message structure.
     */&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;uuid&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;For requirement 2-B, we have &lt;code&gt;src/common/serializers/outbound-response-external.serializer.ts&lt;/code&gt; in our &lt;em&gt;nestMicroservice&lt;/em&gt; project.  Here's the code.  The comments explain its intent.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/common/serializers/outbound-response-external.serializer.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Serializer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;OutgoingResponse&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/microservices&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Logger&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;OutboundResponseExternalSerializer&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;Serializer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;logger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;OutboundResponseExternalSerializer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;serialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;OutgoingResponse&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;debug&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="s2"&gt;`--&amp;gt;&amp;gt; Serializing outbound response: \n&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="cm"&gt;/**
     * Here, we are merely "unpacking" the response payload from the Nest
     * message structure, and returning it as a "plain" top-level object.
     */&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  Enabling the External (De)serializers
&lt;/h4&gt;

&lt;p&gt;The last step is to plug in these (de)serializers in at the appropriate "hook points".  We've seen this before.  For the &lt;code&gt;nestHttpApp&lt;/code&gt;, this is done wherever you configure the &lt;code&gt;ClientProxy&lt;/code&gt;.  In our case, for ease of code organization, we've done this with the &lt;code&gt;@Client()&lt;/code&gt; decorator in the &lt;code&gt;src/app.controller.ts&lt;/code&gt; file.  To enable the external (de)serializers, update that file to look like this:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/app.controller.ts&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;transport&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Transport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NATS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nats://localhost:4222&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="cm"&gt;/**
       * Use the "Identity" (de)serializers for observing messages for
       * nest-only deployment.
       */&lt;/span&gt;
      &lt;span class="c1"&gt;// serializer: new OutboundMessageIdentitySerializer(),&lt;/span&gt;
      &lt;span class="c1"&gt;// deserializer: new InboundResponseIdentityDeserializer(),&lt;/span&gt;

      &lt;span class="cm"&gt;/**
       * Use the "External" (de)serializers for transforming messages to/from
       * (only) an external responder
       */&lt;/span&gt;
      &lt;span class="na"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;OutboundMessageExternalSerializer&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
      &lt;span class="na"&gt;deserializer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;InboundResponseExternalDeserializer&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="nx"&gt;custClient&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ClientProxy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;For the &lt;code&gt;nestMicroservice&lt;/code&gt;, this is done in the &lt;code&gt;src/main.ts&lt;/code&gt; file.  To enable the external (de)serializers, update that file to look like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/main.ts&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;NestFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createMicroservice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AppModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;transport&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Transport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NATS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;queue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;customers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nats://localhost:4222&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="cm"&gt;/**
       * Use the "Identity" (de)serializers for observing messages for
       * nest-only deployment.
       */&lt;/span&gt;
      &lt;span class="c1"&gt;// serializer: new OutboundResponseIdentitySerializer(),&lt;/span&gt;
      &lt;span class="c1"&gt;// deserializer: new InboundMessageIdentityDeserializer(),&lt;/span&gt;

      &lt;span class="cm"&gt;/**
       * Use the "External" (de)serializers for transforming messages to/from
       * an external requestor
       */&lt;/span&gt;
      &lt;span class="na"&gt;serializer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;OutboundResponseExternalSerializer&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
      &lt;span class="na"&gt;deserializer&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;InboundMessageExternalDeserializer&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;At this point, you should be able to send and receive requests between any combination of apps.  For example, from &lt;em&gt;nestHttpApp&lt;/em&gt; to &lt;em&gt;customerService&lt;/em&gt;, from &lt;em&gt;customerApp&lt;/em&gt; to &lt;em&gt;nestMicroservice&lt;/em&gt;, and all other combinations.  I highly recommend you do so now, using the instructions from &lt;a href="https://github.com/johnbiundo/nest-nats-sample#running-the-all-nest-configuration" rel="noopener noreferrer"&gt;here&lt;/a&gt; and &lt;a href="https://github.com/johnbiundo/nest-nats-sample#running-the-all-native-app-configuration" rel="noopener noreferrer"&gt;here&lt;/a&gt; to run all components at the same time*. Pay close attention to the log outputs which show how (de)serialization is happening at each step.&lt;/p&gt;

&lt;p&gt;*&lt;a&gt;&lt;/a&gt;We haven't described the use of &lt;code&gt;queues&lt;/code&gt;, but when you start running the full mixed configuration (Nest and non-Nest components), one thing you'll notice is that requests will be randomly routed between the &lt;em&gt;nestMicroservice&lt;/em&gt; app and the &lt;em&gt;customerService&lt;/em&gt; app.  This is because they are members of a &lt;strong&gt;NATS queue&lt;/strong&gt;. If you want to send a request to a particular responder, you'll need to shut down the other one to guarantee the message is sent there.  I'll discuss NATS queues in more detail the final article of this series.&lt;/p&gt;

&lt;p&gt;Here's a nice surprise! We've magically handled &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3"&gt;Figure 1 Case D&lt;/a&gt; along the way.  If you think carefully about it, this is both not a surprise, and has a somewhat strict limitation.  The reason this works is that we're now serializing and deserializing to and from a canonical external format.  So &lt;strong&gt;all&lt;/strong&gt; requests look like they come from an external requestor, and &lt;strong&gt;all&lt;/strong&gt; responses look like they come from an external responder.  With this in place, our serializers and deserializers work in all combinations.  We've created a "lingua franca" message protocol.  The limitation with this approach is perhaps a subtle one, and is something we'll address later (see bullet point #2 in &lt;strong&gt;What's Next?&lt;/strong&gt; below).&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion 🚀
&lt;/h3&gt;

&lt;p&gt;We've covered a lot of ground.  Hopefully you've got both a conceptual framework for how to think about integrating external applications with Nest apps using tools like Nest microservices and the NATS message broker, and some boilerplate code you can work with.  Some of these concepts also translate directly to other Nest microservice transporters, but as you can imagine, the details are different.  Holler in the comments if you'd like me to cover other transporters in this fashion in another series, and I'll see if I can do so.&lt;/p&gt;

&lt;h3&gt;
  
  
  What's next? ❓
&lt;/h3&gt;

&lt;p&gt;There are a few remaining subtle topics to cover, which I'll do in the next installment of this series. As a teaser, these include:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Can we run both the &lt;em&gt;nestMicroservice&lt;/em&gt; app and the external &lt;em&gt;customerService&lt;/em&gt; app at the same time, and load balance requests across them?  Spoiler: yes!  In fact, we already did so, as mentioned above. This is simple to do with NATS &lt;a href="https://docs.nats.io/nats-concepts/queue" rel="noopener noreferrer"&gt;distributed queues&lt;/a&gt;, which we'll cover briefly in the next article.&lt;/li&gt;
&lt;li&gt;We covered the case where we're running our modified &lt;em&gt;nestHttpApp&lt;/em&gt; in this &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3"&gt;hybrid&lt;/a&gt; (Nest and non-Nest) environment.  What happens if we have a mixture of Nest apps that are unmodified  —  that is, they are deployed apps that we don't want to touch to implement our "lingua franca" message protocol (i.e., they're running out-of-the-box standard de(serializers))?  Can they be made to play nicely in this hybrid environment?  Spoiler: yes!&lt;/li&gt;
&lt;li&gt;What about &lt;strong&gt;events&lt;/strong&gt;?  We've covered what seems to be the trickier case here with request/response style messaging, but how about plain old events?  In fact, you may have noticed we've built a feature for &lt;strong&gt;adding a customer&lt;/strong&gt; (see the &lt;code&gt;'add-customer'&lt;/code&gt; messages and handlers sprinkled through our code).  Go ahead and try them now. You'll see you have mixed results. Can we make events cooperate in this hybrid environment?  Spoiler: yes!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Stay tuned for the answers to these and other exciting questions in our next installment! 😃&lt;/p&gt;

&lt;p&gt;Feel free to ask questions, make comments or suggestions, or just say hello in the comments below. And join us at &lt;a href="https://discord.gg/nestjs" rel="noopener noreferrer"&gt;Discord&lt;/a&gt; for more happy discussions about NestJS. I post there as &lt;em&gt;Y Prospect&lt;/em&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Part 2: Challenges of External Integration</title>
      <dc:creator>John Biundo</dc:creator>
      <pubDate>Tue, 11 Feb 2020 15:32:30 +0000</pubDate>
      <link>https://forem.com/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-2-3hgd</link>
      <guid>https://forem.com/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-2-3hgd</guid>
      <description>&lt;p&gt;&lt;em&gt;John is a member of the NestJS core team&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Part 2: Digging into Transporter Communications
&lt;/h3&gt;

&lt;p&gt;This is Part 2 of a series on using Nest microservices as an integration technology. &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3"&gt;Part 1&lt;/a&gt; lays the foundation with an introduction to the basic communication concepts used by Nest microservices.&lt;/p&gt;

&lt;p&gt;This article lays out the concepts and challenges of integrating Nest apps with non-Nest apps.&lt;/p&gt;

&lt;h3&gt;
  
  
  Get the Code
&lt;/h3&gt;

&lt;p&gt;Reminder: all the code for these articles is available &lt;a href="https://github.com/johnbiundo/nest-nats-sample" rel="noopener noreferrer"&gt;here&lt;/a&gt;, with complete instructions &lt;a href="https://github.com/johnbiundo/nest-nats-sample#sample-repository-for-nestnatsmicroservice-article-series" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Roles in Action
&lt;/h3&gt;

&lt;p&gt;In the prior article, I covered &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3#a-vocabulary-for-transporter-use-cases"&gt;the roles&lt;/a&gt; a Nest app plays in various communication scenarios. Let's dig further into those roles and how they matter in each integration use case.&lt;/p&gt;

&lt;h4&gt;
  
  
  Nest as Requestor
&lt;/h4&gt;

&lt;p&gt;Let's start with a quick review of Nest's transporter application-level protocol. This is covered in depth &lt;a href="https://docs.nestjs.com/microservices/basics#client" rel="noopener noreferrer"&gt;in the Nest documentation&lt;/a&gt;, but we'll summarize briefly here, marrying Nest concepts with our freshly minted terminology covered at the end of &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3#a-vocabulary-for-transporter-use-cases"&gt;Part 1&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;When a Nest component is in the role of &lt;strong&gt;Nest as requestor&lt;/strong&gt;, it performs its role through an instance of the &lt;code&gt;ClientProxy&lt;/code&gt; class which has been configured to work with a specific transporter. Such a requestor may be housed in any sort of Nest app. For example, to make a &lt;code&gt;'get-customers'&lt;/code&gt; request from a Nest HTTP-based app (e.g., from within a REST route handler), via NATS, we would instantiate a &lt;code&gt;ClientProxy&lt;/code&gt; with code something like this:*&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app.controller.ts&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;transport&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Transport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NATS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nats://localhost:4222&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ClientProxy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;*Note: we use the &lt;code&gt;@Client()&lt;/code&gt; decorator as a convenience, but Nest recommends using the dependency injection method for creating a &lt;code&gt;ClientProxy&lt;/code&gt; instance. Both work for our purposes, but the DI method has some advantages for production development. See the &lt;a href="https://docs.nestjs.com/microservices/basics#client" rel="noopener noreferrer"&gt;Nest documentation&lt;/a&gt; for more details.&lt;/p&gt;

&lt;p&gt;Using this &lt;code&gt;ClientProxy&lt;/code&gt; instance, we would then add code to our REST route handler (e.g., one that responds to the REST &lt;code&gt;GET customers&lt;/code&gt; request) to issue the NATS request with code something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app.controller.ts&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;customers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;getCustomers&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nx"&gt;Observable&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;customers&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;get-customers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's update the diagram from &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3#figure1"&gt;Figure 1, Case C&lt;/a&gt; to reflect this terminology.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fmicro-nats-2%2Fassets%2Fcase-c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fmicro-nats-2%2Fassets%2Fcase-c.png" title="Case C" alt="Case C"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a&gt;&lt;/a&gt;Figure 1: Nest as Requestor&lt;/p&gt;
&lt;h4&gt;
  
  
  Nest as Responder
&lt;/h4&gt;

&lt;p&gt;When a Nest component is in the role of &lt;strong&gt;Nest as responder&lt;/strong&gt;, things are slightly more complex. We need the component to function &lt;strong&gt;inside the context of a network listener&lt;/strong&gt; (in order to receive inbound messages from remote senders). This concept gives us an understanding of what we can now refer to as a Nest &lt;strong&gt;microservice&lt;/strong&gt;. A Nest microservice is a component that binds some behavior to incoming network messages bearing specific &lt;strong&gt;topics&lt;/strong&gt; (and, optionally, &lt;strong&gt;payloads&lt;/strong&gt;). Obviously the microservice listener must connect to the correct broker, and possibly be configured with other parameters. Making this association between a microservice listener and a particular broker, and specifying configuration parameters for that association, is the job of the transporter. For example, a Nest component that can respond to a NATS &lt;code&gt;'get-customer'&lt;/code&gt; message would need several moving parts.&lt;/p&gt;

&lt;p&gt;First we need to start up a network listener. This is covered in detail &lt;a href="https://docs.nestjs.com/microservices/basics#getting-started" rel="noopener noreferrer"&gt;in the Nest documentation here&lt;/a&gt;, but the code is straightforward if you're familiar with a typical Nest &lt;code&gt;main.ts&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// main.ts&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;bootstrap&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;NestFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;createMicroservice&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AppModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;transport&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Transport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;NATS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;url&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nats://localhost:4222&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Microservice is listening...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All the transporter-specific details — such as how to connect to the NATS broker — are passed in as an options object to the &lt;code&gt;createMicroservice()&lt;/code&gt; call.&lt;/p&gt;

&lt;p&gt;Then we need to register the handler for any inbound request we want to handle. This is covered in detail &lt;a href="https://docs.nestjs.com/microservices/basics#getting-started" rel="noopener noreferrer"&gt;here&lt;/a&gt;, but the handler code, running inside a microservice controller, is straightforward if you're familiar with typical Nest HTTP controllers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app.controller.ts&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;MessagePattern&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;get-customers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nf"&gt;getCustomers&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Payload&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;customerService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;getCustomers&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can now update &lt;a href=""&gt;Figure 1 Case B&lt;/a&gt; to reflect this understanding.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fmicro-nats-2%2Fassets%2Fcase-b.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fmicro-nats-2%2Fassets%2Fcase-b.png" title="Case C" alt="Case b"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a&gt;&lt;/a&gt;Figure 2: Nest as Responder&lt;/p&gt;

&lt;p&gt;The other roles discussed earlier — emitter and subscriber — are similar (and simpler). For brevity, we'll omit the diagrams and just describe the differences. An emitter is like a requestor except it does not expect a response, so it does not subscribe to a response message. Like a requestor, it's issued via a &lt;code&gt;ClientProxy&lt;/code&gt; instance. A subscriber is like a responder except it does not issue a response message. Like a responder, it's housed as a handler within a microservice listener.&lt;/p&gt;
&lt;h4&gt;
  
  
  Running an All Nest Stack
&lt;/h4&gt;

&lt;p&gt;Full implementations of these apps are included in the repository available &lt;a href="https://github.com/johnbiundo/nest-nats-sample" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Running these two Nest apps as shown in &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3#figure1"&gt;Case A of Figure 1&lt;/a&gt;, which you can do now by following &lt;a href="https://github.com/johnbiundo/nest-nats-sample#running-the-all-nest-configuration" rel="noopener noreferrer"&gt;these instructions&lt;/a&gt;, is easy. Following those steps will start up an instance of a NATS broker, in verbose mode, which is very helpful for watching the message flow. Take a few minutes to run through the full installation and to examine the code.&lt;/p&gt;

&lt;p&gt;Of course so far, we haven't really had to deal with any external app components. This means there are no messy details, and the Nest components "just work". Let's move on to the topic of external app components now.&lt;/p&gt;
&lt;h3&gt;
  
  
  External NATS app
&lt;/h3&gt;

&lt;p&gt;To give us something concrete to look at, let's quickly construct basic implementations of our external apps. We'll build two of them: one as a requestor and one as a responder. These will serve as sandboxes for examining live message flow behavior, and play the role of the "external apps" we see in &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3#figure1"&gt;Figure 1&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As native NATS apps, these can immediately and seamlessly communicate with each other.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fmicro-nats-2%2Fassets%2Fexternal-apps-nats.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fmicro-nats-2%2Fassets%2Fexternal-apps-nats.png" title="External Apps" alt="external apps"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a&gt;&lt;/a&gt;Figure 3: External NATS Apps&lt;/p&gt;
&lt;h4&gt;
  
  
  The customerApp application
&lt;/h4&gt;

&lt;p&gt;This app functions as a &lt;strong&gt;requestor&lt;/strong&gt;, and is quite simple. When the app is called with the command line argument &lt;code&gt;"get-customers"&lt;/code&gt;, it first connects to the NATS server, then runs the &lt;code&gt;getCustomers()&lt;/code&gt; function. Here's the essence of that function (slightly simplified, and minus a little logging and error handling).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// customer-app.ts&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getCustomers&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;nats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;get-customers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{});&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;getCustomers reply: &lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  The customerService application
&lt;/h4&gt;

&lt;p&gt;This app functions as a &lt;strong&gt;responder&lt;/strong&gt;. The code below is the essence of the &lt;code&gt;main()&lt;/code&gt; function, which simply starts up, connects to NATS, and subscribes to a couple of topics. &lt;strong&gt;Note:&lt;/strong&gt; to stay focused on the important elements, the code below is slightly simplified from the code in the repository, though the overall structure and intent remains the same.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// service.ts&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;nats&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;servers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;NATS_URL&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="na"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;NATS customer service starts...&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;nats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;on&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;connect&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;client&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;serverInfo&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;nats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;get-customers&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;getCustomers&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;nats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;add-customer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;addCustomer&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code below is the essence of the &lt;code&gt;getCustomers()&lt;/code&gt; callback function (again, simplified slightly from the repo code), which has been registered as the handler (line #9 above) to call when the &lt;code&gt;'get-customers'&lt;/code&gt; message is received.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// service.ts&lt;/span&gt;
&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getCustomers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;customers&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
        &lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;
          &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;customerList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;filter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
              &lt;span class="nx"&gt;cust&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;cust&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nf"&gt;parseInt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;)&lt;/span&gt;
          &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;customerList&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;nats&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;publish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;reply&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;customers&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Malformed request.  No reply field included in request.&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you look at the full repository source code, you'll see that our "database" is just a stub with an array of customers stored in a local &lt;code&gt;customerList&lt;/code&gt; variable.&lt;/p&gt;

&lt;p&gt;Most importantly, the &lt;code&gt;getCustomers()&lt;/code&gt; method shows the &lt;strong&gt;request-response&lt;/strong&gt; implementation clearly. The logic is straightforward:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;check to see if a customer id is supplied. If so retrieve that customer, otherwise retrieve all customers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;publish&lt;/strong&gt; a response message using the &lt;code&gt;reply&lt;/code&gt; topic passed in on the request message.  Include the customer list as the payload in that response.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The full source code for these apps is available &lt;a href="https://github.com/johnbiundo/nest-nats-sample" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Read more about running them &lt;a href="https://github.com/johnbiundo/nest-nats-sample#running-the-all-native-app-configuration" rel="noopener noreferrer"&gt;here&lt;/a&gt;. I highly encourage you to do this to get familiar with their behavior, and to observe the NATS message flows first-hand.&lt;/p&gt;

&lt;h4&gt;
  
  
  NATS Client Library Native Support for Requests
&lt;/h4&gt;

&lt;p&gt;You may have noticed we made a call to &lt;code&gt;request()&lt;/code&gt; in the customerApp above, and wondered why (perhaps you were expecting us to call something like &lt;code&gt;publish()&lt;/code&gt;). Nice catch! Here's a quick geeky aside on that topic that also provides a little insight into the Nest transporter abstraction layer.&lt;/p&gt;

&lt;p&gt;If you dig into the NATS client API libraries (for example, the &lt;a href="https://github.com/nats-io/nats.ts" rel="noopener noreferrer"&gt;TypeScript client&lt;/a&gt;), you'll notice that they provide API calls to both the NATS &lt;code&gt;PUB&lt;/code&gt; verb (usually &lt;code&gt;publish()&lt;/code&gt;) and &lt;code&gt;SUB&lt;/code&gt; verb (usually &lt;code&gt;subscribe()&lt;/code&gt;), but also offer a &lt;code&gt;request()&lt;/code&gt; method that doesn't have a direct counterpart in the NATS protocol (see an example &lt;a href="https://github.com/nats-io/nats.ts#basic-usage" rel="noopener noreferrer"&gt;here&lt;/a&gt;). By now, you should probably be able to guess what &lt;code&gt;request()&lt;/code&gt; does. It uses essentially the same pattern we described in the previous article  —  implemented by Nest to provide request/response semantics on top of publish/subscribe messaging  —  &lt;strong&gt;within the NATS TypeScript client library itself&lt;/strong&gt;. To be clear: this is a convenience provided by the client library (not a native feature of NATS).&lt;/p&gt;

&lt;p&gt;In the case of the NATS transporter, Nest takes advantage of this client library feature directly, rather than emulating it. For other transporters, where no such "request/response abstraction" exists, Nest emulates this functionality. In the end, both Nest and the client libraries share a similar need to add request/response behavior on top of a publish/subscribe model.&lt;/p&gt;

&lt;h3&gt;
  
  
  Clash of the Message Formats
&lt;/h3&gt;

&lt;h4&gt;
  
  
  Different Message Formats
&lt;/h4&gt;

&lt;p&gt;One thing we can do, now that we have an all-Nest version and a native TypeScript/NATS version of the requestor and responder, is examine the actual NATS messages exchanged. You can and should do this yourself (for now, you can examine the NATS logs produced by following &lt;a href="https://github.com/johnbiundo/nest-nats-sample#running-the-all-nest-configuration" rel="noopener noreferrer"&gt;these instructions&lt;/a&gt; and &lt;a href="https://github.com/johnbiundo/nest-nats-sample#running-the-all-native-app-configuration" rel="noopener noreferrer"&gt;these instructions&lt;/a&gt;), but let's cut right to the chase. Nest encodes message payloads in a format that is probably &lt;strong&gt;not directly compatible with the format used by your external app&lt;/strong&gt;. We'll address why that is, and how to resolve the incompatibility, below. Now we're getting to the meaty part of the article series!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: the following representations of the actual messages as seen in the NATS server log take a few &lt;strong&gt;small&lt;/strong&gt; liberties to simplify and clarify. What you'll actually see in the logs is slightly more verbose. To see the NATS log messages yourself, follow &lt;a href="https://github.com/johnbiundo/nest-nats-sample#running-the-all-nest-configuration" rel="noopener noreferrer"&gt;these instructions&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Native App Messages
&lt;/h4&gt;

&lt;p&gt;The native &lt;em&gt;customerApp&lt;/em&gt;, when it issues the &lt;code&gt;'get-customers'&lt;/code&gt; request, emits a message that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;PUB get-customers _INBOX.6EADK
MSG_PAYLOAD: &lt;span class="o"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, you see the message topic (&lt;code&gt;get-customers&lt;/code&gt;) and the reply topic (&lt;code&gt;_INBOX.6EADK&lt;/code&gt;) on the first line (they're part of the message "header", not the payload), and an empty message payload on the second line.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;customerApp&lt;/em&gt; receives a response back from &lt;em&gt;customerService&lt;/em&gt; (because it included a response topic in the request - &lt;code&gt;_INBOX.6EADK&lt;/code&gt; - that it subscribed to) that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;MSG_PAYLOAD: &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"customers"&lt;/span&gt;: &lt;span class="o"&gt;[{&lt;/span&gt; &lt;span class="s2"&gt;"id"&lt;/span&gt;: 1, &lt;span class="s2"&gt;"name"&lt;/span&gt;: &lt;span class="s2"&gt;"nestjs.com"&lt;/span&gt; &lt;span class="o"&gt;}]}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, payload is just a "stringified" version of our JSON response.&lt;/p&gt;

&lt;h4&gt;
  
  
  Nest App Messages
&lt;/h4&gt;

&lt;p&gt;On the other hand, the Nest components produce slightly different messages. The &lt;em&gt;nestHttpApp&lt;/em&gt;, when it runs &lt;code&gt;client.send('get-customers', {})&lt;/code&gt;, emits a message that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;PUB get-customers _INBOX.9FEAM
MSG_PAYLOAD: &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"pattern"&lt;/span&gt;: &lt;span class="s2"&gt;"get-customers"&lt;/span&gt;,&lt;span class="s2"&gt;"data"&lt;/span&gt;: &lt;span class="o"&gt;{}&lt;/span&gt;,
&lt;span class="s2"&gt;"id"&lt;/span&gt;: &lt;span class="s2"&gt;"84d9259e-fd00-4456-83b8-408311ca72cc"&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It receives a response back from &lt;em&gt;nestMicroservice&lt;/em&gt; that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;MSG_PAYLOAD: &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"err"&lt;/span&gt;: null,&lt;span class="s2"&gt;"response"&lt;/span&gt;: &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="s2"&gt;"customers"&lt;/span&gt;: &lt;span class="o"&gt;[{&lt;/span&gt; &lt;span class="s2"&gt;"id"&lt;/span&gt;: 1, &lt;span class="s2"&gt;"name"&lt;/span&gt;:&lt;span class="s2"&gt;"nestjs.com"&lt;/span&gt; &lt;span class="o"&gt;}]}&lt;/span&gt;,
&lt;span class="s2"&gt;"isDisposed"&lt;/span&gt;: &lt;span class="nb"&gt;true&lt;/span&gt;,&lt;span class="s2"&gt;"id"&lt;/span&gt;: &lt;span class="s2"&gt;"84d9259e-fd00-4456-83b8-408311ca72cc"&lt;/span&gt;&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The differences should be clear. Nest wraps your message payloads inside a JSON object. For requests, your payload is wrapped in a &lt;code&gt;data&lt;/code&gt; property. For responses, your payload is wrapped in a &lt;code&gt;response&lt;/code&gt; property.&lt;/p&gt;

&lt;p&gt;Why the differences? Consider that Nest must properly route and manage the lifetime of messages &lt;strong&gt;within and between Nest apps&lt;/strong&gt; (e.g., our "Pure NestJS" &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3#figure1"&gt;Case A in Figure 1&lt;/a&gt;). Nest needs to pass some metadata, along with the actual application-specific message content, with each message. Nest encodes this metadata in the &lt;strong&gt;payload&lt;/strong&gt; itself (because NATS doesn't allow you to add fields anywhere else in a message), resulting in the extra fields we see.&lt;/p&gt;

&lt;p&gt;With this in mind, we can layout the standard format for all Nest messages, thus defining Nest's transporter message protocol.&lt;/p&gt;

&lt;h4&gt;
  
  
  Nest Transporter Message Protocol
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Request messages (coming from &lt;strong&gt;Nest requestors&lt;/strong&gt;) are wrapped in a structure that we can depict as follows:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;   &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="cm"&gt;/**
      *  The message topic, also known inside Nest as
      *  the "message pattern"
      */&lt;/span&gt;
     &lt;span class="nl"&gt;pattern&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="cm"&gt;/**
      *  The message payload (the first argument of
      *  ClientProxy#send or ClientProxy#emit)
      */&lt;/span&gt;
     &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="cm"&gt;/**
      *  A unique identifier, assigned by Nest.  Present
      *  only when the message is a request (created with
      *  ClientProxy#send)
      */&lt;/span&gt;
     &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Response messages (coming from &lt;strong&gt;Nest responders&lt;/strong&gt;) are wrapped in a structure we can depict as follows:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;   &lt;span class="p"&gt;{&lt;/span&gt;
     &lt;span class="cm"&gt;/**
      *  Request error message, if any
      */&lt;/span&gt;
     &lt;span class="nl"&gt;err&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
     &lt;span class="cm"&gt;/**
      *  The response message payload (return value of a
      *  message pattern handler)
      */&lt;/span&gt;
     &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;
     &lt;span class="cm"&gt;/**
      *  Status of an observable response. False once the final
      *  stream value has been retrieved.
      */&lt;/span&gt;
     &lt;span class="nx"&gt;isDisposed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;
     &lt;span class="cm"&gt;/**
      *  Unique identifier, corresponding to the id field received
      *  from the initial request
      */&lt;/span&gt;
     &lt;span class="nx"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Clearly, we'll have a problem communicating between our Nest and non-Nest apps based on these different message formats. For example, in the &lt;strong&gt;Nest as responder&lt;/strong&gt; case, we have the following issue, where an external request is not understood by the Nest responder due to the message format incompatibility.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fmicro-nats-2%2Fassets%2Fmismatch.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fmicro-nats-2%2Fassets%2Fmismatch.png" title="Message Format Mismatch" alt="message format mismatch"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a&gt;&lt;/a&gt;Figure 4: Message Format Mismatch&lt;/p&gt;

&lt;p&gt;As you can imagine, we have the reverse issue in the &lt;strong&gt;Nest as requestor&lt;/strong&gt; case, where Nest issues requests wrapped in the Nest request format, which aren't understood by the external app, and the external app also responds with an incompatible message format (missing fields expected by Nest).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Now the big question&lt;/strong&gt;: How do we reconcile these message format differences to connect Nest and external NATS apps, as in &lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3#figure1"&gt;Figure 1 Cases B, C and D&lt;/a&gt;?&lt;/p&gt;

&lt;p&gt;The good news is that &lt;strong&gt;Nest anticipates this need and provides a neat solution.&lt;/strong&gt; We now have all the pieces in place to start seeing how Nest solves this problem and how to craft a solution. We'll dive into this in Part 3!&lt;/p&gt;

&lt;p&gt;Feel free to ask questions, make comments or suggestions, or just say hello in the comments below. And join us at &lt;a href="https://discord.gg/nestjs" rel="noopener noreferrer"&gt;Discord&lt;/a&gt; for more happy discussions about NestJS. I post there as &lt;em&gt;Y Prospect&lt;/em&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Part 1: Introduction to Microservices and Transporters</title>
      <dc:creator>John Biundo</dc:creator>
      <pubDate>Mon, 10 Feb 2020 15:24:31 +0000</pubDate>
      <link>https://forem.com/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3</link>
      <guid>https://forem.com/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-1-p3</guid>
      <description>&lt;p&gt;&lt;em&gt;John is a member of the NestJS core team&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Introduction
&lt;/h3&gt;

&lt;p&gt;This article series covers the topic of integrating NestJS applications with other applications and services.  This can be accomplished in several different ways; the approach covered in this series is to use a combination of &lt;a href="https://docs.nestjs.com/microservices/basics" rel="noopener noreferrer"&gt;Nest microservices&lt;/a&gt; and a message broker as the &lt;em&gt;glue&lt;/em&gt;.  In other words, Nest apps and external apps communicate with each other asynchronously through a message broker.&lt;/p&gt;

&lt;p&gt;In these examples, we use &lt;a href="https://docs.nats.io/" rel="noopener noreferrer"&gt;NATS&lt;/a&gt; as the message broker. &lt;strong&gt;Note&lt;/strong&gt;: some of the concepts here are common to all Nest microservices transporters, while others are specific to NATS.  I try to identify those differences in the text.&lt;/p&gt;

&lt;h3&gt;
  
  
  Source Code
&lt;/h3&gt;

&lt;p&gt;Working examples and all source code is available &lt;a href="https://github.com/johnbiundo/nest-nats-sample" rel="noopener noreferrer"&gt;here&lt;/a&gt;. The articles work best if you follow along with the repository.  Full installation instructions, along with many other details, are available &lt;a href="https://github.com/johnbiundo/nest-nats-sample#sample-repository-for-nestnatsmicroservice-article-series" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Nest Microservices Background
&lt;/h3&gt;

&lt;p&gt;Nest's microservices package is one of those technologies that can be a little hard to wrap your mind around.  Not that it's overly complex — to the contrary, it presents an application programming model that is quite simple. I think the confusion comes from the use of the term "microservices", which has become so over-used as to be virtually meaningless, and from the fact that Nest's microservices package supports several considerably different use cases:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Used internally as a &lt;strong&gt;transport layer&lt;/strong&gt;, there is little beyond the surface level that a developer needs to understand to effectively use it.  It simply "works", and provides a mechanism for adding features such as scalability and fault tolerance to your Nest application(s).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Used as an &lt;strong&gt;integration technology&lt;/strong&gt;, one needs to dig a bit deeper.  Because the microservices package plays this dual role, when using it for integration there are important implementation details that surface and can get in the way.  Fortunately, as is usually the case, the elegance of the Nest architecture provides mechanisms that make this use case possible (if not immediately obvious). This topic is the focus of this article series.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Article Series Overview
&lt;/h3&gt;

&lt;p&gt;This article series covers the &lt;strong&gt;integration use case&lt;/strong&gt;.  To get there, it first covers some Nest microservices basics.  Some of these basics might be of interest to you even if you are using the package for the first use case described above (inter-Nest-app communication layer).  The articles are organized so you can skip the basics if you want, or if you want to come back later for reference, you can just read the parts you need.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Part 1&lt;/strong&gt; (this article!) covers introductory material, including the basic microservices communication model, brokers, and how Nest uses publish/subscribe and request/response communication models. It introduces external integration use-cases that will be covered in the series. It also lays out a vocabulary for describing all the moving parts in these sometimes-complex interactions.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-2-3hgd"&gt;&lt;strong&gt;Part 2&lt;/strong&gt;&lt;/a&gt; digs a little deeper, extending the vocabulary to describe the modalities in which Nest operates when interacting with external systems. It introduces a pair of simple external (non-Nest, NATS-based) apps that we will use as part of the integration case study.  It introduces the potential challenges arising from Nest's use of a proprietary &lt;em&gt;message format&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://dev.to/nestjs/integrate-nestjs-with-external-services-using-microservice-transporters-part-3-4m20"&gt;&lt;strong&gt;Part 3&lt;/strong&gt;&lt;/a&gt; describes the Nest approach for solving the message format challenge. It goes on to work through code samples that implement this approach.  It uses the vocabulary introduced earlier to help you keep a simple cognitive model in mind as you address this problem.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Part 4&lt;/strong&gt; (coming soon) covers some leftovers, some advanced use cases, and some tips and tricks.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope you find this article series helpful!&lt;/p&gt;

&lt;h3&gt;
  
  
  Part 1: Introduction to Nest Transporters
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://github.com/nestjs/nest" rel="noopener noreferrer"&gt;NestJS&lt;/a&gt; comes with a built-in versatile network communications-oriented subsystem called (perhaps somewhat confusingly) &lt;a href="https://docs.nestjs.com/microservices/basics" rel="noopener noreferrer"&gt;microservices&lt;/a&gt;. The core of Nest microservices, and the key to understanding them, is what Nest calls &lt;strong&gt;transporters&lt;/strong&gt;. Transporters enable you to connect components over a network using a pluggable communications layer and a very simple application-level &lt;a href="https://docs.nestjs.com/microservices/basics#request-response" rel="noopener noreferrer"&gt;message protocol&lt;/a&gt;. Nest delivers a variety of transporters out-of-the-box, as well as an API allowing developers to build new custom transporters. This combination of architecture and features is extremely powerful.&lt;/p&gt;

&lt;p&gt;A concrete example of a built-in Nest transporter is &lt;a href="https://docs.nats.io/" rel="noopener noreferrer"&gt;NATS&lt;/a&gt;. With the NATS-flavored transporter plugged in, Nest applications can communicate using the NATS messaging system. Since NATS is the intermediary for these communications, Nest can communicate not only with other Nest apps, but also with non-Nest apps, or a combination of both. This leads to four interesting use cases &lt;a&gt;&lt;/a&gt;(Figure 1 below).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fmicro-nats-1%2Fassets%2Ftransporter-use-cases.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fmicro-nats-1%2Fassets%2Ftransporter-use-cases.png" title="Transporter Use Cases" alt="Transporter Use Cases"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a&gt;&lt;/a&gt;Figure 1: Transporter Use Cases&lt;/p&gt;

&lt;p&gt;The current &lt;a href="https://docs.nestjs.com/microservices/basics" rel="noopener noreferrer"&gt;Nest microservices&lt;/a&gt; documentation focuses almost entirely on the first use case (Case A). And rightly so. If you simply want to benefit from a robust communication layer (e.g., easily adding load balancing and fault tolerance to increase the horizontal scalability of your Nest application), you really don't need to know about 90% of the material in this article series! (You may still benefit from a deeper understanding of the Nest microservices architecture, however, so I invite you to read on. Much of the material is still relevant background for leveraging Nest microservices).&lt;/p&gt;

&lt;p&gt;The rest of this article series focuses mainly on the last three use cases — namely, using Nest microservices running on NATS to integrate with external systems.&lt;/p&gt;

&lt;p&gt;To get there we need to lay some conceptual groundwork. Let's get started.&lt;/p&gt;

&lt;h3&gt;
  
  
  Concepts
&lt;/h3&gt;

&lt;p&gt;There's a lot going on in that diagram, even viewed at the highest level. As soon as we begin diving into the details, the terminology soup can easily get in the way of deeper understanding. So without further ado, let's take a crack at some basic concepts and terminology.&lt;/p&gt;

&lt;p&gt;First, let's acknowledge that there are two main flavors of Nest transporters, which I'll refer to as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;broker-based&lt;/strong&gt;: this includes NATS, as well others like &lt;a href="https://docs.nestjs.com/microservices/redis" rel="noopener noreferrer"&gt;&lt;strong&gt;Redis&lt;/strong&gt;&lt;/a&gt;, &lt;a href="https://docs.nestjs.com/microservices/rabbitmq" rel="noopener noreferrer"&gt;&lt;strong&gt;RabbitMQ&lt;/strong&gt;&lt;/a&gt;, &lt;a href="https://docs.nestjs.com/microservices/mqtt" rel="noopener noreferrer"&gt;&lt;strong&gt;MQTT&lt;/strong&gt;&lt;/a&gt;, and &lt;a href="https://docs.nestjs.com/microservices/kafka" rel="noopener noreferrer"&gt;&lt;strong&gt;Kafka&lt;/strong&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;point-to-point&lt;/strong&gt;: this includes &lt;a href="https://docs.nestjs.com/microservices/basics" rel="noopener noreferrer"&gt;&lt;strong&gt;TCP&lt;/strong&gt;&lt;/a&gt; and &lt;a href="https://docs.nestjs.com/microservices/grpc" rel="noopener noreferrer"&gt;&lt;strong&gt;gRPC&lt;/strong&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This article focuses exclusively on the broker-based transporters, so let's discuss that flavor a bit further, using NATS as our case study example. We start with a description of a &lt;strong&gt;broker&lt;/strong&gt; in general terms. An installed broker is comprised of several parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the broker &lt;strong&gt;server&lt;/strong&gt;: this is an active server process (or possibly replicated servers) that manages publishing and subscribing (and the bookkeeping that goes with it), and delivers messages to clients.&lt;/li&gt;
&lt;li&gt;the &lt;strong&gt;broker client API&lt;/strong&gt;: this is delivered in a language-specific package (e.g., a JavaScript flavor, a Java flavor, a Go flavor, etc.) providing an API for accessing the broker from client programs. Client programs include both "native NATS applications" you write with the API (and without benefit of a framework), as well as frameworks like NestJS which use the client API to communicate to the broker.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Message flow diagrams typically only label the broker component (in our case, for example, as a green circle in the middle of various other components), but think of the client API as embedded in the other boxes, connecting them to the broker.&lt;/p&gt;

&lt;p&gt;The key advantage of broker-based messaging systems is they allow you to decouple the various application components from each other. Each need only connect to the broker, and can remain unaware of the existence of, location of, or implementation details of other components. The only thing the components need to share is a message protocol.&lt;/p&gt;

&lt;p&gt;Nest makes use of a small set of features that are typically available across most brokers. This is a bit of an over-generalization — one we'll revisit later on — but for now, let's examine the common broker features that Nest relies on.&lt;/p&gt;

&lt;h3&gt;
  
  
  Broker Message Protocol
&lt;/h3&gt;

&lt;p&gt;From Nest's perspective, brokers provide a basic message-oriented communication protocol that is usually described as &lt;strong&gt;publish/subscribe&lt;/strong&gt;. Publish/subscribe can be generally understood in terms of the following diagram, with the red circles indicating the order of events in a publish/subscribe "conversation".&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fmicro-nats-1%2Fassets%2Fbroker-message-protocol2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fraw.githubusercontent.com%2Fjohnbiundo%2Fdev.to-articles%2Fmaster%2Fblog-posts%2Fmicro-nats-1%2Fassets%2Fbroker-message-protocol2.png" title="Broker Message Protocol" alt="Broker Message Protocol"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;a&gt;&lt;/a&gt;Figure 2: Broker Message Protocol



&lt;p&gt;In that diagram, each client component is either a &lt;em&gt;publisher&lt;/em&gt; or a &lt;em&gt;subscriber&lt;/em&gt;. The salient point is that subscribers "register interest in a &lt;strong&gt;topic&lt;/strong&gt;" and publishers publish messages about a topic. The broker sits in the middle and performs the following functions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;keeps track of subscribers (by managing a list of &lt;strong&gt;subscriptions&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;receives published messages&lt;/li&gt;
&lt;li&gt;forwards published messages to all interested subscribers (based on matching topics between published messages and subscriptions)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One thing obviously missing from this diagram is the &lt;a href="https://en.wikipedia.org/wiki/Request%E2%80%93response" rel="noopener noreferrer"&gt;request/response&lt;/a&gt; style communication model. This communication style is useful when we want to verify receipt of a published message and/or receive a response from whoever consumes the message. In fact, this is the style of message implicit in Figure 1 above (where we can infer that &lt;code&gt;'get-customers'&lt;/code&gt; is a &lt;strong&gt;request&lt;/strong&gt; and the array of customers is a &lt;strong&gt;response&lt;/strong&gt;). How do we get from &lt;strong&gt;publish/subscribe&lt;/strong&gt; to &lt;strong&gt;request/response&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;Nest (as well as some of the brokers natively, including NATS as we'll soon see) solve this problem in a handy way, by building a request/response capability &lt;strong&gt;on top of the publish/subscribe model&lt;/strong&gt;. Let's say component A wishes to "get customers" from component B, which has access to a customer DB. Component A can publish a "get customers" message, and (assuming it has subscribed to that topic) component B receives it, queries the customer DB for a list of customers, and sends a response message. The response message is where the magic happens. In order for B to respond to A, they both must do a few things, agreed upon by convention:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A chooses a &lt;strong&gt;response topic&lt;/strong&gt; (sometimes called a &lt;strong&gt;reply subject&lt;/strong&gt;)&lt;/li&gt;
&lt;li&gt;A subscribes to the response topic&lt;/li&gt;
&lt;li&gt;A passes the response topic as part of the initial message&lt;/li&gt;
&lt;li&gt;B uses the response topic as the topic of its own subsequent response message&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In other words, if a response topic is included in a message received by a subscriber, the subscriber publishes a response message on that response topic. Thus, in our example, B publishes its response, including a payload containing the customer list, on the response topic it received in the "request" message. Because A previously subscribed to the response topic, it receives the response as a normal message. All very tidy!&lt;/p&gt;

&lt;h3&gt;
  
  
  A Vocabulary for Transporter Use Cases
&lt;/h3&gt;

&lt;p&gt;With this in mind, we can give names to the two different types of messages our system handles, and also describe the &lt;strong&gt;roles&lt;/strong&gt; of the participants in those messages. Don't worry, we're almost done with the preliminaries! These terms will all become second nature pretty quickly, but it turns out to be extremely useful to have a vocabulary that will let us quickly navigate a bunch of similar-looking data flow diagrams, and keep track of where our custom code fits in!&lt;/p&gt;

&lt;p&gt;The first kind of message we can exchange based on the above capabilities is called an &lt;strong&gt;event&lt;/strong&gt;. Any given component participating in event-based messaging can be either:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;an event &lt;strong&gt;emitter&lt;/strong&gt; - meaning it &lt;em&gt;publishes&lt;/em&gt; a message with a topic (and an optional payload). An event emitter is a pure message publisher.&lt;/li&gt;
&lt;li&gt;an event &lt;strong&gt;subscriber&lt;/strong&gt; - meaning it registers interest in a topic, and receives messages (forwarded by the broker) when a message matching that topic is published by an emitter.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The second kind of message we can exchange is called a &lt;strong&gt;request/response&lt;/strong&gt; message. In this exchange, a participating component can be either:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a &lt;strong&gt;requestor&lt;/strong&gt; - meaning it &lt;em&gt;publishes&lt;/em&gt; a message it intends to be treated as a request, and it also takes the conventional steps described above — namely, &lt;em&gt;subscribing&lt;/em&gt; to a response topic and including that response topic in the message it publishes.&lt;/li&gt;
&lt;li&gt;a &lt;strong&gt;responder&lt;/strong&gt; - meaning it &lt;em&gt;subscribes&lt;/em&gt; to a topic it intends to treat as an incoming request, it produces a result, and it &lt;em&gt;publishes&lt;/em&gt; a response, including the result payload, to the response topic it received on the inbound request.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So requestors are a special case of publishers, and we use this term to remember that they &lt;strong&gt;also&lt;/strong&gt; await a response. And responders are a special case of subscribers, and we use this term to remember that they are &lt;strong&gt;also&lt;/strong&gt; publishers of a response. Make sense? If not, don't worry, it will all come together soon.&lt;/p&gt;

&lt;h3&gt;
  
  
  Integration Use Cases
&lt;/h3&gt;

&lt;p&gt;Most of the above is background that "just works" in an all-Nest world.  Let's turn our attention now to integrating Nest apps with non-Nest apps using NATS as the communication intermediary.&lt;/p&gt;

&lt;p&gt;In Figure 1, cases B, C, and D illustrate the different topologies for Nest-based apps to integrate with non-Nest apps using NATS. Note, we'll use the generic term &lt;strong&gt;"app"&lt;/strong&gt; to describe these communicating components. As we'll see, these "apps" could be web apps, app services written in NestJS or some other platform, "microservices", etc., but the rules we'll describe are common regardless of the type of app involved.  Let's briefly motivate the use cases.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;In case B, we could be migrating a legacy app service, responsible for managing our customer DB, to Nest. In this case, our Nest app needs to play the role of &lt;strong&gt;responder&lt;/strong&gt; that we described above. Going forward, we call this the &lt;strong&gt;Nest as responder&lt;/strong&gt; use case.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;In case C, we could be writing a new app, in Nest, that needs to query our customer DB using the existing (legacy) service. In this case, our Nest app needs to play the role of &lt;strong&gt;requestor&lt;/strong&gt; that we described above. Going forward, we call this the &lt;strong&gt;Nest as requestor&lt;/strong&gt; use case.&lt;/p&gt;

&lt;p&gt;In case D, we could be somewhere in the middle of a complex migration process, where both types of communications are happening. Nest apps need to be able to speak to both other Nest apps and non-Nest apps, all via NATS. Non-nest apps similarly need to be able to talk to both types of apps. Going forward, we call this the &lt;strong&gt;Nest as duplex requestor/responder&lt;/strong&gt; use case.&lt;/p&gt;

&lt;p&gt;We'll examine each use case in more detail, including sample code you need to make this work (reminder &lt;a href="https://github.com/johnbiundo/nest-nats-sample" rel="noopener noreferrer"&gt;full repository with usage notes here&lt;/a&gt;). Before we can do that, we need to understand a little more about how Nest components interact with the broker. We cover this in the next installment in the series.&lt;/p&gt;

&lt;p&gt;Feel free to ask questions, make comments or suggestions, or just say hello in the comments below. And join us at &lt;a href="https://discord.gg/nestjs" rel="noopener noreferrer"&gt;Discord&lt;/a&gt; for more happy discussions about NestJS. I post there as &lt;em&gt;Y Prospect&lt;/em&gt;.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Build a NestJS Module for Knex.js (or other resource-based libraries) in 5 Minutes</title>
      <dc:creator>John Biundo</dc:creator>
      <pubDate>Mon, 23 Sep 2019 14:45:55 +0000</pubDate>
      <link>https://forem.com/nestjs/build-a-nestjs-module-for-knex-js-or-other-resource-based-libraries-in-5-minutes-12an</link>
      <guid>https://forem.com/nestjs/build-a-nestjs-module-for-knex-js-or-other-resource-based-libraries-in-5-minutes-12an</guid>
      <description>&lt;p&gt;&lt;em&gt;John is a member of the NestJS core team&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Ever wanted to integrate your favorite library into NestJS? For example, while Nest has broad built-in support for database integration, what if you want to use your own favorite library and no Nest package exists? Well, why not build your own?&lt;/p&gt;

&lt;p&gt;This might seem like a daunting task at first. But if you've been &lt;a href="https://dev.to/johnbiundo"&gt;following my blog posts&lt;/a&gt;, you saw a &lt;em&gt;design pattern&lt;/em&gt; for NestJS dynamic modules in my &lt;a href="https://dev.to/nestjs/advanced-nestjs-how-to-build-completely-dynamic-nestjs-modules-1370"&gt;last post&lt;/a&gt; that points you in the right direction. OK, you say, but it &lt;em&gt;still&lt;/em&gt; seems like a lot of work to integrate a library.&lt;/p&gt;

&lt;p&gt;I have good news! Using the awesome power of the Nest CLI, you can &lt;strong&gt;generate a complete, custom dynamic module template, following NestJS best practices, with a single command!&lt;/strong&gt; You can then literally have your library integrated in about 5 minutes! This technique works for many &lt;em&gt;resource-based&lt;/em&gt; libraries that export what I'll call an &lt;em&gt;API object&lt;/em&gt;, which naturally work well with Nest's built-in &lt;em&gt;singleton provider&lt;/em&gt; model for sharing a global resource. Follow along with such an adventure as we build a quick integration to &lt;a href="http://knexjs.org/"&gt;Knex.js&lt;/a&gt; below.&lt;/p&gt;

&lt;p&gt;And stay tuned for more on the wonders of the vastly under-appreciated NestJS CLI. Built on top of the Angular CLI, the potential for this tool to revolutionize the way you use Nest is limitless. I have lots more planned for this topic in the near future!&lt;/p&gt;

&lt;h3&gt;
  
  
  Intro
&lt;/h3&gt;

&lt;p&gt;In my &lt;a href="https://dev.to/nestjs/advanced-nestjs-how-to-build-completely-dynamic-nestjs-modules-1370"&gt;How to build completely dynamic NestJS modules&lt;/a&gt; article last week, I covered a design pattern that is used throughout standard Nest packages like &lt;code&gt;@nestjs/passport&lt;/code&gt;, &lt;code&gt;@nestjs/jwt&lt;/code&gt;, and &lt;code&gt;@nestjs/typeorm&lt;/code&gt;. The point of the article was two-fold:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Provide a roadmap to a basic pattern used within common NestJS packages to help you be a better consumer of those packages.&lt;/li&gt;
&lt;li&gt;Encourage Devs to think about how to make use of this pattern in their own code to enable more easily composable modules.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Some might consider the pattern a bit complex, and I wouldn't necessarily argue. However, gaining a good understanding of it is useful, and helps in the quest for mastery of some core concepts of Nest, especially around leveraging the &lt;em&gt;module system&lt;/em&gt; and &lt;em&gt;dependency injection&lt;/em&gt;. Nevertheless, there's a fair amount of boilerplate code in the pattern. Wouldn't it be great to simplify the process?&lt;/p&gt;

&lt;p&gt;Buckle up! NestJS CLI custom schematics to the rescue! 🚀&lt;/p&gt;

&lt;h3&gt;
  
  
  What We'll Build
&lt;/h3&gt;

&lt;p&gt;In this tutorial, we'll build a module that exports a direct API to the full &lt;a href="http://knexjs.org/"&gt;Knex.js&lt;/a&gt; library. This is a powerful DB integration library used widely across the Node.js ecosystem. Here's what we'll do. The following represent the &lt;em&gt;exact same steps&lt;/em&gt; you can use to integrate any other basic callable API (for example, &lt;a href="https://github.com/luin/ioredis"&gt;ioredis&lt;/a&gt;, &lt;a href="https://github.com/datastax/nodejs-driver#readme"&gt;Cassandra&lt;/a&gt;, &lt;a href="https://github.com/neo4j/neo4j-javascript-driver#readme"&gt;Neo4J&lt;/a&gt;, &lt;a href="https://www.elastic.co/blog/new-elasticsearch-javascript-client-released"&gt;Elasticsearch&lt;/a&gt;, &lt;a href="https://github.com/Level/levelup"&gt;LevelDb&lt;/a&gt; to name just a few).&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use the &lt;a href="https://github.com/nestjsplus/dyn-schematics#use-case-1-generating-a-standalone-package"&gt;dynpkg&lt;/a&gt; &lt;em&gt;custom schematic&lt;/em&gt; to generate a customized package (the schematic automates the &lt;a href="https://dev.to/nestjs/advanced-nestjs-how-to-build-completely-dynamic-nestjs-modules-1370"&gt;dynamic module pattern&lt;/a&gt; I've been discussing above).&lt;/li&gt;
&lt;li&gt;Fill in a few quick details to customize the package for Knex.js.&lt;/li&gt;
&lt;li&gt;Use the auto-generated test client to demonstrate that it works.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Thanks to the power of the Nest CLI, coupled with a &lt;em&gt;custom schematic&lt;/em&gt;, we can complete these steps in about 5 minutes! As a bonus, we'll cover a few more advanced features at the end of this article, including how to publish the generated package on npm with a single click.&lt;/p&gt;

&lt;p&gt;If you want to see a completed version of the steps presented in the remainder of this article (a &lt;em&gt;fully useable NestJS/Knex.js module&lt;/em&gt;), you can find a &lt;a href="https://github.com/nestjsplus/knex"&gt;github repo with the completed code here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Nest CLI and Schematics
&lt;/h3&gt;

&lt;p&gt;You probably already use the Nest CLI on a regular basis. &lt;code&gt;nest new myProject&lt;/code&gt; and &lt;code&gt;nest generate controller user&lt;/code&gt; are common examples. They use the &lt;code&gt;@nestjs/schematics&lt;/code&gt; package that ships with the CLI out-of-the-box. What's very cool is that you can easily use other schematics to do highly customized work. A feature common to all schematics is that they &lt;em&gt;understand&lt;/em&gt; your project, and have a smart way of scaffolding new architectural components and wiring them into your project. Think of an individual schematic as a blueprint, and of the CLI machinery as a &lt;em&gt;set of standards&lt;/em&gt; for how each blueprint works. Because of this, new schematics "just work", and inherit all of the features provided by the CLI and its standard capabilities.&lt;/p&gt;

&lt;p&gt;You can write your own schematics, and I plan to show you how in some upcoming blog posts. For now, I've built one that you can use to do the library integration project we've undertaken today. Let's get started!&lt;/p&gt;

&lt;p&gt;To use any external schematics, you of course need to install them. One important note: you &lt;strong&gt;must&lt;/strong&gt; install a schematics collection as a global package, due to the way the CLI works.&lt;/p&gt;

&lt;h4&gt;
  
  
  Installing a Custom Schematics Collection
&lt;/h4&gt;

&lt;p&gt;Schematics are packaged as &lt;em&gt;collections&lt;/em&gt; and can be bundled up as npm packages, so installing them is simple. You can do so now with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt; @nestjsplus/dyn-schematics &lt;span class="nt"&gt;-g&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Using a Custom Schematics Collection
&lt;/h4&gt;

&lt;p&gt;Using schematics from a custom collection is straightforward. The standard Nest CLI command structure looks like this:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;nest &lt;em&gt;commandOrAlias&lt;/em&gt; [-c schematicCollection] &lt;em&gt;requiredArg&lt;/em&gt; [options]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;&lt;code&gt;commandOrAlias&lt;/code&gt;&lt;/em&gt; is: &lt;code&gt;new&lt;/code&gt; or &lt;code&gt;generate&lt;/code&gt; (alias: &lt;code&gt;g&lt;/code&gt;) or &lt;code&gt;add&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;schematicCollection&lt;/code&gt; is optional, and defaults to the built-in NestJS schematics; you can optionally specify a globally installed npm package&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;&lt;code&gt;requiredArg&lt;/code&gt;&lt;/em&gt; is the architectural element being generated/added, e.g., &lt;code&gt;controller&lt;/code&gt;, &lt;code&gt;module&lt;/code&gt;, or soon, in our case &lt;code&gt;dynpkg&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;options&lt;/code&gt; are global to all schematics; they can be &lt;code&gt;--dry-run&lt;/code&gt;, &lt;code&gt;--no-spec&lt;/code&gt;, or &lt;code&gt;--flat&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once it's installed, you use the &lt;a href="https://github.com/nestjsplus/dyn-schematics"&gt;@nestjsplus/dyn-schematics&lt;/a&gt; package like this:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Make sure you're in the folder you want to have as the parent of the project. With this schematic, we're creating a &lt;em&gt;new&lt;/em&gt; complete nest package, meaning it's a new standalone project. So it will create a folder using the &lt;code&gt;name&lt;/code&gt; you provide, and put all of the component parts inside that folder.&lt;/li&gt;
&lt;li&gt;Run the schematic with the CLI
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nest g &lt;span class="nt"&gt;-c&lt;/span&gt; @nestjsplus/dyn-schematics dynpkg nest-knex
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This runs using the custom schematics collection from &lt;code&gt;@nestjsplus/dyn-schematics&lt;/code&gt; and specifying the &lt;code&gt;dynpkg&lt;/code&gt; schematic to execute (the schematic identifies the &lt;em&gt;thing&lt;/em&gt; to generate - in this case, a &lt;em&gt;dynamic module package&lt;/em&gt; identified as &lt;code&gt;dynpkg&lt;/code&gt;), and giving it a &lt;code&gt;name&lt;/code&gt; of &lt;code&gt;nest-knex&lt;/code&gt;. If you want to see what this would do &lt;strong&gt;without adding files&lt;/strong&gt; to your filesystem, just add &lt;code&gt;--dry-run&lt;/code&gt; at the end of the command.&lt;/p&gt;

&lt;p&gt;This should prompt you with the question &lt;code&gt;Generate a testing client?&lt;/code&gt;. Answer yes to this to make testing easier. You'll see the following output, and you'll then be able to open this project in your IDE from the &lt;code&gt;nest-knex&lt;/code&gt; folder.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;► nest g &lt;span class="nt"&gt;-c&lt;/span&gt; @nestjsplus/dyn-schematics dynpkg nest-knex
? Generate a testing client? Yes
CREATE /nest-knex/.gitignore &lt;span class="o"&gt;(&lt;/span&gt;375 bytes&lt;span class="o"&gt;)&lt;/span&gt;
CREATE /nest-knex/.prettierrc &lt;span class="o"&gt;(&lt;/span&gt;51 bytes&lt;span class="o"&gt;)&lt;/span&gt;
CREATE /nest-knex/README.md &lt;span class="o"&gt;(&lt;/span&gt;2073 bytes&lt;span class="o"&gt;)&lt;/span&gt;
CREATE /nest-knex/nest-cli.json &lt;span class="o"&gt;(&lt;/span&gt;84 bytes&lt;span class="o"&gt;)&lt;/span&gt;
CREATE /nest-knex/package.json &lt;span class="o"&gt;(&lt;/span&gt;1596 bytes&lt;span class="o"&gt;)&lt;/span&gt;
CREATE /nest-knex/tsconfig.build.json &lt;span class="o"&gt;(&lt;/span&gt;97 bytes&lt;span class="o"&gt;)&lt;/span&gt;
CREATE /nest-knex/tsconfig.json &lt;span class="o"&gt;(&lt;/span&gt;430 bytes&lt;span class="o"&gt;)&lt;/span&gt;
CREATE /nest-knex/tslint.json &lt;span class="o"&gt;(&lt;/span&gt;426 bytes&lt;span class="o"&gt;)&lt;/span&gt;
CREATE /nest-knex/src/constants.ts &lt;span class="o"&gt;(&lt;/span&gt;54 bytes&lt;span class="o"&gt;)&lt;/span&gt;
CREATE /nest-knex/src/index.ts &lt;span class="o"&gt;(&lt;/span&gt;103 bytes&lt;span class="o"&gt;)&lt;/span&gt;
CREATE /nest-knex/src/main.ts &lt;span class="o"&gt;(&lt;/span&gt;519 bytes&lt;span class="o"&gt;)&lt;/span&gt;
CREATE /nest-knex/src/nest-knex.module.ts &lt;span class="o"&gt;(&lt;/span&gt;1966 bytes&lt;span class="o"&gt;)&lt;/span&gt;
CREATE /nest-knex/src/nest-knex.providers.ts &lt;span class="o"&gt;(&lt;/span&gt;262 bytes&lt;span class="o"&gt;)&lt;/span&gt;
CREATE /nest-knex/src/nest-knex.service.ts &lt;span class="o"&gt;(&lt;/span&gt;1111 bytes&lt;span class="o"&gt;)&lt;/span&gt;
CREATE /nest-knex/src/interfaces/index.ts &lt;span class="o"&gt;(&lt;/span&gt;162 bytes&lt;span class="o"&gt;)&lt;/span&gt;
CREATE /nest-knex/src/interfaces/nest-knex-module-async-options.interface.ts &lt;span class="o"&gt;(&lt;/span&gt;532 bytes&lt;span class="o"&gt;)&lt;/span&gt;
CREATE /nest-knex/src/interfaces/nest-knex-options-factory.interface.ts &lt;span class="o"&gt;(&lt;/span&gt;194 bytes&lt;span class="o"&gt;)&lt;/span&gt;
CREATE /nest-knex/src/interfaces/nest-knex-options.interface.ts &lt;span class="o"&gt;(&lt;/span&gt;409 bytes&lt;span class="o"&gt;)&lt;/span&gt;
CREATE /nest-knex/src/nest-knex-client/nest-knex-client.controller.ts &lt;span class="o"&gt;(&lt;/span&gt;732 bytes&lt;span class="o"&gt;)&lt;/span&gt;
CREATE /nest-knex/src/nest-knex-client/nest-knex-client.module.ts &lt;span class="o"&gt;(&lt;/span&gt;728 bytes&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At this point, you can install the generated code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;nest-knex
npm &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now start up the app with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm run start:dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you now browse to &lt;a href="http://localhost:3000"&gt;http://localhost:3000&lt;/a&gt; you should get:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Hello from NestKnexModule!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Great! At this point, we've scaffolded the &lt;em&gt;framework&lt;/em&gt; of our dynamic module, and we're ready to begin the Knex.js integration.&lt;/p&gt;

&lt;h3&gt;
  
  
  Integrating with Knex.js
&lt;/h3&gt;

&lt;p&gt;You now have a project scaffolded to do the library integration. We have to make just a few edits to perform the actual integration. Let's talk them through:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We need to install &lt;code&gt;knex&lt;/code&gt; as a dependency. Note, in order to &lt;strong&gt;run&lt;/strong&gt; this project, you'll need access to a live SQL database. Knex.js doesn't bundle any database libraries, so you'll need to install the appropriate one (&lt;a href="http://knexjs.org/#Installation-node"&gt;you can read more here&lt;/a&gt;). In this tutorial, I'll use PostgreSql, which I have available on localhost. If you do not have a local DB available, you can consider &lt;a href="https://docs.docker.com/samples/library/postgres/"&gt;using a docker setup&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;We need a way to provide the configuration options to the Knex.js API.&lt;/li&gt;
&lt;li&gt;We need to call the Knex.js API in the way it wants to be initialized, returning a handle to a &lt;code&gt;knex&lt;/code&gt; object which we can use to access Knex.js features.&lt;/li&gt;
&lt;li&gt;To test things out, we can use the client controller that was auto-generated by the schematic.&lt;/li&gt;
&lt;li&gt;Once it works, we can use this package directly. Or we can publish the package to a registry (e.g., an internal package registry or publicly to npmjs.com)&lt;/li&gt;
&lt;/ol&gt;

&lt;h4&gt;
  
  
  Install Dependencies
&lt;/h4&gt;

&lt;p&gt;The &lt;code&gt;knex&lt;/code&gt; package is required. You will also need a database API library to &lt;strong&gt;run&lt;/strong&gt; the module. Below, I use &lt;code&gt;pg&lt;/code&gt; for PostgreSql, but you can choose whatever you want (from the &lt;a href="http://knexjs.org/#Installation-node"&gt;list here&lt;/a&gt;).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;nest-knex
npm &lt;span class="nb"&gt;install &lt;/span&gt;knex pg
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Knex.js Options
&lt;/h4&gt;

&lt;p&gt;As discussed in the &lt;a href="https://dev.to/nestjs/advanced-nestjs-how-to-build-completely-dynamic-nestjs-modules-1370#async-options-providers-usecase"&gt;dynamic modules article&lt;/a&gt;, we provide options to the service using an &lt;em&gt;async options provider&lt;/em&gt;. The code to do all this has been scaffolded for you, but to pass the appropriate options in a native TypeScript way, we need to modify the &lt;code&gt;NestKnexOptions&lt;/code&gt; interface. This is contained in the file &lt;code&gt;src/interfaces/nest-knex-options.interface.ts&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We want to describe the available Knex.js options that will be passed to the library API. The easiest way to do this is to use the types already &lt;a href="https://github.com/tgriesser/knex/blob/master/types/index.d.ts#L1590"&gt;provided by Knex.js&lt;/a&gt;. Since this interface is exported by the &lt;code&gt;knex&lt;/code&gt; package, we can simply import it, and our job is nearly done! We'll simply alias it to make it visible to our generated package. Open up &lt;code&gt;src/interfaces/nest-knex-options.interface.ts&lt;/code&gt; and edit it to look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/interfaces/nest-knex-options.interface.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Config&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;knex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;NestKnexOptions&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Config&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Knex Connections
&lt;/h4&gt;

&lt;p&gt;The job of our module is pretty simple: connect to the API and return a re-usable &lt;code&gt;knex&lt;/code&gt; object for interacting with the database. In the case of PostgreSql, &lt;code&gt;knex&lt;/code&gt; (on top of &lt;code&gt;pg&lt;/code&gt;) returns a &lt;em&gt;connection pool&lt;/em&gt;, which means we can connect once and return a &lt;em&gt;singleton&lt;/em&gt; &lt;code&gt;knex&lt;/code&gt; object that will automatically balance multiple queries across the connection pool. This works &lt;em&gt;seamlessly&lt;/em&gt; with NestJS and its notion of singleton providers!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: I'm less familiar with Knex than other DB libraries, and also less familiar with other DBs like MySQL, so if you are using this module for such databases, be sure you understand the right pattern for sharing a &lt;code&gt;knex&lt;/code&gt; connection object within your application. This topic is &lt;strong&gt;not&lt;/strong&gt; a NestJS issue -- it's all about the architecture of the Knex.js library and the database client library.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Now we'll implement a simple pattern to return our &lt;code&gt;knex&lt;/code&gt; object. Open up &lt;code&gt;src/nest-knex.service.ts&lt;/code&gt;. Have a quick look to see what the generated code looks like, then replace that code with the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/nest-knex.service.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Injectable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Inject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Logger&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;NEST_KNEX_OPTIONS&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./constants&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;NestKnexOptions&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./interfaces&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Knex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;knex&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;INestKnexService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;getKnex&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Injectable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;NestKnexService&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;INestKnexService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;_knexConnection&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Inject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;NEST_KNEX_OPTIONS&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;_NestKnexOptions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;NestKnexOptions&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;logger&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Logger&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;NestKnexService&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;logger&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Options: &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_NestKnexOptions&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nx"&gt;getKnex&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_knexConnection&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_knexConnection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;Knex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_NestKnexOptions&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_knexConnection&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Almost all of this is unchanged from the generated boilerplate. We simply added a property to store our connection object (&lt;code&gt;_knexConnection&lt;/code&gt;), and a &lt;code&gt;getKnex()&lt;/code&gt; method to instantiate the object the first time it's requested, and then cache it for future use.&lt;/p&gt;

&lt;h4&gt;
  
  
  Test the Module
&lt;/h4&gt;

&lt;p&gt;As mentioned, you'll need a local database instance available to test the module. If you don't have one handy, consider &lt;a href="https://docs.docker.com/samples/library/postgres/"&gt;using docker&lt;/a&gt; to do so. It's easy!&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;@nestjsplus/dyn-schematics&lt;/code&gt; &lt;code&gt;dynpkg&lt;/code&gt; schematic built a small test &lt;em&gt;client module&lt;/em&gt; for us (assuming you answered &lt;code&gt;yes&lt;/code&gt; to the prompt). We can use this to quickly test our &lt;code&gt;nest-knex&lt;/code&gt; module.&lt;/p&gt;

&lt;p&gt;First, open &lt;code&gt;src/nest-knex-client/nest-knex-client.module.ts&lt;/code&gt; and add the needed Knex.js options in the &lt;code&gt;register()&lt;/code&gt; method. Adjust yours appropriately:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/nest-knex-client/nest-knex-client.module.ts&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;controllers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;NestKnexClientController&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;NestKnexModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;register&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;pg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;localhost&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;john&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mypassword&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;database&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nest&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5432&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, open &lt;code&gt;src/nest-knex-client/nest-knex-client.controller.ts&lt;/code&gt; and plug in some queries. Of course this is not really a great design pattern (invoking database services directly from your controller), but is really just a quick test that the Knex.js integration works. In reality, you'll want to delegate any such access to true NestJS &lt;em&gt;services&lt;/em&gt; as per NestJS best practices.&lt;/p&gt;

&lt;p&gt;Here's an idea of what you can try. This test relies on the following database table being available (syntax below works with PostgreSql, but may require slight tweaks for other databases):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;cats&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;
   &lt;span class="n"&gt;id&lt;/span&gt;     &lt;span class="nb"&gt;serial&lt;/span&gt;    &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;name&lt;/span&gt;   &lt;span class="nb"&gt;text&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;age&lt;/span&gt;    &lt;span class="nb"&gt;integer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
   &lt;span class="n"&gt;breed&lt;/span&gt;  &lt;span class="nb"&gt;text&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;ALTER&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;cats&lt;/span&gt;
   &lt;span class="k"&gt;ADD&lt;/span&gt; &lt;span class="k"&gt;CONSTRAINT&lt;/span&gt; &lt;span class="n"&gt;cats_pkey&lt;/span&gt;
   &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And here's a sample of what you could try in your test controller. Note that you have the full power of Knex.js available via the &lt;code&gt;knex&lt;/code&gt; object. See here for &lt;a href="http://knexjs.org/#Builder"&gt;lots more interesting samples&lt;/a&gt; of things you can do with Knex. Since you have a handle to the &lt;code&gt;knex&lt;/code&gt; object, all of those &lt;em&gt;Knex Query Builder&lt;/em&gt; methods should just work! In this admittedly silly example, we are both creating and querying some cats in our index route, using the &lt;code&gt;knex&lt;/code&gt; object to access the database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/nest-knex-client/nest-knex-client.controller.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Get&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;NestKnexService&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../nest-knex.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;NestKnexClientController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;nestKnexService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;NestKnexService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;knex&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;nestKnexService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getKnex&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newcat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cats&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fred&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tom cat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cats&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;*&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cats&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;cats&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, fire up the app with &lt;code&gt;npm run start:dev&lt;/code&gt;, and browse to &lt;code&gt;http://localhost:3000&lt;/code&gt;, and you should see the results of your query!&lt;/p&gt;

&lt;p&gt;If you want to test the module with a separate app -- really demonstrating the power of the reusable library module you just built -- read on to see how to upload it as an npm package in one step. And for convenience, you can download a &lt;a href="https://github.com/nestjsplus/knex-cats"&gt;full client app that imports the module here&lt;/a&gt; and use it to test your newly minted Knex.js module. In fact, &lt;a href="https://github.com/nestjsplus/knex-cats"&gt;knex-cats&lt;/a&gt; is a fully working example, so if you've been reading along without coding, that's the easiest way to see the finished product.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bonus Section
&lt;/h3&gt;

&lt;p&gt;Hopefully I kept my promise of showing you how quickly you can generate a dynamic NestJS module that wraps an external resource library in just a few minutes! We're done -- you have a full-fledged Knex.js API at your disposal now! The queries we did in the previous section are just the tip of the iceberg, as Knex.js has &lt;a href="http://knexjs.org/#Builder"&gt;a boat load of cool features&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Let's cover a couple more topics to round out the discussion.&lt;/p&gt;

&lt;h4&gt;
  
  
  A Better API
&lt;/h4&gt;

&lt;p&gt;As mentioned, it's not good practice to access the &lt;code&gt;knex&lt;/code&gt; object directly from our controller. We can make things a little bit better by creating a service to house our database logic. We can also make our Knex.js module a little easier to use by adding a higher level API. Let's do that. We're going to add a &lt;em&gt;provider&lt;/em&gt; that let's us directly inject the &lt;code&gt;knex&lt;/code&gt; object into any service. You'll see how this cleans the API up in a moment.&lt;/p&gt;

&lt;p&gt;First, create a file called &lt;code&gt;nest-knex-connection.provider.ts&lt;/code&gt; inside the &lt;code&gt;src&lt;/code&gt; folder. Add the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/nest-knex-connection.provider.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;KNEX_CONNECTION&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./constants&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;NestKnexService&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./nest-knex.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;connectionFactory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;provide&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;KNEX_CONNECTION&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;useFactory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;nestKnexService&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;nestKnexService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getKnex&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="na"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;NestKnexService&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To follow best practices for providers, we are using the &lt;code&gt;KNEX_CONNECTION&lt;/code&gt; constant as our provider injection token. Be sure to add this line to &lt;code&gt;src/constants.ts&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;KNEX_CONNECTION&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;KNEX_CONNECTION&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;See what we're doing here? We're binding an injection token (&lt;code&gt;KNEX_CONNECTION&lt;/code&gt;) to a factory so that we can use our &lt;code&gt;knex&lt;/code&gt; API object directly, avoiding the need to instantiate the service. For example, once we complete one more step (below), we'll be able to access the &lt;code&gt;knex&lt;/code&gt; object in our controller as follows (notice the slightly simplified technique between &lt;em&gt;old&lt;/em&gt; and &lt;em&gt;new&lt;/em&gt; in the sample code):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/nest-knex-client/nest-knex-client.controller.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Inject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Get&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;KNEX_CONNECTION&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../constants&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;NestKnexClientController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// new&lt;/span&gt;
  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Inject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;KNEX_CONNECTION&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
  &lt;span class="c1"&gt;// old&lt;/span&gt;
  &lt;span class="c1"&gt;// constructor(private readonly nestKnexService: NestKnexService) {}&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// following line no longer needed&lt;/span&gt;
    &lt;span class="c1"&gt;// const knex = this.nestKnexService.getKnex();&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newcat&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cats&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Fred&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;breed&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;tom cat&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;});&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;cats&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;knex&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;select&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;*&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="k"&gt;from&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;cats&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;cats&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To wire this new provider into our module, open up &lt;code&gt;src/nest-knex.module.ts&lt;/code&gt; and make the following changes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Import the &lt;code&gt;connectionFactory&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Add the &lt;code&gt;connectionFactory&lt;/code&gt; to the &lt;code&gt;@Module&lt;/code&gt; metadata &lt;code&gt;providers&lt;/code&gt; and &lt;code&gt;exports&lt;/code&gt; properties. This section of the file will now look like this:
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;connectionFactory&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./nest-knex-connection.provider&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Global&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;NestKnexService&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;connectionFactory&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;NestKnexService&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;connectionFactory&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The complete implementation of our best practices for database access would also have us create a &lt;strong&gt;service&lt;/strong&gt; that accesses the &lt;code&gt;KNEX_CONNECTION&lt;/code&gt; provider, rather than doing that in a controller, but I'll leave that as an exercise for the reader. 😉&lt;/p&gt;

&lt;h4&gt;
  
  
  Dynamic Registration (with a Config Factory)
&lt;/h4&gt;

&lt;p&gt;In the &lt;a href="https://dev.to/nestjs/advanced-nestjs-how-to-build-completely-dynamic-nestjs-modules-1370#async-options-providers-usecase"&gt;dynamic modules article&lt;/a&gt;, we talked about using &lt;em&gt;asynchronous options providers&lt;/em&gt;. By that, we meant that rather than use a &lt;em&gt;statically declared&lt;/em&gt; object containing our connection params like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;    &lt;span class="nx"&gt;NestKnexModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;register&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;pg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;localhost&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;john&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mypassword&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;database&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nest&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5432&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;}),&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We'd instead like to supply our connection options in a dynamic fashion. Good news! This is &lt;strong&gt;already built-in&lt;/strong&gt; to the generated package. Let's test it out. For simplicity, we'll just use an &lt;em&gt;in-place&lt;/em&gt; factory, but you can use the full power of &lt;em&gt;class-based&lt;/em&gt;, &lt;em&gt;factory based&lt;/em&gt;, and &lt;em&gt;existing&lt;/em&gt; providers. To test it, we'll update the registration of the &lt;code&gt;NestKnexModule&lt;/code&gt; in &lt;code&gt;src/nest-knex-client/nest-knex-client.module.ts&lt;/code&gt; to look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// src/nest-knex-client/nest-knex-client/module.ts&lt;/span&gt;
    &lt;span class="nx"&gt;NestKnexModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;registerAsync&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;useFactory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
          &lt;span class="na"&gt;debug&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;client&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;pg&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="na"&gt;connection&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="na"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;localhost&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;john&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mypassword&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;database&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nest&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
            &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5432&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="p"&gt;},&lt;/span&gt;
        &lt;span class="p"&gt;};&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;}),&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's not a terribly exciting example, but you get the idea. You can take advantage of any of the asynchronous provider methods to supply the configuration data to the module. If you check out the &lt;a href="https://github.com/nestjsplus/knex-cats"&gt;knex-cats&lt;/a&gt; sample, you'll see some more robust use of a config module to configure our Knex module dynamically.&lt;/p&gt;

&lt;h4&gt;
  
  
  Publish the Package
&lt;/h4&gt;

&lt;p&gt;If you read my earlier &lt;a href="https://dev.to/nestjs/publishing-nestjs-packages-with-npm-21fm"&gt;Publishing NestJS Packages with npm&lt;/a&gt; article, you may have noticed that the &lt;em&gt;structure&lt;/em&gt; of this package (e.g., the &lt;code&gt;package.json&lt;/code&gt; file, the &lt;code&gt;tsconfig.json&lt;/code&gt; file, the presence of the &lt;code&gt;index.ts&lt;/code&gt; file in the root folder) follows that pattern exactly. As a result, publishing this package is as simple as this (assuming you have an &lt;code&gt;npmjs.com&lt;/code&gt; account, that is):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm publish
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Seriously, that's it!&lt;/p&gt;

&lt;p&gt;Of course, you can always use this ready-made &lt;a href=""&gt;@nestjsplus/knex&lt;/a&gt; package, built &lt;em&gt;exactly&lt;/em&gt; as describe in this article.&lt;/p&gt;

&lt;h3&gt;
  
  
  Not Just External APIs!
&lt;/h3&gt;

&lt;p&gt;The &lt;a href="https://github.com/nestjsplus/dyn-schematics"&gt;@nestjsplus/dyn-schematics package&lt;/a&gt; not only supports generating a standalone package, as described so far, but also generating a regular dynamic module &lt;em&gt;inside an existing project&lt;/em&gt;. I won't cover that in detail here, but the use-case is simple. Let's say you're building a &lt;em&gt;ConfigModule&lt;/em&gt; for use within a project. Simply scaffold it with the &lt;code&gt;dynmod&lt;/code&gt; schematic (instead of &lt;code&gt;dynpkg&lt;/code&gt;), and it will generate a new dynamic module &lt;em&gt;into your existing project&lt;/em&gt;, fully ready for you to implement the details of your &lt;code&gt;register()&lt;/code&gt; and &lt;code&gt;registerAsync()&lt;/code&gt; static methods.&lt;/p&gt;

&lt;p&gt;This works &lt;em&gt;exactly&lt;/em&gt; like the normal CLI commands for adding elements like &lt;em&gt;controllers&lt;/em&gt;, &lt;em&gt;services&lt;/em&gt;, and &lt;em&gt;modules&lt;/em&gt;. In fact, its behavior is very parallel to &lt;code&gt;nest generate module&lt;/code&gt;. It builds the &lt;em&gt;dynamic module&lt;/em&gt; and all its parts (which look quite similar to what we've seen in this article, minus the top level Nest application components) in a folder inside your project, and &lt;em&gt;wires them up to the rest of the project&lt;/em&gt; just as the normal &lt;code&gt;module&lt;/code&gt; schematic does. Read more about it at the github page for &lt;a href="https://github.com/nestjsplus/dyn-schematics"&gt;@nestjsplus/dyn-schematics&lt;/a&gt;. This use-case (adding a dynamic module to an existing project) is &lt;a href="https://github.com/nestjsplus/dyn-schematics#use-case-2-adding-a-dynamic-module-to-an-existing-project"&gt;covered here&lt;/a&gt;. You can easily give it a quick &lt;em&gt;dry run&lt;/em&gt; inside an existing Nest project with:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;nest g &lt;span class="nt"&gt;-c&lt;/span&gt; @nestjsplus/dyn-schematics dynmod myNewModule &lt;span class="nt"&gt;--dry-run&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;If you've been following this series, you've previously learned a powerful architectural pattern for building modular NestJS services and modules, and composing them into applications. Now, with the power of schematics, you can &lt;em&gt;automatically generate&lt;/em&gt; the boilerplate code to implement this powerful pattern. You can use this schematic to customize your own internal modules, or to easily integrate external APIs. In future articles, I'll show you how to build your own schematics to extend the power of NestJS and the NestJS CLI even further!&lt;/p&gt;

&lt;p&gt;Feel free to ask questions, make comments or suggestions, or just say hello in the comments below. And join us at &lt;a href="https://discord.gg/G7Qnnhy"&gt;Discord&lt;/a&gt; for more happy discussions about NestJS. I post there as &lt;em&gt;Y Prospect&lt;/em&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Advanced NestJS: How to build completely dynamic NestJS modules</title>
      <dc:creator>John Biundo</dc:creator>
      <pubDate>Mon, 16 Sep 2019 15:02:26 +0000</pubDate>
      <link>https://forem.com/nestjs/advanced-nestjs-how-to-build-completely-dynamic-nestjs-modules-1370</link>
      <guid>https://forem.com/nestjs/advanced-nestjs-how-to-build-completely-dynamic-nestjs-modules-1370</guid>
      <description>&lt;p&gt;&lt;em&gt;John is a member of the NestJS core team&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;So you've noticed the cool ways in which you can dynamically configure some of the out-of-the-box NestJS modules like &lt;code&gt;@nestjs/jwt&lt;/code&gt;, &lt;code&gt;@nestjs/passport&lt;/code&gt; and &lt;code&gt;@nestjs/typeorm&lt;/code&gt;, and want to know how to do that in your own modules? This is the right article for you! 😃&lt;/p&gt;

&lt;h3&gt;
  
  
  Intro
&lt;/h3&gt;

&lt;p&gt;Over on the &lt;a href="https://docs.nestjs.com"&gt;NestJS documentation site&lt;/a&gt;, we recently added a new chapter on &lt;a href="https://docs.nestjs.com/fundamentals/dynamic-modules"&gt;dynamic modules&lt;/a&gt;. This is a fairly advanced chapter, and along with some recent significant improvements to the &lt;a href="https://docs.nestjs.com/fundamentals/async-providers"&gt;asynchronous providers chapter&lt;/a&gt;, Nest developers have some great new resources to help build configurable modules that can be assembled into complex, robust applications.&lt;/p&gt;

&lt;p&gt;This article builds on that foundation and takes it one step further. One of the hallmarks of NestJS is making &lt;strong&gt;asynchronous programming&lt;/strong&gt; very straightforward. Nest fully embraces Node.js Promises and the &lt;code&gt;async/await&lt;/code&gt; paradigm. Coupled with Nest's signature Dependency Injection features, this is an extremely powerful combination. Let's look at how these features can be used to create &lt;em&gt;dynamically configurable modules&lt;/em&gt;. Mastering this skill lets you build modules that are re-usable in any context. This enables fully context-aware, re-usable packages (libraries), and lets you &lt;em&gt;assemble&lt;/em&gt; apps that deploy smoothly across cloud providers and throughout the DevOps spectrum - from development to staging to production.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic Dynamic Modules
&lt;/h3&gt;

&lt;p&gt;In the &lt;a href="https://docs.nestjs.com/fundamentals/dynamic-modules"&gt;dynamic modules chapter&lt;/a&gt;, the end result of the code sample is the ability to pass in an &lt;code&gt;options&lt;/code&gt; object to configure a module that is being imported. After reading that chapter, we know how the following code snippet works via the dynamic module API.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Module&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AppController&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app.controller&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AppService&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ConfigModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./config/config.module&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;ConfigModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;register&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;folder&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./config&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;})],&lt;/span&gt;
  &lt;span class="na"&gt;controllers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;AppController&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;AppService&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;AppModule&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If you've played around with any NestJS modules - such as &lt;code&gt;@nestjs/typeorm&lt;/code&gt;, &lt;code&gt;@nestjs/passport&lt;/code&gt; or &lt;code&gt;@nestjs/jwt&lt;/code&gt; - you'll have noticed that they go beyond the features described in that chapter. In addition to supporting the &lt;code&gt;register(...)&lt;/code&gt; method shown above, they also support a fully &lt;strong&gt;dynamic and asynchronous&lt;/strong&gt; version of the method. For example, with the &lt;code&gt;@nestjs/jwt&lt;/code&gt; module, you can use a construct like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;JwtModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;registerAsync&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;useClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ConfigService&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;With this construct, not only is the module dynamically configured, but the &lt;em&gt;options passed to the dynamic module are themselves constructed dynamically&lt;/em&gt;. This is higher order functionality at its best. Configuration options are provided based on values &lt;em&gt;extracted from the environment&lt;/em&gt; by the &lt;code&gt;ConfigService&lt;/code&gt;, meaning they can be changed completely external to your feature code. Compare that to hardcoding a parameter, as with &lt;code&gt;ConfigModule.register({ folder: './config'})&lt;/code&gt;, and you can immediately see the win.&lt;/p&gt;

&lt;p&gt;In this article, we'll further explore why you may need this feature, and how to build it. Make sure you have a firm grasp on the concepts in the &lt;a href="https://docs.nestjs.com/fundamentals/custom-providers"&gt;Custom providers&lt;/a&gt; and &lt;a href="https://docs.nestjs.com/fundamentals/dynamic-modules"&gt;Dynamic modules&lt;/a&gt; chapters before moving on to the next section.&lt;/p&gt;

&lt;h3&gt;
  
  
  Async Options Providers Use-case
&lt;/h3&gt;

&lt;p&gt;The section heading above is quite a mouthful! What the heck is an &lt;em&gt;async options provider&lt;/em&gt;?&lt;/p&gt;

&lt;p&gt;To answer that, first consider once again that our example above (the &lt;code&gt;ConfigModule.register({ folder: './config'})&lt;/code&gt; part) is passing a &lt;strong&gt;static options object&lt;/strong&gt; in to the &lt;code&gt;register()&lt;/code&gt; method. As we learned in the &lt;em&gt;Dynamic modules&lt;/em&gt; chapter, this options object is used to customize the behavior of the module. (Review &lt;a href="https://docs.nestjs.com/fundamentals/dynamic-modules"&gt;that chapter&lt;/a&gt; before proceeding if this concept is not familiar). As mentioned, we're now going to take that concept &lt;em&gt;one step further&lt;/em&gt;, and let our &lt;strong&gt;options&lt;/strong&gt; object be provided dynamically at run-time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Async (Dynamic) Options Example
&lt;/h3&gt;

&lt;p&gt;To give us a concrete working example for the remainder of this article, I'm going to introduce a new module. We'll then walk through how to use async options providers in that context.&lt;/p&gt;

&lt;p&gt;I recently published the &lt;a href="https://github.com/nestjsplus/massive"&gt;@nestjsplus/massive package&lt;/a&gt; to enable a Nest project to easily make use of the impressive &lt;a href="https://massivejs.org/"&gt;MassiveJS&lt;/a&gt; library to do database work. We'll study the architecture of that package and use parts of it for the code we analyze in this article. Briefly, the package &lt;em&gt;wraps&lt;/em&gt; the MassiveJS library into a Nest module. MassiveJS provides its entire API to each consumer module (e.g., each feature module) through a &lt;code&gt;db&lt;/code&gt; object that has methods like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;db.find()&lt;/code&gt; to retrieve database records&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;db.update()&lt;/code&gt; to update database records&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;db.myFunction()&lt;/code&gt; to execute scripts or database procedures/functions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The primary function of the &lt;code&gt;@nestjsplus/massive&lt;/code&gt; package is to make a connection to the database, and return the &lt;code&gt;db&lt;/code&gt; object. In our feature modules, we then access the database using methods hung off the &lt;code&gt;db&lt;/code&gt; object, like those shown above. Right off the bat, it should be clear that in order to establish a database connection, we need to pass in some connection parameters. In our case, with PostgreSQL, those parameters would be something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;john&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;password&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;localhost&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5432&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;database&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nest&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;What we quickly realize is that it's not optimal to hard-code those connection parameters in our Nest app. The conventional solution is to supply them through some sort of &lt;em&gt;Configuration Module&lt;/em&gt;. And that's exactly what we can do with Nest's &lt;code&gt;@nestjs/jwt&lt;/code&gt; module as shown in the example above. That, in a nutshell, is the purpose of &lt;em&gt;async options providers&lt;/em&gt;. Now let's figure out how to do that in our Massive module.&lt;/p&gt;

&lt;h3&gt;
  
  
  Coding for Async Options Providers
&lt;/h3&gt;

&lt;p&gt;To begin with, we can imagine supporting a module import statement using the same construct we found in &lt;code&gt;@nestjs/jwt&lt;/code&gt;, like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;MassiveModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;registerAsync&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;useClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ConfigService&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;If this doesn't look familiar, take a quick look at &lt;a href="https://docs.nestjs.com/fundamentals/custom-providers#class-providers-useclass"&gt;this section&lt;/a&gt; of the &lt;em&gt;Custom providers&lt;/em&gt; chapter. The similarities are deliberate. We're taking inspiration from the concepts learned from &lt;em&gt;Custom providers&lt;/em&gt; to build a more flexible means of supplying options to our dynamic modules.&lt;/p&gt;

&lt;p&gt;Let's dig in to the implementation. This is going to be a &lt;strong&gt;long&lt;/strong&gt; section, so take a deep breath, maybe refill your coffee cup, but don't worry - we can do this! 😄. Bear in mind that we are walking through a design pattern that, once you understand it, you can &lt;strong&gt;confidently&lt;/strong&gt; cut-and-paste as boilerplate to start building any module that needs dynamic configuration. But before the cutting and pasting starts, let's make sure to understand the template so we can customize it to our needs. Just remember that you won't have to write this from scratch each time!&lt;/p&gt;

&lt;p&gt;First, let's revisit what we expect to accomplish with the &lt;code&gt;registerAsync(...)&lt;/code&gt; construct above. Basically, we're saying "Hey module! I don't want to give you the option property values in code. How about instead I give you a class that has a &lt;em&gt;method&lt;/em&gt; you can call to get the option values?". This would give us a great deal of flexibility in how we can generate the options dynamically at run-time.&lt;/p&gt;

&lt;p&gt;This implies that our dynamic module is going to need to do a little more work in this case, as compared to the static options technique, to acquire its connection options. We're going to work our way up to that result. We begin by formalizing some definitions. We're trying to supply MassiveJS with its expected &lt;em&gt;connection options&lt;/em&gt;, so first we'll create an interface to model that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;MassiveConnectOptions&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="cm"&gt;/**
   * server name or IP address
   */&lt;/span&gt;
  &lt;span class="nl"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="cm"&gt;/**
   * server port number
   */&lt;/span&gt;
  &lt;span class="nl"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="cm"&gt;/**
   * database name
   */&lt;/span&gt;
  &lt;span class="nl"&gt;database&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="cm"&gt;/**
   * user name
   */&lt;/span&gt;
  &lt;span class="nl"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="cm"&gt;/**
   * user password, or a function that returns one
   */&lt;/span&gt;
  &lt;span class="nl"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="cm"&gt;/**
   * use SSL (it also can be a TSSLConfig-like object)
   */&lt;/span&gt;

  &lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There are actually more options available (we can see what they are from examining the &lt;a href="https://massivejs.org/docs/connecting"&gt;MassiveJS connection options&lt;/a&gt; documentation), but let's keep the choices basic for now. The ones we modelled are &lt;strong&gt;required&lt;/strong&gt; to establish a connection. As a side note, we're using JSDoc to document them so that we get a nice Intellisense developer experience when we later use the module.&lt;/p&gt;

&lt;p&gt;The next concept to grapple with is as follows. Since our consumer module (the one &lt;strong&gt;calling&lt;/strong&gt; &lt;code&gt;registerAsync()&lt;/code&gt; to import the MassiveJS module) is handing us a class and expecting us to call a method on that class, we can surmise that we'll probably need to use some sort of factory pattern. In other words, somewhere, we're going to have to instantiate that class, call a method on it, and use the result returned from that method call as our connection options, right? Sounds (kind of) like a factory. Let's go with that concept for now.&lt;/p&gt;

&lt;p&gt;Let's describe our prospective factory with an interface. The method could be something like &lt;code&gt;createMassiveConnectOptions()&lt;/code&gt;. It needs to return an object of type &lt;code&gt;MassiveConnectOptions&lt;/code&gt; (the interface we defined a minute ago). So we have:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;MassiveOptionsFactory&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;createMassiveConnectOptions&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;MassiveConnectOptions&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="nx"&gt;MassiveConnectOptions&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Nice! We can return the options object directly, or return a Promise that will resolve to an options object. Nest makes it super easy to support either. Hence, we now see the "async" part of our &lt;em&gt;async options provider&lt;/em&gt; coming into play.&lt;/p&gt;

&lt;p&gt;Now, let's ask the following: what mechanism is going to actually &lt;em&gt;call&lt;/em&gt; our factory function at run time, take the resulting &lt;code&gt;options&lt;/code&gt; object, and make it available to the part of our code that needs it? Hmmm... if only we had some general purpose mechanism. Maybe a feature that we could register an arbitrary object (or function returning an object) with at run-time, and then have that object passed into a constructor. Anybody got any ideas? 😉&lt;/p&gt;

&lt;p&gt;Well of course, we've got the awesome NestJS Dependency Injection system at our disposal. That seems like a good fit! Let's figure out how to do that.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;recipe&lt;/em&gt; for binding something to the Nest IoC container, and later having it injected, is captured in an object called a &lt;em&gt;provider&lt;/em&gt;. Let's whip up an &lt;em&gt;options provider&lt;/em&gt; that will meet our needs. If you need a quick refresher course on custom providers, go ahead and re-read the &lt;a href="https://docs.nestjs.com/fundamentals/custom-providers"&gt;Custom providers chapter&lt;/a&gt; now. It won't take long. I'll wait right here.&lt;/p&gt;

&lt;p&gt;OK, so now you remember that we can define our &lt;em&gt;options provider&lt;/em&gt; with a construct like the following. We already have an intuition that we need a factory provider, so this seems like the right construct:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;provide&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MASSIVE_CONNECT_OPTIONS&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;useFactory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="c1"&gt;// &amp;lt;-- we need to get our options factory inserted here!&lt;/span&gt;
  &lt;span class="nx"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;     &lt;span class="c1"&gt;// &amp;lt;-- we need to supply injectable parameters for useFactory here!&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's tie a few things together. We're in pretty deep, so now's a good time to do a quick refresher on the big picture, and assess where we're at:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;We're writing code that constructs, then returns, a dynamic module (our &lt;code&gt;registerAsync()&lt;/code&gt; static method will house that code).&lt;/li&gt;
&lt;li&gt;The dynamic module it returns can be imported into other feature modules, and provides a service (the thing that connects to the database and returns a &lt;code&gt;db&lt;/code&gt; object).&lt;/li&gt;
&lt;li&gt;That service needs to be &lt;em&gt;configured&lt;/em&gt; at the time the module is constructed. A more helpful way to say this is that the service &lt;em&gt;depends on&lt;/em&gt; a dynamically constructed &lt;em&gt;options object&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;We're going to construct that configuration options object at run-time, using a class that the consuming module hands us.&lt;/li&gt;
&lt;li&gt;That class contains a method that knows how to return an appropriate options object.&lt;/li&gt;
&lt;li&gt;We're going to use the NestJS Dependency Injection system to do the heavy lifting to manage that options object dependency for us.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;OK, so we're working on steps 4, 5, and 6 right now. We're not yet ready to assemble the entire dynamic module. Before we do that, we have to work out the mechanics of our &lt;em&gt;options provider&lt;/em&gt;. Returning to that task, we should be able to see how to fill in the blanks in the skeleton &lt;em&gt;options provider&lt;/em&gt; we sketched out earlier (see the lines annotated &lt;code&gt;&amp;lt;-- we need to...&lt;/code&gt; above). We can fill in those values based on how the &lt;code&gt;registerAsync()&lt;/code&gt; call was made:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;MassiveModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;registerAsync&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;useClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ConfigService&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's go ahead and fill them in now based on what we know. We'll sketch a static version of the object, just to see what we're trying to generate dynamically in the code we're about to write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;provide&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MASSIVE_CONNECT_OPTIONS&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;useFactory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;ConfigService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;ConfigService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createMassiveConnectOptions&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
  &lt;span class="nx"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;ConfigService&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So we've now figured out what our generated &lt;em&gt;options provider&lt;/em&gt; should look like. Good so far? It's important to remember that the &lt;code&gt;'MASSIVE_CONNECT_OPTIONS'&lt;/code&gt; provider is just fulfilling a dependency &lt;em&gt;inside the dynamic module&lt;/em&gt;. Now that I mention it, we haven't really looked at the service that depends on the &lt;code&gt;'MASSIVE_CONNECT_OPTIONS'&lt;/code&gt; provider we're working so hard to supply. Let's connect a few more dots there and take a quick moment to consider that service. The service -- the one that connects and returns a &lt;code&gt;db&lt;/code&gt; object -- is, predictably enough, declared in the &lt;code&gt;MassiveService&lt;/code&gt; class. It's surprisingly straightforward:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Injectable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;MassiveService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;_massiveClient&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Inject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MASSIVE_CONNECT_OPTIONS&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="nx"&gt;_massiveConnectOptions&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nx"&gt;connect&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_massiveClient&lt;/span&gt;
      &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_massiveClient&lt;/span&gt;
      &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_massiveClient&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;massive&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;_massiveConnectOptions&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;MassiveService&lt;/code&gt; class injects the connection options provider and uses that information to make the API call needed to asynchronously create a database connection (&lt;code&gt;await massive(this._massiveConnectOptions)&lt;/code&gt;). Once made, it caches the connection so it can return an existing connection on subsequent calls. That's it. That's why we're jumping through hoops to be able to pass in our &lt;em&gt;options provider&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;We've now worked out the concepts and sketched out the piece parts of our dynamically configurable module. We're ready to start assembling them. First we'll write some &lt;em&gt;glue code&lt;/em&gt; to pull this all together. As we learned in the &lt;a href="https://docs.nestjs.com/fundamentals/dynamic-modules"&gt;dynamic modules&lt;/a&gt; chapter, all that glue should live in the module definition class. Let's create the &lt;code&gt;MassiveModule&lt;/code&gt; class for that purpose. We'll describe what's happening in this code just below it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Global&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;MassiveModule&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="cm"&gt;/**
   *  public static register( ... )
   *  omitted here for brevity
   */&lt;/span&gt;

  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nx"&gt;registerAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;connectOptions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MassiveConnectAsyncOptions&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;DynamicModule&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;module&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MassiveModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
        &lt;span class="nx"&gt;MassiveService&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createConnectProviders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;connectOptions&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="na"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;MassiveService&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nx"&gt;createConnectProviders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MassiveConnectAsyncOptions&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Provider&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;provide&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MASSIVE_CONNECT_OPTIONS&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;useFactory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;optionsFactory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MassiveOptionsFactory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
          &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;optionsFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createMassiveConnectOptions&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
        &lt;span class="na"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useClass&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;provide&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useClass&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;useClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;useClass&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Let's get a firm handle on what this code does. This is really where the rubber meets the road, so take time to understand it carefully. Consider that if we were to insert a logging statement displaying the return value from the following call:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="nx"&gt;registerAsync&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;useClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ConfigService&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;we'd see an object that looks pretty much like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;module&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MassiveModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;MassiveService&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;provide&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;MASSIVE_CONNECT_OPTIONS&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;useFactory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;optionsFactory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MassiveOptionsFactory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;optionsFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createMassiveConnectOptions&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
      &lt;span class="na"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;ConfigService&lt;/span&gt; &lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;provide&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ConfigService&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;useClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ConfigService&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="nx"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;MassiveService&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This should be pretty recognizable, as it would plug right into a standard &lt;code&gt;@Module()&lt;/code&gt; decorator to declare module metadata (well, all except for the &lt;code&gt;module&lt;/code&gt; property, which is part of the Dynamic module API). To describe it in English, we're returning a dynamic module that declares &lt;strong&gt;three&lt;/strong&gt; providers, exporting one of them for use in other modules that may import it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The first provider is obviously the &lt;code&gt;MassiveService&lt;/code&gt; itself, which we plan to use in our consumer's feature modules, so we duly export it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The second provider (&lt;code&gt;'MASSIVE_CONNECT_OPTIONS'&lt;/code&gt;) is &lt;em&gt;only used internally&lt;/em&gt; by the &lt;code&gt;MassiveService&lt;/code&gt; to ingest the connection options it needs (notice that we do &lt;strong&gt;not&lt;/strong&gt; export it). Let's take a little closer look at that &lt;code&gt;useFactory&lt;/code&gt; construct. Note that there's also an &lt;code&gt;inject&lt;/code&gt; property, which is used to inject the &lt;code&gt;ConfigService&lt;/code&gt; into the factory function. This is described in detail &lt;a href="https://docs.nestjs.com/fundamentals/custom-providers#factory-providers-usefactory"&gt;here in the Custom providers chapter&lt;/a&gt;, but basically, the idea is that the factory function takes optional input arguments which, if specified, are resolved by injecting a provider from the &lt;code&gt;inject&lt;/code&gt; property array. You might be wondering where that &lt;code&gt;ConfigService&lt;/code&gt; injectable comes from. Read on 😉.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Finally, we have a third provider, also used only internally by our dynamic module (and hence not exported), which is our single private instance of the &lt;code&gt;ConfigService&lt;/code&gt;. So, Nest is going to instantiate a &lt;code&gt;ConfigService&lt;/code&gt; &lt;em&gt;inside the dynamic module context&lt;/em&gt; (this makes sense, right? We told our module to &lt;code&gt;useClass&lt;/code&gt;, which means "create your own instance"), and that will be injected into the factory.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you made it this far - congrats! That was the hardest part. We just worked out all the mechanics of assembling a dynamically configurable module. The rest of the article is gravy!&lt;/p&gt;

&lt;p&gt;One other thing that should be obvious from looking at the generated &lt;code&gt;useFactory&lt;/code&gt; syntax above is that the &lt;code&gt;ConfigService&lt;/code&gt; class must implement a &lt;code&gt;createMassiveConnectOptions()&lt;/code&gt; method. This should be a familiar pattern if you're already using some sort of configuration module that implements various functions to return options of a particular shape for each service it gets plugged into. Now perhaps you can see a little more clearly how this all fits together.&lt;/p&gt;

&lt;h3&gt;
  
  
  Variant Forms of Asynchronous Options Providers
&lt;/h3&gt;

&lt;p&gt;What we've built so far allows us to configure the &lt;code&gt;MassiveModule&lt;/code&gt; by handing it a class whose purpose is to dynamically provide connection options. Let's just remind ourselves again how that looks from the consumer perspective:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="nx"&gt;MassiveModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;registerAsync&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;useClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ConfigService&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We can refer to this as configuring our dynamic module with a &lt;code&gt;useClass&lt;/code&gt; technique (AKA a &lt;em&gt;class provider&lt;/em&gt;). Are there other techniques? You may recall seeing several other similar patterns in the &lt;em&gt;Custom providers&lt;/em&gt; chapter. We can model our &lt;code&gt;registerAsync()&lt;/code&gt; interface based on those patterns. Let's sketch out what those techniques would look like from a consumer module perspective, and then we can easily add support for them.&lt;/p&gt;

&lt;h4&gt;
  
  
  Factory Providers: useFactory
&lt;/h4&gt;

&lt;p&gt;While we did make use of a factory in the previous section, that was strictly &lt;em&gt;internal&lt;/em&gt; to the dynamic module construction mechanics, not a part of the callable API. What would &lt;code&gt;useFactory&lt;/code&gt; look like when exposed as an option for our &lt;code&gt;registerAsync()&lt;/code&gt; method?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;MassiveModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;registerAsync&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;useFactory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;localhost&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;port&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5432&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;database&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;nest&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;john&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;password&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;})]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the sample above, we supplied a very simple factory &lt;em&gt;in place&lt;/em&gt;, but we could of course plug in (or pass in a function implementing) any arbitrarily sophisticated factory as long as it returns an appropriate connections object.&lt;/p&gt;

&lt;h4&gt;
  
  
  Alias Providers: useExisting
&lt;/h4&gt;

&lt;p&gt;This sometimes-overlooked construct is actually extremely useful. In our context, it means we can ensure that we re-use an existing &lt;em&gt;options provider&lt;/em&gt; rather than instantiating a new one. For example, &lt;code&gt;useClass: ConfigService&lt;/code&gt; will cause Nest to create and inject a new private instance of our &lt;code&gt;ConfigService&lt;/code&gt;. In the real world, we'll usually want a single shared instance of the &lt;code&gt;ConfigService&lt;/code&gt; injected anywhere it's needed, not a private copy. The &lt;code&gt;useExisting&lt;/code&gt; technique is our friend here. Here's how it would look:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;MassiveModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;registerAsync&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;useExisting&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ConfigService&lt;/span&gt;
  &lt;span class="p"&gt;})]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Supporting Multiple Async Options Providers Techniques
&lt;/h3&gt;

&lt;p&gt;We're in the home stretch. We're going to focus now on generalizing and optimizing our &lt;code&gt;registerAsync()&lt;/code&gt; method to support the additional techniques described above. When we're done, our module will support all three techniques:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;useClass - to get a private instance of the options provider.&lt;/li&gt;
&lt;li&gt;useFactory - to use a function as the options provider.&lt;/li&gt;
&lt;li&gt;useExisting - to re-use an existing (shared, &lt;code&gt;SINGLETON&lt;/code&gt;) service as the options provider.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I'm going to jump right to the code, as we're all getting weary now 😉. I'll describe the key elements below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Global&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;MassiveService&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;MassiveService&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;MassiveModule&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

  &lt;span class="cm"&gt;/**
   *  public static register( ... )
   *  omitted here for brevity
   */&lt;/span&gt;

  &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nx"&gt;registerAsync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;connectOptions&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MassiveConnectAsyncOptions&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;DynamicModule&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;module&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MassivconnectOptions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;imports&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;&lt;span class="nx"&gt;eModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
      &lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createConnectProviders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;connectOptions&lt;/span&gt;&lt;span class="p"&gt;)],&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nx"&gt;createConnectProviders&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MassiveConnectAsyncOptions&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Provider&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useExisting&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useFactory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createConnectOptionsProvider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;)];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// for useClass&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createConnectOptionsProvider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;provide&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useClass&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;useClass&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useClass&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="nx"&gt;createConnectOptionsProvider&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MassiveConnectAsyncOptions&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;Provider&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useFactory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;

      &lt;span class="c1"&gt;// for useFactory&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;provide&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MASSIVE_CONNECT_OPTIONS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;useFactory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useFactory&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;inject&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
      &lt;span class="p"&gt;};&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="c1"&gt;// For useExisting...&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;provide&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MASSIVE_CONNECT_OPTIONS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;useFactory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;optionsFactory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MassiveConnectOptionsFactory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;optionsFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createMassiveConnectOptions&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
      &lt;span class="na"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useExisting&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nx"&gt;options&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useClass&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Before discussing the details of the code, let's cover a few superficial changes to make sure they don't trip you up.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We now use the constant &lt;code&gt;MASSIVE_CONNECT_OPTIONS&lt;/code&gt; in place of a string-valued token. This is a simple best practice convention, covered at the end of &lt;a href="https://docs.nestjs.com/fundamentals/custom-providers#non-class-based-provider-tokens"&gt;this section of the docs&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Rather than listing &lt;code&gt;MassiveService&lt;/code&gt; in the &lt;code&gt;providers&lt;/code&gt; and &lt;code&gt;exports&lt;/code&gt; properties of the dynamically constructed module, we promoted them up to live in the &lt;code&gt;@Module()&lt;/code&gt; decorator metadata. Why? Partly style, and partly to keep the code DRY. The two approaches are equivalent.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Fully Understanding the Code
&lt;/h3&gt;

&lt;p&gt;You should be able to trace the path through this code to see how it handles each case uniquely. I highly recommend you do the following exercise. Construct an arbitrary &lt;code&gt;registerAsync()&lt;/code&gt; registration call on paper, and walk through the code to predict what the returned dynamic module will look like. This will strongly reinforce the patterns and help you firmly connect all the dots.&lt;/p&gt;

&lt;p&gt;For example, if we were to code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;MassiveModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;registerAsync&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;useExisting&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ConfigService&lt;/span&gt;
  &lt;span class="p"&gt;})]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We could expect a dynamic module to be constructed with the following properties:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;module&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MassiveModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[],&lt;/span&gt;
  &lt;span class="nx"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;provide&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MASSIVE_CONNECT_OPTIONS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;useFactory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;optionsFactory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MassiveConnectOptionsFactory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;optionsFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createMassiveConnectOptions&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
      &lt;span class="na"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;ConfigService&lt;/span&gt; &lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;(Note: because the module is preceded by an &lt;code&gt;@Module()&lt;/code&gt; decorator that now lists &lt;code&gt;MassiveService&lt;/code&gt; as a provider and exports, it our resulting dynamic module will &lt;em&gt;also&lt;/em&gt; have those properties. Above we are just showing the elements that get added dynamically.)&lt;/p&gt;

&lt;p&gt;Consider another question. How is the &lt;code&gt;ConfigService&lt;/code&gt; available inside the factory for injection in this &lt;code&gt;useExisting&lt;/code&gt; case? Well - heh heh - that's kind of a trick question. In the sample above, I assumed that it was already visible inside the consuming module -- perhaps as a global module (one declared with &lt;code&gt;@Global()&lt;/code&gt;). Let's say that wasn't true, and that it lives in &lt;code&gt;ConfigModule&lt;/code&gt; which has &lt;strong&gt;not&lt;/strong&gt; somehow registered ConfigService as a global provider. Can our code handle this? Let's see.&lt;/p&gt;

&lt;p&gt;Our registration would instead look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;MassiveModule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;registerAsync&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;useExisting&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ConfigService&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;ConfigModule&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;})]&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And our resulting dynamic module would look like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;module&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MassiveModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;ConfigModule&lt;/span&gt; &lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="nx"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="na"&gt;provide&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MASSIVE_CONNECT_OPTIONS&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;useFactory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;optionsFactory&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MassiveConnectOptionsFactory&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;optionsFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createMassiveConnectOptions&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt;
      &lt;span class="na"&gt;inject&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;ConfigService&lt;/span&gt; &lt;span class="p"&gt;],&lt;/span&gt;
    &lt;span class="p"&gt;},&lt;/span&gt;
  &lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Do you see how the pieces fit together?&lt;/p&gt;

&lt;p&gt;Another exercise is to ponder the difference in the code paths when you use &lt;code&gt;useClass&lt;/code&gt; vs. &lt;code&gt;useExisting&lt;/code&gt;. The important point is how we either instantiate a new &lt;code&gt;ConfigService&lt;/code&gt; object, or inject an existing one. It's worth working through those details, as the concepts will give you a full picture of how NestJS modules and providers fit together in a coherent way. But this article is already too long, so I'll leave that as an exercise for you, dear reader. 😄&lt;/p&gt;

&lt;p&gt;If you have questions, feel free to ask in the comments below!&lt;/p&gt;

&lt;h3&gt;
  
  
  Conclusion
&lt;/h3&gt;

&lt;p&gt;The patterns illustrated above are used throughout Nest's add-on modules, like &lt;code&gt;@nestjs/jwt&lt;/code&gt;, &lt;code&gt;@nestjs/passport&lt;/code&gt; and &lt;code&gt;@nestjs/typeorm&lt;/code&gt;. Hopefully you now see not only how powerful these patterns are, but how you can make use of them in your own project.&lt;/p&gt;

&lt;p&gt;As a next step, you may want to consider browsing through the source code of those modules, now that you have a roadmap. You can also see a slightly evolved version of the code in this article in the &lt;a href="https://github.com/nestjsplus/massive"&gt;@nestjsplus/massive repository&lt;/a&gt; (while you're there, maybe give it a quick ⭐ if you like this article 😉). The main difference between the code in this article and that repo is that the production version needs to handle &lt;strong&gt;multiple&lt;/strong&gt; asynchronous options providers, so there's a tiny bit more plumbing.&lt;/p&gt;

&lt;p&gt;Now you can confidently start using these powerful patterns in your own code to create robust and flexible modules that work reliably in a wide variety of contexts.&lt;/p&gt;

&lt;p&gt;As a final bonus, if you're building an Open Source package for public use, just combine this technique with the steps described in my last article &lt;a href="https://dev.to/nestjs/publishing-nestjs-packages-with-npm-21fm"&gt;on publishing NPM packages&lt;/a&gt;, and you're all set.&lt;/p&gt;

&lt;p&gt;Feel free to ask questions, make comments or suggestions, or just say hello in the comments below. And join us at &lt;a href="https://discord.gg/G7Qnnhy"&gt;Discord&lt;/a&gt; for more happy discussions about NestJS. I post there as &lt;em&gt;Y Prospect&lt;/em&gt;.&lt;/p&gt;

</description>
      <category>nestjs</category>
      <category>nest</category>
      <category>dependencyinjection</category>
      <category>providers</category>
    </item>
    <item>
      <title>Publishing NestJS Packages with npm</title>
      <dc:creator>John Biundo</dc:creator>
      <pubDate>Mon, 19 Aug 2019 17:50:38 +0000</pubDate>
      <link>https://forem.com/nestjs/publishing-nestjs-packages-with-npm-21fm</link>
      <guid>https://forem.com/nestjs/publishing-nestjs-packages-with-npm-21fm</guid>
      <description>&lt;p&gt;&lt;em&gt;John is a member of the NestJS core team, primarily contributing to the documentation.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This is the first in a multi-part series on building re-usable npm packages for NestJS. This article focuses on the mechanics of the process:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;building a simple npm package&lt;/li&gt;
&lt;li&gt;consuming and testing it locally in a NestJS app&lt;/li&gt;
&lt;li&gt;publishing it to NPM&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This article lays the foundation for future articles in this series, which delve into more complex NestJS packages.&lt;/p&gt;

&lt;h3&gt;
  
  
  Intro
&lt;/h3&gt;

&lt;p&gt;We all interact with the npm registry on a daily basis. Usually the registry itself, and how it works, are conveniently relegated to the background - we don't have to think about the details when we run &lt;code&gt;npm install&lt;/code&gt;. But sometimes we need to work with the registry more directly - perhaps we're publishing a core infrastructure/utility package for private use across teams within an organization, or maybe we're releasing an open source package for public use.&lt;/p&gt;

&lt;p&gt;Publishing a package for the first time, then seeing it install with &lt;code&gt;npm install&lt;/code&gt; is pretty fun.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F6t7bci79p15a0wej84kw.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2F6t7bci79p15a0wej84kw.gif" alt="npm install"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And it's surprisingly easy. While there are plenty of general npm tutorials out there, I've created this tutorial, along with a &lt;a href="https://github.com/nestjsplus/nestjs-package-starter" rel="noopener noreferrer"&gt;package starter repository&lt;/a&gt; that you can quickly clone to set things up in a standard way, and skip past some of the little hurdles.  While there really isn't anything Nest-specific about this, TypeScript can introduce a wrinkle or two not typically covered in other npm tutorials.  And combined with the the NestJS environment, you can enjoy a productive iterative development/deployment workflow if you follow a few basic steps. That's what I'll cover here. Follow along, and you should be able to publish a basic NestJS package in about 15 minutes.&lt;/p&gt;

&lt;p&gt;As to why you want to do this, I can think of at least two great reasons:&lt;br&gt;
1) Learning how to publish a package demystifies one of the things we tend to take for granted. It probably ought to be on the resume of any self-respecting full stack developer. 😃&lt;br&gt;
2) Whether you publish to the npmjs.com registry, or a private one, publishing an npm package is the standard way of creating and sharing re-usable packages.&lt;/p&gt;

&lt;p&gt;Again, while the sample package we'll build in this tutorial is trivial, this article lays the foundation for several more articles that will go into detail on much more sophisticated modules.  Stay tuned!&lt;/p&gt;

&lt;h3&gt;
  
  
  Pre-requisites
&lt;/h3&gt;

&lt;p&gt;If you plan to follow the steps in this article, make sure you have an account at &lt;a href="https://www.npmjs.com" rel="noopener noreferrer"&gt;www.npmjs.com&lt;/a&gt; (you can use a free one for this exercise, and indeed, unless you want to publish &lt;em&gt;private&lt;/em&gt; packages, you can stick with a free account).  You could also publish to a private npm registry like &lt;a href="https://github.com/verdaccio/verdaccio" rel="noopener noreferrer"&gt;verdaccio&lt;/a&gt;, but setting that up is beyond the scope of this article.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up for development
&lt;/h3&gt;

&lt;p&gt;If you're live-coding along with reading this article, I have a couple of brief recommendations.&lt;/p&gt;

&lt;p&gt;First, note that we'll actually be working on two &lt;em&gt;separate&lt;/em&gt; but related bits of code.  One for the npm package, and one for a companion NestJS app that will consume and exercise the package.&lt;/p&gt;

&lt;p&gt;I recommend sticking with the names used below to keep things properly synchronized.  If you're building your own &lt;strong&gt;real&lt;/strong&gt; package with its own unique name, just make sure to synchronize the name you choose across the various places it occurs (folders, package.json, npmjs, git, etc.).&lt;/p&gt;

&lt;p&gt;Next, I recommend you open two command line windows (as well as two editing sessions) to work on both bits of code simultaneously. This will help you get the feel of a useful development pattern for building a package in an iterative fashion.  The rest of this tutorial will refer to these two command line windows.  If you prefer to work in a single command line window, just pay careful attention to what folder you're in as you execute each step!&lt;/p&gt;

&lt;h4&gt;
  
  
  Setting up the folders
&lt;/h4&gt;

&lt;p&gt;In terminal window 1, create a new folder.  This will be the &lt;em&gt;parent&lt;/em&gt; folder for both parts of the project: the package we're building, and our small companion NestJS app.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;mkdir &lt;/span&gt;nestmod &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nb"&gt;cd &lt;/span&gt;nestmod


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  Clone the module starter repo
&lt;/h4&gt;

&lt;p&gt;In terminal window 1, start by cloning the &lt;a href="https://github.com/nestjsplus/nestjs-package-starter" rel="noopener noreferrer"&gt;nestjs-package-starter repo&lt;/a&gt;.  This is a starter repo that sets up a lot of the default details for your NestJS related npm package.  These details can be a bit tedious to get right.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

git clone https://github.com/nestjsplus/nestjs-package-starter.git


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Once completed, this step should result in a brand new package template in the &lt;code&gt;nestjs-package-starter&lt;/code&gt; sub-folder.&lt;/p&gt;

&lt;p&gt;Now install its dependencies. Still in terminal window 1:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;cd &lt;/span&gt;nestjs-package-starter
npm &lt;span class="nb"&gt;install&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  Create the test app
&lt;/h4&gt;

&lt;p&gt;In your second terminal window, make sure you start out in the top level folder you created (I created &lt;code&gt;nestmod&lt;/code&gt; above so that's what I would use; use whatever folder you created to follow along with the tutorial).  Scaffold the small NestJS app we'll be using to exercise our package.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

nest new test-app


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Choose your preferred package manager (&lt;code&gt;npm&lt;/code&gt; or &lt;code&gt;yarn&lt;/code&gt;), and wait a moment while the Nest CLI builds your starter app.&lt;/p&gt;

&lt;p&gt;Your folder structure should look similar to this now:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

nestmod
└─── nestjs-package-starter
│   └───node_modules
│   └───src
│   ...
└─── test-app
│   └───node_modules
│   └───src
│   ...


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Build the package
&lt;/h3&gt;

&lt;p&gt;Take a moment to poke around in the &lt;code&gt;nestjs-package-starter&lt;/code&gt; folder.  Here are a few things to notice:&lt;/p&gt;

&lt;p&gt;1) The &lt;code&gt;src&lt;/code&gt; folder has two files:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;test.ts&lt;/code&gt; is the entire functionality of our package; it exports one simple test function we'll import into our Nest test-app to prove to ourselves that we've actually installed and are using the package.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;index.ts&lt;/code&gt; exports the &lt;code&gt;test.ts&lt;/code&gt; function from the folder (it's a &lt;a href="https://basarat.gitbooks.io/typescript/content/docs/tips/barrel.html" rel="noopener noreferrer"&gt;barrel file&lt;/a&gt;).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;2) The &lt;code&gt;package.json&lt;/code&gt; file has a number of interesting parts.  We'll delve into several of these throughout the tutorial. One to notice now is the different types of package dependencies that are apparent:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;regular &lt;code&gt;dependencies&lt;/code&gt; are those required to run our code; these should be anything &lt;em&gt;in addition to, but not including&lt;/em&gt; NestJS itself.  For example, if we were using the &lt;code&gt;dotenv&lt;/code&gt; package in our code, this is something that is not provided by NestJS itself, so it would belong in &lt;code&gt;dependencies&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;peerDependencies&lt;/code&gt;: since this is a NestJS-based package, we use this section to declare that it is &lt;em&gt;compatible&lt;/em&gt; with Nest version 6 (a minimum of 6.0.0).  This means we assume that the person using the package has a compatible NestJS environment that we can rely on.  Mentioning just the &lt;code&gt;@nestjs/common&lt;/code&gt; package is sufficient for this purpose.  Specifying a version of &lt;code&gt;^6.0.0&lt;/code&gt; gives us broad compatibility with any minor versions of NestJS the user may have installed.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;devDependencies&lt;/code&gt;: these are the packages we need &lt;em&gt;only&lt;/em&gt; for development; since we expect to build a Nest-based package, we need Nest in our development environment, along with testing utilities, TypeScript, etc. These dev dependencies should be pretty much identical to the regular &lt;code&gt;devDependencies&lt;/code&gt; you have when building a normal Nest app.  For example, compare these to the &lt;code&gt;devDependencies&lt;/code&gt; entry in the &lt;code&gt;test-app&lt;/code&gt; and they should be identical.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In terminal window 1, make sure you're still in the folder where you cloned the nestjs-package-starter, and build the package with:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npm run build


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The npm &lt;code&gt;build&lt;/code&gt; script is trivially simple: it just runs the TypeScript compiler in the folder configured as the &lt;code&gt;rootDir&lt;/code&gt; in our &lt;code&gt;tsconfig.json&lt;/code&gt; file.  Speaking of which, let's take a quick look at &lt;code&gt;tsconfig.json&lt;/code&gt;, which kind of "teams up" with &lt;code&gt;package.json&lt;/code&gt; to control how our package is built.&lt;/p&gt;

&lt;p&gt;The most notable things in the &lt;code&gt;tsconfig.json&lt;/code&gt; file are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;declaration: true&lt;/code&gt; ensures that our type files are generated, which helps consumers of the package benefit from TypeScript type checking and editor features like intellisense.&lt;/li&gt;
&lt;li&gt;the two &lt;code&gt;Decorators&lt;/code&gt; flags ensure that TypeScript decorators work properly.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;outDir&lt;/code&gt; controls where our compiled code is published.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;rootdir&lt;/code&gt;, along with the top-level &lt;code&gt;include&lt;/code&gt; and &lt;code&gt;exclude&lt;/code&gt; entries, control which source code is compiled.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Take another quick peek at &lt;code&gt;package.json&lt;/code&gt;, and note the &lt;code&gt;"main": "dist/test.js"&lt;/code&gt; entry.  This is the last piece of the "packaging puzzle".  The &lt;code&gt;build&lt;/code&gt; script compiles our TypeScript into JavaScript in the &lt;code&gt;dist&lt;/code&gt; folder; the barrel file (&lt;code&gt;index.ts&lt;/code&gt;) ensures that our function is exported and publicly visible; the &lt;code&gt;"main"&lt;/code&gt; entry in our &lt;code&gt;package.json&lt;/code&gt; tells the module loader where to discover the exported symbols from our package when it's imported.&lt;/p&gt;

&lt;p&gt;So we've now compiled the TypeScript and prepared it for deployment in a package.&lt;/p&gt;

&lt;h3&gt;
  
  
  Install the package into the test app
&lt;/h3&gt;

&lt;p&gt;You now have a full-fledged npm package, though it's only available locally. You can &lt;strong&gt;use&lt;/strong&gt; the package in &lt;code&gt;test-app&lt;/code&gt; with a familiar-looking &lt;code&gt;npm&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;In terminal window 2, change to the folder created by the &lt;code&gt;nest new test-app&lt;/code&gt; command, where our test app lives.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nb"&gt;cd &lt;/span&gt;nestmod/test-app


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Use &lt;code&gt;npm&lt;/code&gt; to install the package we just built into our test app.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npm &lt;span class="nb"&gt;install&lt;/span&gt; ../nestjs-package-starter


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The main difference from a normal &lt;code&gt;npm install&lt;/code&gt;, I'm sure you've noticed, is that you are referring to a combination of a path (the &lt;code&gt;..&lt;/code&gt; part) and a name (the &lt;code&gt;nestjs-package-starter&lt;/code&gt; part), rather than the typical usage (when pulling from the npmjs.com registry) which uses just a package name.  This illustrates the simple process enabled by npm for developing packages: &lt;code&gt;npm install&lt;/code&gt; works &lt;em&gt;exactly the same&lt;/em&gt; for local packages as it does for remote packages downloaded from npmjs.com. You can see this clearly by noticing that in &lt;code&gt;testapp/package.json&lt;/code&gt;, there's an entry like:&lt;br&gt;
&lt;code&gt;"@nestjsplus/nestjs-package-starter": "file:../nestjs-package-starter"&lt;/code&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Use the package in the test app
&lt;/h3&gt;

&lt;p&gt;The template package exports a single simple test function.  Examine &lt;code&gt;nestjs-package-starter/src/test.ts&lt;/code&gt; to see it:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// nestjs-package-starter/src/test.ts&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;getHello&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Hello from the new package!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now that you've installed the new package in &lt;code&gt;test-app&lt;/code&gt;, it's available like any npm package, and you can use it in the normal fashion.  Open &lt;code&gt;test-app/src/app.controller.ts&lt;/code&gt; and import the function; make sure the file looks like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// test-app/src/app.controller.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Get&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;getHello&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjsplus/nestjs-package-starter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nf"&gt;getHello&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;getHello&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In terminal window 2, start &lt;code&gt;test-app&lt;/code&gt; (using &lt;code&gt;start:dev&lt;/code&gt; is recommended here so we can make iterative changes):&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npm run start:dev


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In a browser, head on over to &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt; and see the message coming from the imported package function.&lt;/p&gt;

&lt;p&gt;If you've followed along so far, let's do one more thing that shows how easy it is to continue to iterate.  Make a simple change to the &lt;code&gt;getHello()&lt;/code&gt; function exported by the package (I like to change it to return &lt;code&gt;'Buon Giorno!'&lt;/code&gt; 😄).&lt;/p&gt;

&lt;p&gt;Now, make sure the test app is still running in dev mode (e.g., &lt;code&gt;npm run start:dev&lt;/code&gt;) in terminal window 2, then, in terminal window 1, rebuild the package with:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npm run build


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Notice that in terminal window 2, since we're linked to the local package, the dev server will automatically restart as the package is rebuilt (it may show errors for a few seconds while the process completes).  This highlights a key thing to keep in mind: whenever you make local changes to the package, you must rebuild it &lt;strong&gt;before it's visible to consumers of the package&lt;/strong&gt;.  Obviously this means you must rebuild &lt;strong&gt;before you publish it&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;To make sure this last bit happens, take another look at the &lt;code&gt;package.json&lt;/code&gt; file.  The &lt;code&gt;prepare&lt;/code&gt; script is a special script recognized by the &lt;code&gt;npm publish&lt;/code&gt; command.  Whenever you publish (which we'll do in a moment), the &lt;code&gt;prepare&lt;/code&gt; script will be run before publishing.  Note that there are several other such npm "hooks" available that will let you do things like run &lt;code&gt;lint&lt;/code&gt;, check in/tag source code to your git repo, etc.  &lt;a href="https://docs.npmjs.com/misc/scripts" rel="noopener noreferrer"&gt;See here for details&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;When you refresh your page at &lt;code&gt;localhost:3000&lt;/code&gt;, you should see your nice warm Italian greeting!&lt;/p&gt;

&lt;h3&gt;
  
  
  Publish the package
&lt;/h3&gt;

&lt;p&gt;We're now ready to push our newborn package out of the nest and onto the 'net (ugghh, sorry for the bad pun). To complete this step, you'll need a free account at &lt;a href="https://www.npmjs.com" rel="noopener noreferrer"&gt;www.npmjs.com&lt;/a&gt;. If you don't have one, go to the &lt;a href="https://www.npmjs.com/signup" rel="noopener noreferrer"&gt;signup page&lt;/a&gt; to get one.&lt;/p&gt;

&lt;h4&gt;
  
  
  Give the package a unique scoped name for npm
&lt;/h4&gt;

&lt;h5&gt;
  
  
  A few notes about npm
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;npm names&lt;br&gt;
All npm packages have a name.  If the name starts with &lt;code&gt;@&lt;/code&gt;, it's a &lt;em&gt;scoped&lt;/em&gt; name. Scoped names are useful to create a unique namespace for your packages.  If you and I both publish &lt;code&gt;nestjs-package-starter&lt;/code&gt; to npmjs.org, there will be a collision. Whoever publishes first &lt;em&gt;owns&lt;/em&gt; the name.  But if I publish a &lt;em&gt;scoped&lt;/em&gt; package name, such as &lt;code&gt;@johnbiundo/nestjs-package-starter&lt;/code&gt;, you can publish your own copy at &lt;code&gt;@yournpmjsname/nestjs-package-starter&lt;/code&gt;, and they both happily co-exist.  Anyone that wants mine will do &lt;code&gt;npm install @johnbiundo/nestjs-package-starter&lt;/code&gt;, and anyone that wants yours will do &lt;code&gt;npm install @yournpmjsname/nestjs-package-starter&lt;/code&gt;. Read &lt;a href="https://docs.npmjs.com/misc/scope" rel="noopener noreferrer"&gt;more about scopes here&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;npm public packages&lt;br&gt;
npm lets you publish packages for free as long as they're public.  If you want to publish private packages (say for sharing only within your company), you'll need a private account.  Alternatively, you can set up an internal private registry with a registry server like &lt;a href="https://github.com/verdaccio/verdaccio" rel="noopener noreferrer"&gt;verdaccio&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;npm orgs&lt;br&gt;
In addition to publishing packages scoped under your name, you can create a free npm &lt;em&gt;org&lt;/em&gt; to give your packages a more custom name (scope).  For example, I publish several packages under the org &lt;code&gt;@nestjsplus&lt;/code&gt;.  Read &lt;a href="https://docs.npmjs.com/orgs/" rel="noopener noreferrer"&gt;more about npm orgs here&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once you're ready, open the &lt;code&gt;package.json&lt;/code&gt; file for the package and change the following entries:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;name&lt;/em&gt;: your scoped package name, something like &lt;code&gt;@mynpmjsname/my-new-package&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;version&lt;/em&gt;: choose a starting version number; I comment a bit more about this below, but something like &lt;code&gt;1.0.0&lt;/code&gt; will work fine for now.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;author&lt;/em&gt;: your name and email, in a format like &lt;code&gt;John Biundo &amp;lt;john@email.com&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Later, you'll want to customize other things like the description, license, keywords, repository links, etc.  None of those are very TypeScript or NestJS-specific, so we won't cover them here, but a little Googling will turn up lots of good articles and help for them.&lt;/p&gt;

&lt;h4&gt;
  
  
  Once ready, publish the package
&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; Each time you re-publish the package, you'll have to change the &lt;code&gt;version&lt;/code&gt; field in &lt;code&gt;package.json&lt;/code&gt; (or the publish step will fail).  It's best practice to use &lt;a href="https://semver.org/" rel="noopener noreferrer"&gt;semantic versioning ("semver")&lt;/a&gt; for npm packages.&lt;/p&gt;

&lt;p&gt;In terminal window 1, run:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npm publish


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The output of this step shows you exactly what files are bundled up and sent to npmjs.org to create your package, as well as the version number and some other metadata.&lt;/p&gt;

&lt;h4&gt;
  
  
  Search for your package on &lt;a href="http://www.npmjs.com" rel="noopener noreferrer"&gt;www.npmjs.com&lt;/a&gt;
&lt;/h4&gt;

&lt;p&gt;Head on over to &lt;a href="https://www.npmjs.org" rel="noopener noreferrer"&gt;https://www.npmjs.org&lt;/a&gt;, and search for your package.&lt;/p&gt;




&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Flf35il3cylwrewxskvm0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fthepracticaldev.s3.amazonaws.com%2Fi%2Flf35il3cylwrewxskvm0.png" alt="search-npmjs"&gt;&lt;/a&gt;&lt;/p&gt;



&lt;h3&gt;
  
  
  Install the published npm package in your test app
&lt;/h3&gt;

&lt;p&gt;Now that the package is live on npm, you can install it as you would any package.&lt;/p&gt;

&lt;p&gt;In terminal window 2, first, uninstall the local one we created and installed about 10 minutes ago.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npm uninstall @nestjsplus/nestjs-package-starter


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, install your shiny new npm package from the internet.  Change the names as appropriate below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npm &lt;span class="nb"&gt;install&lt;/span&gt; @yournpmjsname/your-package


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Finally, edit your test app to reflect the new npm package name (paying attention to using the correct "scoped" name if you published that way).  For example:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/app.controller.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Get&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;getHello&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@yournpmjsname/your-new-package&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nf"&gt;getHello&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;getHello&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Fire up the app, and you'll be using your new package!&lt;/p&gt;

&lt;p&gt;Now that you know how easy it is to publish an npm package, stay tuned for the next articles in this series where we'll cover building more interesting NestJS modules.&lt;/p&gt;

&lt;p&gt;Feel free to ask questions, make comments or suggestions, or just say hello in the comments below.  And join us at &lt;a href="https://discord.gg/G7Qnnhy" rel="noopener noreferrer"&gt;Discord&lt;/a&gt; for more happy discussions about NestJS.  I post there as &lt;em&gt;Y Prospect&lt;/em&gt;.&lt;/p&gt;

</description>
      <category>nestjs</category>
      <category>npm</category>
      <category>package</category>
      <category>nest</category>
    </item>
    <item>
      <title>Authentication and Sessions for MVC Apps with NestJS</title>
      <dc:creator>John Biundo</dc:creator>
      <pubDate>Mon, 22 Jul 2019 14:29:22 +0000</pubDate>
      <link>https://forem.com/nestjs/authentication-and-sessions-for-mvc-apps-with-nestjs-55a4</link>
      <guid>https://forem.com/nestjs/authentication-and-sessions-for-mvc-apps-with-nestjs-55a4</guid>
      <description>&lt;p&gt;&lt;em&gt;John is a member of the NestJS core team, primarily contributing to the documentation.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Note:&lt;/strong&gt; You can find all of the source code from this article &lt;a href="https://github.com/johnbiundo/mvc-sessions" rel="noopener noreferrer"&gt;on github here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Intro
&lt;/h3&gt;

&lt;p&gt;There's a lot of good information on building API servers in &lt;a href="https://github.com/nestjs/nest" rel="noopener noreferrer"&gt;NestJS&lt;/a&gt; (AKA Nest).  That use case is certainly a very strong fit for Nest.&lt;/p&gt;

&lt;p&gt;Perhaps surprisingly, Nest is also very good at building traditional web applications - what Nest refers to as &lt;a href="https://docs.nestjs.com/techniques/mvc" rel="noopener noreferrer"&gt;MVC apps in the documentation&lt;/a&gt;.  By this I mean apps that you might otherwise consider building in ExpressJS + Handlebars, Laravel, or Ruby on Rails, with template-driven server-side rendering of pages.  Users considering Nest for this use case might find the resources in this area to be a little lighter than the API server use case.&lt;/p&gt;

&lt;p&gt;This is probably partially due to the fact that most of the information you need to build say, an ExpressJS + Handlebars app, is already well covered and easy to find.  And most of that information applies directly to building such an app in Nest.  But leaving it at that still leaves a few questions for the Nest newcomer to sort out on their own.  This article attempts to provide a little more color on one aspect of the challenge: how to manage session-based authentication with Nest.  Why is this different with Nest?&lt;/p&gt;

&lt;p&gt;Fully answering that question probably needs one or more complete articles on its own. The short answer is: if you really want to take advantage of Nest, it helps to try to learn to do things "the Nest way".  Nest makes no apologies for being opinionated.  It has a certain way of approaching application architecture.  If you're on-board with this approach, it helps to go all-in and try to build things the Nest way. This gives you amazing productivity advantages, helps you build &lt;a href="https://en.wikipedia.org/wiki/Don%27t_repeat_yourself" rel="noopener noreferrer"&gt;DRY&lt;/a&gt; maintainable code, and lets you spend most of your time focusing on business logic, not boilerplate.  &lt;/p&gt;

&lt;p&gt;The Nest way will be familiar to many coming from a Java or .NET background, as many of the concepts - such as OO and dependency injection - are similar.  But it should be equally accessible to anyone who has a background in &lt;code&gt;TypeScript&lt;/code&gt;, &lt;code&gt;Angular&lt;/code&gt;, or any modern FE or BE framework/enviroment.  As with any such framework, there's plenty of room for individual choice and style, but following the general patterns of the framework can make you feel like you have the wind at your back instead of swimming against the tide.&lt;/p&gt;

&lt;h3&gt;
  
  
  Requirements
&lt;/h3&gt;

&lt;p&gt;Phew!  With that out of the way, let's get a little more specific.  When it comes to the problem at hand, there are only a few Nest-specific techniques that you need to know to get a quick jump out of the starting blocks:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How to set up a template engine like &lt;a href="https://github.com/ericf/express-handlebars" rel="noopener noreferrer"&gt;Handlebars&lt;/a&gt; to work with Nest&lt;/li&gt;
&lt;li&gt;How to integrate template rendering with Nest controllers&lt;/li&gt;
&lt;li&gt;How to integrate &lt;a href="https://github.com/expressjs/session" rel="noopener noreferrer"&gt;express-session&lt;/a&gt; with Nest&lt;/li&gt;
&lt;li&gt;How to integrate an authentication system (we'll specifically use &lt;a href="http://www.passportjs.org/" rel="noopener noreferrer"&gt;Passport&lt;/a&gt;) with Nest&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Items 3 and 4 are the main focus of this article.  To get there, we'll wade slightly into items 1 and 2 (providing code samples and references to Nest docs that can help you go deeper), but we won't spend a lot of time there.  Let me know in the comments if these are areas you'd like to see more about in future articles.&lt;/p&gt;

&lt;p&gt;Let's take a quick look at the app we're going to build.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqk8u67dib2fg8ujap5pz.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqk8u67dib2fg8ujap5pz.gif" alt="App"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Our requirements are simple.  Users will authenticate with a username and password. Once authenticated, the server will use Express sessions so that the user remains "logged in" until they choose to log out. We'll set up a protected route that is accessible only to an authenticated user.&lt;/p&gt;

&lt;h3&gt;
  
  
  Installation
&lt;/h3&gt;

&lt;p&gt;If you haven't installed Nest before, the pre-requisites are simple.  You just need a modern Node.js environment (&amp;gt;=8.9 will do) and &lt;code&gt;npm&lt;/code&gt; or &lt;code&gt;yarn&lt;/code&gt;.  Then install Nest:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

npm i &lt;span class="nt"&gt;-g&lt;/span&gt; @nestjs/cli


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We start by installing the required packages, and building our basic routes.&lt;/p&gt;

&lt;p&gt;Passport provides a library called &lt;a href="https://github.com/jaredhanson/passport-local" rel="noopener noreferrer"&gt;passport-local&lt;/a&gt; that implements a username/password &lt;em&gt;authentication strategy&lt;/em&gt;, which suits our needs for this use case. Since we are rendering some basic HTML pages, we'll also install the versatile and popular &lt;a href="https://github.com/ericf/express-handlebars" rel="noopener noreferrer"&gt;express-handlebars&lt;/a&gt; package to make that a little easier.  To support sessions and to provide a convenient way to give user feedback during login, we'll also utilize the &lt;code&gt;express-session&lt;/code&gt; and &lt;code&gt;connect-flash&lt;/code&gt; packages. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt; For &lt;strong&gt;any&lt;/strong&gt; Passport strategy you choose (there are many available &lt;a href="http://www.passportjs.org" rel="noopener noreferrer"&gt;here&lt;/a&gt;), you'll always need the &lt;code&gt;@nestjs/passport&lt;/code&gt; and &lt;code&gt;passport&lt;/code&gt; packages. Then, you'll need to install the strategy-specific package (e.g., &lt;code&gt;passport-jwt&lt;/code&gt; or &lt;code&gt;passport-local&lt;/code&gt;) that scaffolds the particular authentication strategy you are building.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;With these basic requirements in mind, we can now start by scaffolding a new Nest application, and installing the dependencies:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

nest new mvc-sessions
&lt;span class="nb"&gt;cd &lt;/span&gt;mvc-sessions
npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save&lt;/span&gt; @nestjs/passport passport passport-local express-handlebars express-session connect-flash 
npm &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="nt"&gt;--save-dev&lt;/span&gt; @types/express @types/express-session @types/connect-flash @types/express-handlebars


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Web interface
&lt;/h3&gt;

&lt;p&gt;Let's start by building the templates we'll use for the UI of our authentication subsystem.  Following a standard MVC type project structure, create the following folder structure (i.e., the &lt;code&gt;public&lt;/code&gt; folder and its sub-folders):&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

mvc-sessions
└───public
│   └───views
│       └───layouts


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now create the following handlebars templates, and configure Nest to use express-handlebars as the view engine.  Refer &lt;a href="https://handlebarsjs.com/" rel="noopener noreferrer"&gt;here for more on the handlebars template language&lt;/a&gt; , and &lt;a href="https://docs.nestjs.com/techniques/mvc" rel="noopener noreferrer"&gt;here for more background on Nest-specific techniques&lt;/a&gt;  for server-side rendered (MVC style) web apps.&lt;/p&gt;

&lt;h4&gt;
  
  
  Main layout
&lt;/h4&gt;

&lt;p&gt;Create &lt;code&gt;main.hbs&lt;/code&gt; in the layouts folder, and add the following code.  This is the outermost container for our views.  Note the &lt;code&gt;{{{ body }}}&lt;/code&gt; line, which is where each individual view is inserted.  This structure allows us to set up global styles.  In this case, we're taking advantage of Google's widely used &lt;a href="https://github.com/google/material-design-lite" rel="noopener noreferrer"&gt;material design lite&lt;/a&gt; component library to style our minimal UI. All of those dependencies are taken care of in the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; section of our layout.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="c"&gt;&amp;lt;!-- public/views/layouts/main.hbs --&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;&amp;lt;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;script &lt;/span&gt;&lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"https://code.getmdl.io/1.3.0/material.min.js"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/script&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://fonts.googleapis.com/icon?family=Material+Icons"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel=&lt;/span&gt;&lt;span class="s"&gt;"stylesheet"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;"https://code.getmdl.io/1.3.0/material.indigo-pink.min.css"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
      &lt;span class="nc"&gt;.mdl-layout__content&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;24px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;flex&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;none&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nc"&gt;.mdl-textfield__error&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;visibility&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;visible&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;padding&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;5px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="nc"&gt;.mdl-card&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nl"&gt;padding-bottom&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;10px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="nl"&gt;min-width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="m"&gt;500px&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    {{{ body }}}
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  Home page
&lt;/h4&gt;

&lt;p&gt;Create &lt;code&gt;home.hbs&lt;/code&gt; in the &lt;code&gt;views&lt;/code&gt; folder, and add the following code.  This is the page users land on after authenticating.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="c"&gt;&amp;lt;!-- public/views/home.hbs --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-layout mdl-js-layout mdl-color--grey-100"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;main&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-layout__content"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-card mdl-shadow--6dp"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-card__title mdl-color--primary mdl-color-text--white"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;h2&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-card__title-text"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Welcome {{ user.username }}!&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-card__supporting-text"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-card__actions mdl-card--border"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-button"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;'/profile'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;GetProfile&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/main&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  Login page
&lt;/h4&gt;

&lt;p&gt;Create &lt;code&gt;login.hbs&lt;/code&gt; in the &lt;code&gt;views&lt;/code&gt; folder, and add the following code.  This is the login form.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="c"&gt;&amp;lt;!-- public/views/login.hbs --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-layout mdl-js-layout mdl-color--grey-100"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;main&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-layout__content"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-card mdl-shadow--6dp"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-card__title mdl-color--primary mdl-color-text--white"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;h2&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-card__title-text"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Nest Cats&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-card__supporting-text"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;form&lt;/span&gt; &lt;span class="na"&gt;action=&lt;/span&gt;&lt;span class="s"&gt;"/login"&lt;/span&gt; &lt;span class="na"&gt;method=&lt;/span&gt;&lt;span class="s"&gt;"post"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-textfield mdl-js-textfield"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-textfield__input"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"text"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"username"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"username"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-textfield__label"&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"username"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Username&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-textfield mdl-js-textfield"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;input&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-textfield__input"&lt;/span&gt; &lt;span class="na"&gt;type=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt; &lt;span class="na"&gt;name=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;label&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-textfield__label"&lt;/span&gt; &lt;span class="na"&gt;for=&lt;/span&gt;&lt;span class="s"&gt;"password"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Password&lt;span class="nt"&gt;&amp;lt;/label&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-card__actions mdl-card--border"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;button&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-button mdl-button--colored mdl-js-button mdl-js-ripple-effect"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Log In&lt;span class="nt"&gt;&amp;lt;/button&amp;gt;&lt;/span&gt;
            &lt;span class="nt"&gt;&amp;lt;span&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-textfield__error"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;{{ message }}&lt;span class="nt"&gt;&amp;lt;/span&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/form&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/main&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  Profile page
&lt;/h4&gt;

&lt;p&gt;Create &lt;code&gt;profile.hbs&lt;/code&gt; in the &lt;code&gt;views&lt;/code&gt; folder and add the following code.  This page displays details about the logged in user.  It's rendered on our protected route.&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;

&lt;span class="c"&gt;&amp;lt;!-- public/views/profile.hbs --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-layout mdl-js-layout mdl-color--grey-100"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;main&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-layout__content"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-card mdl-shadow--6dp"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-card__title mdl-color--primary mdl-color-text--white"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;h2&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-card__title-text"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;About {{ user.username }}&lt;span class="nt"&gt;&amp;lt;/h2&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;figure&amp;gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;src=&lt;/span&gt;&lt;span class="s"&gt;"http://lorempixel.com/400/200/cats/{{ user.pet.picId }}"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;figcaption&amp;gt;&lt;/span&gt;{{ user.username }}'s friend {{ user.pet.name }}&lt;span class="nt"&gt;&amp;lt;/figcaption&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/figure&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-card__actions mdl-card--border"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="nt"&gt;&amp;lt;a&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"mdl-button"&lt;/span&gt; &lt;span class="na"&gt;href=&lt;/span&gt;&lt;span class="s"&gt;'/logout'&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;Log Out&lt;span class="nt"&gt;&amp;lt;/a&amp;gt;&lt;/span&gt;
        &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/main&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h4&gt;
  
  
  Set up view engine
&lt;/h4&gt;

&lt;p&gt;Now we tell Nest to use express-handlebars as the view engine.  We do this in the &lt;code&gt;main.ts&lt;/code&gt; file, which by convention is where your Nest app will be bootstrapped from.  While we won't go into a lot of detail here, the main concepts are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pass the &lt;code&gt;&amp;lt;NestExpressApplication&amp;gt;&lt;/code&gt; type assertion in the &lt;code&gt;NestFactory.create()&lt;/code&gt;  method call to gain access to native Express methods.&lt;/li&gt;
&lt;li&gt;Having done that, you can now simply access any of Express's native &lt;code&gt;app&lt;/code&gt; methods.  For the most part, the configuration of the view engine, and any other traditional Express middleware, proceeds as normal as with the view engine setup shown below.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Modify the &lt;code&gt;main.ts&lt;/code&gt; file so that it looks like this:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/main.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;NestFactory&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;NestExpressApplication&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/platform-express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;join&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;path&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AppModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app.module&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;exphbs&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express-handlebars&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;bootstrap&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;NestFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;NestExpressApplication&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AppModule&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;viewsPath&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../public/views&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;engine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.hbs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;exphbs&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;extname&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.hbs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;defaultLayout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;main&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}));&lt;/span&gt;
  &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;views&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;viewsPath&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;view engine&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.hbs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;bootstrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Authentication routes
&lt;/h3&gt;

&lt;p&gt;The final step in this section is setting up our routes.  This is one area where the MVC architecture starts to shine through.  It can be a little unfamiliar if you're used to bare Express routes, so let's wade in a little.  Feel free to take a few minutes to read the &lt;a href="https://docs.nestjs.com/controllers" rel="noopener noreferrer"&gt;Nest documentation on controllers&lt;/a&gt; for lots more details on how Nest Controllers work.&lt;/p&gt;

&lt;p&gt;We'll modify &lt;code&gt;app.controller.ts&lt;/code&gt; so that it looks like the large code block below.  &lt;/p&gt;

&lt;p&gt;Before we do that, let's take a moment to look at the &lt;code&gt;GET /&lt;/code&gt; route handler in the large code block below (the line starting with &lt;code&gt;@Get('/')&lt;/code&gt;.  The work is done by the &lt;code&gt;@Render()&lt;/code&gt; decorator.  Since all we're doing in this first iteration of the code is rendering our templates, the code is trivial.  &lt;code&gt;@Render()&lt;/code&gt; takes a single argument, which is the name of the template to render.  Think of this as the Nest equivalent of Express code like:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;login&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Methods decorated with &lt;code&gt;@Render()&lt;/code&gt; can also return a value that will supply template variables.  For now, we'll just do an empty return (implicitly returning &lt;code&gt;undefined&lt;/code&gt;).  Later, we'll use this feature to pass template variables.  &lt;/p&gt;

&lt;p&gt;Go ahead and update &lt;code&gt;src/app.controller.ts&lt;/code&gt; with this code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/app.controller.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Render&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;login&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/login&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Res&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/home&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/home&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;home&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;getHome&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/profile&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;profile&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;getProfile&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/logout&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;logout&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Res&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;At this point, you should be able to run the app:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

&lt;span class="nv"&gt;$ &lt;/span&gt;npm run start


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, browse to &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt; and click through the basic UI.  At this point, of course, you can click through the pages without logging in.&lt;/p&gt;

&lt;h3&gt;
  
  
  Implementing Passport strategies
&lt;/h3&gt;

&lt;p&gt;We're now ready to implement the authentication feature. It's helpful to have a good understanding of how Nest integrates with Passport.  The Nest docs &lt;a href="https://docs.nestjs.com/techniques/authentication#implementing-passport-strategies" rel="noopener noreferrer"&gt;covers this in some depth&lt;/a&gt;.  It's worth taking a quick detour to read that section.  &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Note: I authored that section of the NestJS docs, so you'll see some similarities/overlaps with the code and documentation below&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The key takeaways are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Nest provides the &lt;code&gt;@nestjs/passport&lt;/code&gt; module which &lt;em&gt;wraps&lt;/em&gt; Passport in a Nest style package that makes it easy to treat Passport as a &lt;a href="https://docs.nestjs.com/providers" rel="noopener noreferrer"&gt;&lt;em&gt;provider&lt;/em&gt;&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;You implement Passport &lt;em&gt;strategies&lt;/em&gt; by extending the &lt;code&gt;PassportStrategy&lt;/code&gt; class, where you implement the strategy-specific initialization and callback.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;As mentioned, we'll utilize the passport-local strategy for this use case.  We'll get to that implementation in a moment.  Start by generating an &lt;code&gt;AuthModule&lt;/code&gt; and in it, an &lt;code&gt;AuthService&lt;/code&gt;:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

nest g module auth
nest g service auth


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We'll also implement a &lt;code&gt;UsersService&lt;/code&gt; to manage our &lt;em&gt;User store&lt;/em&gt;, so we'll generate that module and service now:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;

nest g module &lt;span class="nb"&gt;users
&lt;/span&gt;nest g service &lt;span class="nb"&gt;users&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Replace the default contents of these generated files as shown below.  For our sample app, the &lt;code&gt;UsersService&lt;/code&gt; simply maintains a hard-coded in-memory list of users, and a method to retrieve one by username.  In a real app, this is where you'd build your user model and persistence layer, using your library of choice (e.g., TypeORM, Sequelize, Mongoose, etc.).&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/users/users.service.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Injectable&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Injectable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UsersService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;[];&lt;/span&gt;

  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;john&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;changeme&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;pet&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;alfred&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;picId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;chris&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;secret&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;pet&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;gopher&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;picId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;maria&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;guess&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="na"&gt;pet&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;jenny&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;picId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
    &lt;span class="p"&gt;];&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;findOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In the &lt;code&gt;UsersModule&lt;/code&gt;, the only change is to add the &lt;code&gt;UsersService&lt;/code&gt; to the exports array of the &lt;code&gt;@Module&lt;/code&gt; decorator so that it is visible outside this module (we'll soon use it in our &lt;code&gt;AuthService&lt;/code&gt;).&lt;br&gt;&lt;br&gt;
You can &lt;a href="https://docs.nestjs.com/modules" rel="noopener noreferrer"&gt;read more here about how Nest uses modules&lt;/a&gt; to organize code and to understand more about the &lt;code&gt;exports&lt;/code&gt; array and other parameters of the &lt;code&gt;@Module()&lt;/code&gt; decorator.&lt;/p&gt;

&lt;p&gt;Make sure &lt;code&gt;src/users/users.module.ts&lt;/code&gt; looks like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/users/users.module.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Module&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;UsersService&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./users.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;UsersService&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;exports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;UsersService&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;UsersModule&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Our &lt;code&gt;AuthService&lt;/code&gt; has the job of retrieving a user and verifying the password.  Replace the default contents of the &lt;code&gt;src/auth/auth.service.ts&lt;/code&gt; file with the code below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/auth/auth.service.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Injectable&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;UsersService&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../users/users.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Injectable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AuthService&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;usersService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;UsersService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;validateUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;pass&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;usersService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;findOne&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;password&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="nx"&gt;pass&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
      &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Warning&lt;/strong&gt; Of course in a real application, you wouldn't store a password in plain text. You'd instead use a library like &lt;a href="https://github.com/kelektiv/node.bcrypt.js#readme" rel="noopener noreferrer"&gt;bcrypt&lt;/a&gt;, with a salted one-way hash algorithm. With that approach, you'd only store hashed passwords, and then compare the stored password to a hashed version of the &lt;strong&gt;incoming&lt;/strong&gt; password, thus never storing or exposing user passwords in plain text. To keep our sample app simple, we violate that absolute mandate and use plain text.  &lt;strong&gt;Don't do this in your real app!&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We'll call into our &lt;code&gt;validateUser()&lt;/code&gt; method from our passport-local strategy subclass in a moment. The Passport library expects us to return a full user if the validation succeeds, or a null if it fails (failure is defined as either the user is not found, or the password does not match). In our code, we use a convenient ES6 spread operator to strip the password property from the user object before returning it. Upon successful validation, Passport then takes care of a few details for us, which we'll explore later on in the Sessions section.&lt;/p&gt;

&lt;p&gt;And finally, we update our &lt;code&gt;AuthModule&lt;/code&gt; to import the &lt;code&gt;UsersModule&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/auth/auth.module.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Module&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AuthService&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./auth.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;UsersModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../users/users.module&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;UsersModule&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;AuthService&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AuthModule&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Our app will function now, but remains incomplete until we finish a few more steps.  You can restart the app and navigate to &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt; and still move around without logging in (after all, we haven't implemented our passport-local strategy yet.  We'll get there momentarily). &lt;/p&gt;

&lt;h4&gt;
  
  
  Implementing Passport local
&lt;/h4&gt;

&lt;p&gt;Now we can implement our passport-local strategy.  Create a file called &lt;code&gt;local.strategy.ts&lt;/code&gt; in the &lt;code&gt;auth&lt;/code&gt; folder, and add the following code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/auth/local.strategy.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Strategy&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;passport-local&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PassportStrategy&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/passport&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Injectable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;UnauthorizedException&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AuthService&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./auth.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Injectable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LocalStrategy&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;PassportStrategy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;Strategy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;constructor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;authService&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;AuthService&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;validate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;authService&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;validateUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;password&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;UnauthorizedException&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We've followed the recipe described in the &lt;a href="https://docs.nestjs.com/techniques/authentication#implementing-passport-strategies" rel="noopener noreferrer"&gt;NestJS Authentication Chapter&lt;/a&gt;.  In this use case with passport-local, there are no configuration options, so our constructor simply calls &lt;code&gt;super()&lt;/code&gt;, without an options object.&lt;/p&gt;

&lt;p&gt;We've also implemented the &lt;code&gt;validate()&lt;/code&gt; method. For the local-strategy, Passport expects a &lt;code&gt;validate()&lt;/code&gt; method with a signature like&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

validate(username: string, password:string): any


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Most of the work is done in our &lt;code&gt;AuthService&lt;/code&gt; (and in turn, in our &lt;code&gt;UserService&lt;/code&gt;), so this method is quite straightforward. The &lt;code&gt;validate()&lt;/code&gt; method for &lt;strong&gt;any&lt;/strong&gt; Passport strategy will follow a similar pattern.  If a user is found and valid, it's returned so request handling can continue, and Passport can do some further housekeeping.  If it's not found, we throw an exception and let &lt;a href="https://docs.nestjs.com/exceptions" rel="noopener noreferrer"&gt;Nest's exceptions layer&lt;/a&gt; handle it.&lt;/p&gt;

&lt;p&gt;With the strategy in place, we have a few more tasks to complete:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Create guards used to decorate routes so that the configured Passport strategy is invoked&lt;/li&gt;
&lt;li&gt;Add &lt;code&gt;@UseGuards()&lt;/code&gt; decorators as needed&lt;/li&gt;
&lt;li&gt;Implement sessions so that users stay logged in across requests&lt;/li&gt;
&lt;li&gt;Configure Nest to use Passport and session-related features&lt;/li&gt;
&lt;li&gt;Add a little polish to the user experience&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's get started.  For the following sections, we'll want to adhere to a best practice project structure, so start by creating a few more folders.  Under &lt;code&gt;src&lt;/code&gt;, create a &lt;code&gt;common&lt;/code&gt; folder.  Inside &lt;code&gt;common&lt;/code&gt;, create &lt;code&gt;filters&lt;/code&gt; and &lt;code&gt;guards&lt;/code&gt; folders.  Our project structure now looks like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

mvc-sessions
└───src
│   └───auth
│   └───common
│       └───filters
│       └───guards
│   └───users
└───public


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Implement guards
&lt;/h3&gt;

&lt;p&gt;The NestJS &lt;a href="https://docs.nestjs.com/guards" rel="noopener noreferrer"&gt;Guards chapter&lt;/a&gt; describes the primary function of guards: to determine whether a request will be handled by the route handler or not.  That remains true, and we'll use that feature soon.  However, in the context of using the &lt;code&gt;@nestjs/passport&lt;/code&gt; module, we will also introduce a slight new wrinkle that may at first be confusing, so let's discuss that now. Once again, the NestJS Authentication chapter has a &lt;a href="https://docs.nestjs.com/techniques/authentication#built-in-passport-guards" rel="noopener noreferrer"&gt;section which describes this scenario&lt;/a&gt; which would be good to read now.  The key takeaways are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Passport supports authentication in two &lt;em&gt;modes&lt;/em&gt;.  First, you need to perform the authentication step (i.e., &lt;em&gt;logging in&lt;/em&gt;)&lt;/li&gt;
&lt;li&gt;Subsequently, you need to &lt;em&gt;verify a user's credentials&lt;/em&gt;.  In the case of passport-local, this means ensuring that the user's session is valid.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Both&lt;/strong&gt; of these steps are implemented via Nest &lt;strong&gt;guards&lt;/strong&gt;.  &lt;/p&gt;
&lt;h4&gt;
  
  
  Initial Authentication
&lt;/h4&gt;

&lt;p&gt;Looking at our UI, it's easy to see that we'll handle this initial authentication step via a &lt;code&gt;POST&lt;/code&gt; request on our &lt;code&gt;/login&lt;/code&gt; route. So how do we invoke the "login phase" of the passport-local strategy in that route? As suggested, the answer is to use a Guard.  Similar to the way we extended the &lt;code&gt;PassportStrategy&lt;/code&gt; class in the last section, we'll start with a default &lt;code&gt;AuthGuard&lt;/code&gt; provided in the &lt;code&gt;@nestjs/passport&lt;/code&gt; package, and extend it as needed. We'll name our new Guard &lt;code&gt;LoginGuard&lt;/code&gt;.  We'll then decorate our &lt;code&gt;POST /login&lt;/code&gt; route with this &lt;code&gt;LoginGuard&lt;/code&gt; to invoke the login phase of our passport-local strategy.&lt;/p&gt;

&lt;p&gt;Create a file called &lt;code&gt;login.guard.ts&lt;/code&gt; in the &lt;code&gt;guards&lt;/code&gt; folder and replace its default contents as follows:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/common/guards/login.guard.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ExecutionContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Injectable&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AuthGuard&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/passport&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Injectable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;LoginGuard&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;AuthGuard&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;local&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;canActivate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ExecutionContext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;canActivate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;switchToHttp&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;getRequest&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;logIn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;There's a lot going on in these few lines of code, so let's walk through it.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Our passport-local strategy has a default name of 'local'.  We reference that name in the &lt;code&gt;extends&lt;/code&gt; clause of the &lt;code&gt;LoginGuard&lt;/code&gt; we are defining in order to tie our custom guard to the code supplied by the &lt;code&gt;passport-local&lt;/code&gt; package. This is needed to disambiguate which class we are extending in case we end up using multiple Passport strategies in our app (each of which may contribute a strategy-specific &lt;code&gt;AuthGuard&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;As with all guards, the primary method we define/override is &lt;code&gt;canActivate()&lt;/code&gt;, which is what we do here.  You can read more about &lt;a href="https://docs.nestjs.com/guards" rel="noopener noreferrer"&gt;guards and customizing the &lt;code&gt;canActivate()&lt;/code&gt; method here&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;The critical part happens in the body of &lt;code&gt;canActivate()&lt;/code&gt; , where we set up an Express session.  Here's what's happening:

&lt;ul&gt;
&lt;li&gt;We call &lt;code&gt;canActivate()&lt;/code&gt; on the super class, as we normally would in extending a class method. Our super class provides the framework for invoking our passport-local strategy.  Recall from the &lt;a href="https://docs.nestjs.com/guards" rel="noopener noreferrer"&gt;Guards&lt;/a&gt; chapter that &lt;code&gt;canActivate()&lt;/code&gt; returns a boolean indicating whether or not the target route will be called.  When we get here, Passport will have run the previously configured strategy (from the super class) and will return a boolean to indicate whether or not the user has successfully authenticated.  Here, we stash the result so we can do a little more processing before finally returning from our method.&lt;/li&gt;
&lt;li&gt;The key step for starting a session is to now invoke the &lt;code&gt;logIn()&lt;/code&gt; method on our super class, passing in the current request.  This actually calls a special method that Passport automatically added to our Express &lt;code&gt;Request&lt;/code&gt; object during the previous step.  See &lt;a href="http://www.passportjs.org/docs/configure/" rel="noopener noreferrer"&gt;here&lt;/a&gt; and &lt;a href="http://www.passportjs.org/docs/login/" rel="noopener noreferrer"&gt;here&lt;/a&gt; for more on Passport sessions and these special methods.&lt;/li&gt;
&lt;li&gt;The Express session has now been set up, and we can return our &lt;code&gt;canActivate()&lt;/code&gt; result, allowing only authenticated users to continue.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Sessions
&lt;/h3&gt;

&lt;p&gt;Now that we've introduced sessions, there's one additional detail we need to take care of.  Sessions are a way of associating a unique user with some server-side state information about that user.  Let's delve briefly into how Passport rides on top of Express sessions to provide some context.&lt;/p&gt;

&lt;p&gt;Passport adds properties to the &lt;code&gt;session&lt;/code&gt; object to keep track of information about the user and their authentication state.  The user details are populated by a &lt;code&gt;serializeUser()&lt;/code&gt; call to the Passport library.  This function is called automatically by Nest with the &lt;code&gt;user&lt;/code&gt; object created in the &lt;code&gt;validate()&lt;/code&gt; method (which we implemented in  &lt;code&gt;src/auth/local.strategy.ts&lt;/code&gt; a few minutes ago).  This approach may feel a little complex at first, but it supports a completely flexible model for how you can manage interactions with your &lt;em&gt;User store&lt;/em&gt;.  In our case, we are simply passing the &lt;code&gt;user&lt;/code&gt; object through untouched.  In advanced scenarios, you may find yourself calling out to your database or caching layer to augment the user object with more information (e.g., roles/permissions).  Unless you're doing something advanced like that, you can usually just use the following boilerplate for the serialization process.&lt;/p&gt;

&lt;p&gt;Create the &lt;code&gt;session.serializer.ts&lt;/code&gt; file in the &lt;code&gt;auth&lt;/code&gt; folder, and add the following code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/auth/session.serializer.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PassportSerializer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/passport&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Injectable&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Injectable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;SessionSerializer&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;PassportSerializer&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;serializeUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;done&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="nf"&gt;deserializeUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;done&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;err&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;done&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We need to configure our &lt;code&gt;AuthModule&lt;/code&gt; to use the Passport features we just defined.  Of course &lt;code&gt;AuthService&lt;/code&gt; and &lt;code&gt;LocalStrategy&lt;/code&gt; make sense as &lt;em&gt;providers&lt;/em&gt; (read &lt;a href="https://docs.nestjs.com/providers" rel="noopener noreferrer"&gt;more about providers here&lt;/a&gt; if you need to).  Note that the &lt;code&gt;SessionSerializer&lt;/code&gt; we just created is also a pluggable provider, and needs to be included in the &lt;code&gt;providers&lt;/code&gt; array. Don't worry if this isn't 100% clear at the moment. Just think of &lt;em&gt;providers&lt;/em&gt; as a generic way to inject customizable services into your application structure (including 3rd party modules you've included).  &lt;/p&gt;

&lt;p&gt;Update &lt;code&gt;auth.module.ts&lt;/code&gt; to look like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/auth/auth.module.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Module&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AuthService&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./auth.service&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;UsersModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../users/users.module&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;PassportModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/passport&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;LocalStrategy&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./local.strategy&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;SessionSerializer&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./session.serializer&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Module&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;imports&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;UsersModule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;PassportModule&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;providers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;AuthService&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;LocalStrategy&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;SessionSerializer&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;})&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AuthModule&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now let's create our &lt;code&gt;AuthenticatedGuard&lt;/code&gt;.  This is a traditional guard, as covered in the NestJS &lt;a href="https://docs.nestjs.com/guards" rel="noopener noreferrer"&gt;Guards chapter&lt;/a&gt;. Its role is simply to protect certain routes. Create the file &lt;code&gt;authenticated.guard.ts&lt;/code&gt; in the &lt;code&gt;guards&lt;/code&gt; folder, and add the following code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/common/guards/authenticated.guard.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ExecutionContext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Injectable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;CanActivate&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Injectable&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AuthenticatedGuard&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;CanActivate&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="nf"&gt;canActivate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ExecutionContext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;switchToHttp&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;getRequest&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;isAuthenticated&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The only thing to point out here is that in order to determine whether a user is authenticated or not, we use the convenient &lt;code&gt;isAuthenticated()&lt;/code&gt; method that Passport has attached to the &lt;code&gt;request&lt;/code&gt; object for us.  Passport will return &lt;code&gt;true&lt;/code&gt; only if the user is authenticated (i.e., has a valid session).&lt;/p&gt;

&lt;h3&gt;
  
  
  Configure Nest to bootstrap features
&lt;/h3&gt;

&lt;p&gt;We can now tell Nest to use the Passport features we've configured.  Update &lt;code&gt;main.ts&lt;/code&gt; to look like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/main.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;NestFactory&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/core&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;NestExpressApplication&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/platform-express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;join&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;path&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AppModule&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./app.module&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;session&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express-session&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;flash&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;require&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;connect-flash&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;exphbs&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express-handlebars&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;passport&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;passport&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;bootstrap&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;NestFactory&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;create&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;NestExpressApplication&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AppModule&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;viewsPath&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;../public/views&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;engine&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.hbs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;exphbs&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="na"&gt;extname&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.hbs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;defaultLayout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;main&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="p"&gt;}));&lt;/span&gt;
  &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;views&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;viewsPath&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;view engine&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;.hbs&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="nf"&gt;session&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="na"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;nest cats&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;resave&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="na"&gt;saveUninitialized&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;passport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;passport&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;session&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;
  &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;flash&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3000&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;bootstrap&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Here, we've added the session and Passport support to our Nest app.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Warning&lt;/strong&gt; As always, be sure to keep secrets out of your source code (&lt;strong&gt;don't put your session secret in the code, as we did here; use environment variables or a config module (such as &lt;a href="https://github.com/nestjsplus/config" rel="noopener noreferrer"&gt;NestJS Config Manager&lt;/a&gt; instead&lt;/strong&gt;).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Note carefully that the order is important (register the session middleware first, then initialize Passport, then configure Passport to use sessions).  We'll see the use of the &lt;code&gt;flash&lt;/code&gt; feature in a few minutes.&lt;/p&gt;

&lt;h4&gt;
  
  
  Add route guards
&lt;/h4&gt;

&lt;p&gt;Now we're ready to start applying these guards to routes.  Update &lt;code&gt;app.controller.ts&lt;/code&gt; to look like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/app.controller.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Render&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;UseGuards&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;LoginGuard&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./common/guards/login.guard&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AuthenticatedGuard&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./common/guards/authenticated.guard&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;login&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;index&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;UseGuards&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;LoginGuard&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/login&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Res&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/home&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;UseGuards&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AuthenticatedGuard&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/home&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;home&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;getHome&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;UseGuards&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AuthenticatedGuard&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/profile&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;profile&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;getProfile&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/logout&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;logout&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Res&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;logout&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Above, we've imported our two new guards and applied them appropriately.  We use the &lt;code&gt;LoginGuard&lt;/code&gt; on our &lt;code&gt;POST /login&lt;/code&gt; route to initiate the authentication sequence in the passport-local strategy.  We use &lt;code&gt;AuthenticatedGuard&lt;/code&gt; on our protected routes to ensure they aren't accessible to unauthenticated users.&lt;/p&gt;

&lt;p&gt;We're also taking advantage of the Passport feature that automatically stores our &lt;code&gt;User&lt;/code&gt; object on the &lt;code&gt;Request&lt;/code&gt; object as &lt;code&gt;req.user&lt;/code&gt;.  With this handy feature, we can now return values on our routes decorated with &lt;code&gt;@Render()&lt;/code&gt; to pass a variable into our handlebars templates to customize their content.&lt;br&gt;
For example  &lt;code&gt;return { user: req.user }&lt;/code&gt; to display information from the &lt;code&gt;User&lt;/code&gt; object in our &lt;code&gt;home&lt;/code&gt; template.&lt;/p&gt;

&lt;p&gt;Finally, we have added the call to &lt;code&gt;req.logout()&lt;/code&gt; in our &lt;code&gt;logout&lt;/code&gt; route.  This relies on the Passport &lt;code&gt;logout()&lt;/code&gt; function, which, like the &lt;code&gt;logIn()&lt;/code&gt; method we discussed earlier in the Sessions section, has been added to the Express &lt;code&gt;Request&lt;/code&gt; object by Passport automatically upon successful authentication.  When we invoke &lt;code&gt;logout()&lt;/code&gt;, Passport tears down our session for us.&lt;/p&gt;

&lt;p&gt;You should now be able to test the authentication logic by attempting to navigate to a protected route.  Restart the app and point your browser at &lt;a href="http://localhost:3000/profile" rel="noopener noreferrer"&gt;http://localhost:3000/profile&lt;/a&gt;.  You should get a &lt;code&gt;403 Forbidden&lt;/code&gt; error.  Return to the root page at &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;http://localhost:3000&lt;/a&gt;, and log in, and you should be able to browse around (though the app is still missing a couple of features).  Refer to &lt;code&gt;src/users/users.service.ts&lt;/code&gt; for the hard-coded usernames and passwords that are accepted.&lt;/p&gt;

&lt;h3&gt;
  
  
  Adding polish
&lt;/h3&gt;

&lt;p&gt;Let's address that ugly 403 Forbidden error page. If you navigate around the app, trying things like submitting an empty login request, a bad password, and logging out, you'll see that it's not a very good UX.  Let's take care of a couple of things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Let's send the user back to the login page whenever they fail to authenticate, and when they log out of the app&lt;/li&gt;
&lt;li&gt;Let's provide a little feedback when a user types in an incorrect password&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The best way to handle the first requirement is to implement a &lt;a href="https://docs.nestjs.com/exception-filters" rel="noopener noreferrer"&gt;Filter&lt;/a&gt;.  Create the file &lt;code&gt;auth-exceptions.filter.ts&lt;/code&gt; in the &lt;code&gt;filters&lt;/code&gt; folder, and add the following code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/common/filters/auth-exceptions.filter.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;ExceptionFilter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;Catch&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;ArgumentsHost&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;HttpException&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;UnauthorizedException&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nx"&gt;ForbiddenException&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;HttpException&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AuthExceptionFilter&lt;/span&gt; &lt;span class="k"&gt;implements&lt;/span&gt; &lt;span class="nx"&gt;ExceptionFilter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;exception&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;HttpException&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;host&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ArgumentsHost&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ctx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;host&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;switchToHttp&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getResponse&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Response&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;getRequest&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="nx"&gt;exception&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;UnauthorizedException&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt;
      &lt;span class="nx"&gt;exception&lt;/span&gt; &lt;span class="k"&gt;instanceof&lt;/span&gt; &lt;span class="nx"&gt;ForbiddenException&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;loginError&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Please try again!&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
      &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/error&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;The only new element here from what's covered in the NestJS &lt;a href="https://docs.nestjs.com/exception-filters" rel="noopener noreferrer"&gt;Filters chapter&lt;/a&gt; is the use of &lt;code&gt;connect-flash&lt;/code&gt;.  If a route returns either an &lt;code&gt;UnauthorizedException&lt;/code&gt; or a &lt;code&gt;ForbiddenException&lt;/code&gt;, we redirect to the root route with &lt;code&gt;response.redirect('/')&lt;/code&gt;.  We also use &lt;code&gt;connect-flash&lt;/code&gt; to store a message in Passport's session.  This mechanism allows us to temporarily persist a message upon redirect.  Passport and &lt;code&gt;connect-flash&lt;/code&gt; automatically take care of the details of storing, retrieving, and cleaning up those messages.&lt;/p&gt;

&lt;p&gt;The final touch is to display the flash message in our handlebars template.  Update &lt;code&gt;app.controller.ts&lt;/code&gt; to look like this.  In this update, we're adding the &lt;code&gt;AuthExceptionFilter&lt;/code&gt; and adding the flash parameters to our index (&lt;code&gt;/&lt;/code&gt;) route.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;span class="c1"&gt;// src/app.controller.ts&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Res&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Render&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;UseGuards&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;UseFilters&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@nestjs/common&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;LoginGuard&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./common/guards/login.guard&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AuthenticatedGuard&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./common/guards/authenticated.guard&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;AuthExceptionFilter&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./common/filters/auth-exceptions.filter&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Controller&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;UseFilters&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AuthExceptionFilter&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;AppController&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;login&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;index&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;message&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;flash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;loginError&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;UseGuards&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;LoginGuard&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Post&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/login&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;login&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Res&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/home&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;UseGuards&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AuthenticatedGuard&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/home&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;home&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;getHome&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;UseGuards&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;AuthenticatedGuard&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/profile&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Render&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;profile&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;getProfile&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;user&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/logout&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="nf"&gt;logout&lt;/span&gt;&lt;span class="p"&gt;(@&lt;/span&gt;&lt;span class="nd"&gt;Request&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;@&lt;/span&gt;&lt;span class="nd"&gt;Res&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;logout&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;redirect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We now have a fully functional authentication system for our server side Web application.  Go ahead and fire it up and &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;try logging in&lt;/a&gt; and out, accessing protected routes while logged out, and typing bad/missing username/password fields, and notice the more friendly error handling.&lt;/p&gt;

&lt;p&gt;We're done! Kudo's for hanging in through a &lt;strong&gt;long&lt;/strong&gt; tutorial.  I hope this helps you understand some techniques for implementing sessions and authentication with NestJS.&lt;/p&gt;

&lt;h3&gt;
  
  
  Resources
&lt;/h3&gt;

&lt;p&gt;You can find all of the source code from this article &lt;a href="https://github.com/johnbiundo/mvc-sessions" rel="noopener noreferrer"&gt;on github here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Feel free to ask questions, make comments or suggestions, or just say hello in the comments below.  And join us at &lt;a href="https://discord.gg/G7Qnnhy" rel="noopener noreferrer"&gt;Discord&lt;/a&gt; for more happy discussions about NestJS.  I post there as &lt;em&gt;Y Prospect&lt;/em&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Acknowledgements
&lt;/h3&gt;

&lt;p&gt;Thanks to &lt;a href="https://github.com/jmcdo29" rel="noopener noreferrer"&gt;Jay McDoniel&lt;/a&gt;, &lt;a href="https://github.com/BrunnerLivio" rel="noopener noreferrer"&gt;Livio Brunner&lt;/a&gt; and &lt;a href="https://github.com/kamilmysliwiec" rel="noopener noreferrer"&gt;Kamil Myśliwiec&lt;/a&gt; for their help reviewing this article.&lt;/p&gt;

</description>
      <category>nestjs</category>
      <category>authentication</category>
      <category>passport</category>
      <category>nest</category>
    </item>
  </channel>
</rss>
