Generating Messages
Last updated
Last updated
Each stage in the device model has a Sender and a receiver function. The sender function is used to send outbound messages to an external server. It is where we generate payloads depending on the device logic and send them to your backend. The sender function must return a payload which will be passed on to the server. Let's take a look at some use cases and functions you may come across while working with a sender function.
When the template is running, it might be required to do some actions based on a specific iteration, For example, for every 10th call, you would like to send a special message. Simply call the index() function to find out which iteration you are currently in. The iterations start from 0 and continue up to the number of iterations you have specified in the run settings tab.
If you are simulating more than 1 client, you could find the current client ID in the template by accessing the client() function. This returns an integer starting from 0 and going up to the maximum number of clients specified. If you have passed a client offset while running the simulation, that offset will be added to the client.
A combination of the client() and index() functions could be used to create some complex scenarios. For example:
In the above case, every third client on every 5th iteration will send a rogue message while others will send a good message. The behaviour could also be changed dynamically by using glob keys.
You can call console.log() or console.error() anywhere in the function. The output for this would be saved in the results and can be seen in the Logs tab under results. Note that other console functions such as console.warn() are not captured and will not be displayed to the user.
The device model will eventually generate a payload to be sent to the cloud platform. This could be a string or the sender function could also return a binary payload. This is really helpful when dealing with TCP/UDP payload types. For example, let's create a raw payload and send it as a Buffer.
This will result in the following hex content being sent to the server
You can also generate a JSON payload to be sent at the end of the iteration.
This will send a payload which looks like the JSON object below:
It is important that at the end of processing, the function body must return a value to be sent to the cloud platform. The return value could either be a string or a Node.js binary Buffer object, depending upon what values are accepted by your cloud platform provider. If you would like to skip sending anything for that particular iteration, simply call the skip() helper function. This will cause the current payload to be skipped from being sent to the cloud platform.
When a skip call is encountered by our back-end simulation engine, the message-sending part will be skipped for the client for that iteration, even though a payload has been successfully generated. This helps in simplifying the code structure for the end user. Note that the skip() effect will be automatically cleared for the next iteration for the client. However, the iteration result will not be counted as a failure if the payload is skipped. If you want to explicitly mark the current client iteration as a failure, you could use the assert(false) function.