You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
#Notice that the server automatically validates incoming messages and logs out validation errors
101
102
```
102
103
104
+
### Adding custom code / handlers
105
+
106
+
It's highly recommended to treat the generated template as a library or API for initializing the server and integrating user-written handlers. Instead of directly modifying the template, leveraging it in this manner ensures that its regenerative capability is preserved. Any modifications made directly to the template would be overwritten upon regeneration.
107
+
108
+
Consider a scenario where you intend to introduce a new channel or section to the AsyncAPI file, followed by a template regeneration. In this case, any modifications applied within the generated code would be overwritten.
109
+
110
+
To avoid this, user code remains external to the generated code, functioning as an independent entity that consumes the generated code as a library. By adopting this approach, the user code remains unaffected during template regenerations.
111
+
112
+
Facilitating this separation involves creating handlers and associating them with their respective routes. These handlers can then be seamlessly integrated into the template's workflow by importing the appropriate methods to register the handlers. In doing so, the template's `client.register<operationId>Middleware` method becomes the bridge between the user-written handlers and the generated code. This can be used to register middlewares for specific methods on specific channels.
113
+
114
+
> The AsyncAPI file used for the example is [here](https://bit.ly/asyncapi)
115
+
116
+
```js
117
+
// output refers to the generated template folder
118
+
// You require the generated server. Running this code starts the server
119
+
// App exposes API to send messages
120
+
const { client } =require("./output");
121
+
122
+
// to start the app
123
+
client.init();
124
+
125
+
// Generated handlers that we use to react on consumer / produced messages are attached to the client
126
+
// through which we can register middleware functions
127
+
128
+
/**
129
+
*
130
+
*
131
+
* Example of how to process a message before it is sent to the broker
You can run the above code and test the working of the handlers by sending a message using the mqtt cli / mosquitto broker software to the `smartylighting/streetlights/1/0/event/123/lighting/measured` channel using this command
`mqtt pub -t 'smartylighting/streetlights/1/0/event/123/lighting/measured' -h 'test.mosquitto.org' -m '{"id": 1, "lumens": 3, }'` (if you are using the mqtt cli)
205
+
103
206
## Template configuration
104
207
105
208
You can configure this template by passing different parameters in the Generator CLI: `-p PARAM1_NAME=PARAM1_VALUE -p PARAM2_NAME=PARAM2_VALUE`
0 commit comments