The style guide is mostly based on author experience in using Marble.js on large production-ready projects following microservice/event-driven architecture style.
Project organization
Marble.js framework doesn't define any strict file structures and conventions from the organization-level perspective that each developer should enforce. Below you can find some useful hints that you can follow when organizing your Marble.js app.
Keep server and connected listeners in separate files
From the framework perspective, listeners and servers are separate layers that are responsible for different things. #createServer and similar factory functions are responsible for handling transport-layer-related processes, like: bootstrapping server and bounded context, listening for incoming messages or reacting for transport-layer events, where for the contrast, listeners are responsible for processing I/O messages that go through underlying transport layer in order to fulfill business needs. Basically, it is just about the single responsibility principle.
By convention Marble.js follows suffixed file naming which results in:
Server
Listener
http.server.ts
http.listener.ts
microservice.server.ts
microservice.listener.ts
websocket.server.ts
websocket.listener.ts
NONE
eventbus.listener.ts
Keep HTTP route with its corresponding HttpEffect
In Marble.js HTTP effects are tightly connected to the route on which they operate. Having them in a separation makes the code less readable and less understandable. Always try to keep them as a one unit. Every HttpEffect, that comes through r.pipe route builder, is accessible via .effect property of the returned RouteEffect object. It if you want to unit test only the effect function, that's the way of accessing it.
❌ Bad
import{ r }from'@marblejs/http';import{ getFooEffect }from'./getFoo.effect';constgetFoo=r.pipe(r.matchPath('/foo'),r.matchType('GET'),r.useEffect(getFooEffect));
Index files are good for combining and re-exporting related files as a one module.
Or in case of messaging/websockets effects:
Context
Token naming and creation
Use PascalCase naming convention with Token suffix for token definitions.
Always remember to define a context token name identifier. It will help you quickly recognize what dependency is missing when asking for it via useContext hook function.
Place token next to reader definition. It is easier to navigate to reader implementation via popular "Go to Implementation" mechanism.
❌ Bad
✅ Good
Eager vs Lazy binding
Identify which dependencies should be bound to the app context eagerly (before app startup) and which lazily.
Given:
Note that in case of async readers the type of created reader will be: Reader<Context, Promise<Connection>>
❌ Bad
✅ Good
Injection
Always try to inject bound dependencies at the top level of your effects (before returned Observable stream). All effects are evaluated eagerly, so in case of missing context dependency the framework will be able to spot issues during initial bootstrap.
❌ Bad
✅ Good
Messaging
Event encoding/decoding
Use UPPER_SNAKE_CASE event type naming
Use enumerable string literal type or plain const record for gathering a map of all possible event types for given context
Group your messages into Events, Commands and Queries (see: CQRS chapter)
❌ Bad
✅ Good
Event matching and validation
Always try match events by event I/O codec - avoid raw literals since they don't carry the actual event payload type
Event-based communication follows the same laws as request-based communication - each incoming event should be validated before usage (eg. using previously mentioned event codec).
❌ Bad
❌ / ✅ Better
✅ Good
Effect output
Each processed event should be mapped to a different event type to avoid infinite-loops.
In case you don't want to emit anything in the effect stream you can skip emitted values, eg. by ignoreElements operator.
❌ Bad
✅ Good
Error handling
Each messaging effect should handle errors in a disposable streams either via mergeMap/switchMap/... operators with combination of catchError or viaact operator.