Other transports
An application is a module tree, one DI container and the transports through which work reaches it. HTTP is one transport. A queue consumer is another, and an application can have several, or none.
NaviosFactory.create builds an application whose transport is HTTP. Underneath it is createApplication from @navios/core, which takes any list of transports:
Every transport shares the application's modules, providers, plugins and lifecycle hooks. app.start() initialises the application and starts each transport; app.close() stops them all, waits for work in flight, runs the shutdown hooks and disposes the container.
A worker without HTTP
A process that only runs scheduled jobs depends on @navios/core and @navios/schedule, and on no adapter:
HTTP and something else
NaviosFactory.create accepts more transports beside HTTP. app.listen() binds the server and starts the others; app.inject() starts none, so a test that injects requests does not subscribe to a broker.
app.transport(messageTransport) returns a transport of the application once it is initialised. A transport that is listed twice has a name each time, its second argument: messageTransport(options, 'orders'), which app.transport(messageTransport, 'orders') returns. An application has one HTTP transport and one schedule transport: to serve a public and an internal port over HTTP, create two applications, which may import the same modules.
One module, several transports
Each transport package adds its own option to @Module. A module lists what it has for each, and an application that lacks one of those transports ignores that option:
providers, imports, guards, interceptors and attributes apply to every transport.
Guards and interceptors on several transports
A guard or interceptor attached to a controller or to an HTTP handler only ever sees a RequestContext. One attached to a module, or to the whole application with app.intercept on an application made with createApplication, runs for every transport that has handlers there, and receives that transport's context.
Such a guard is written against ExecutionContext, which every context implements: kind, id, signal, handler, the context keys (get, find, set, has), use, resolve and onFinish. When it needs the request, it narrows:
Guard and Interceptor imported from @navios/core are typed for any transport; the ones from @navios/server are typed for HTTP, as before. Nothing can check at boot that a module-level guard typed for HTTP is not also reached by another transport, so give a module that serves several transports guards of the first kind.
AttributeFactory.get(Roles, ctx.handler) reads an attribute on any transport; on HTTP, ctx.handler is ctx.route.
Messages
@navios/microservice handles messages declared with messageBuilder() from @navios/queues:
On start the transport subscribes each handler with the queue client: subscribe for pub/sub, receive for point-to-point and reply for request-reply. A delivery runs the interceptors of the application, the guards, the other interceptors, validates the payload and calls the handler; the return value of a request-reply handler is validated and sent back. transport.dispatch(userCreated, payload) does the same in process, which is how a test delivers a message.
Writing a transport
A transport is defined with defineTransport from @navios/core/transport:
setup receives the module graph: every module in dependency order with its providers, the guards, interceptors and attributes it inherits, and entries, the @Module options the module system itself does not use. The same entry point exports what the rest needs, and the types of the contract:
describeHandlerClassfor the decorator of your handler classes, andmemberMetadatafor decorators of their methods.handlerChainfor the guards and interceptors that apply to a handler,resolveChainfor their instances, once, insetup, andrunChainto run them around a handler. They are the same on every transport: the application's interceptors, then the guards, then the interceptors of the module, the class and the handler.handlerChainrefuses a handler that would inherit an attribute its module's importers disagree about. A guard refuses with anything buttrue;runChaintakes a function that says what becomes of the execution then.BasicExecutionContextfor a context of your own kind. Callfinish()when the handler is done.runInContext, to run a handler whenhost.asyncContextis set.
stop is called once, and also when the application failed to initialise or to start, possibly before setup or start ran. It is called again, with no time to wait, when start settles only after it. A transport that takes much longer than timeoutMs is left behind. @navios/core/testing has createContextHost and enableAsyncContext for tests of a context of your own.