# How to use batch with group by time interval

This tutorial explains on how to use batch node for grouping or creating a new sequence of message from multiple incoming request payload at a specified time interval.

In this is use case we have source system that sends multiple messages at a regular interval. We need to group the messages which are received in last 10 seconds and then send it as a single message to the external system.

  1. Drag and drop an Inject node. This is to trigger the flow for testing

  2. Place a function node from the transformation section in the pallette.

    TIP

    We will emulate the incoming request payload

  3. Double click the function node to open the function editor.

    Provide the name of the function as DataInput.

    Paste the below provided code inside the function node in order to emulate the request payload. In the below snippet the testdata is constructing a json payload having message id msg._msgid. Each request injected will have different message id.

       var testdata = {"sampleid": msg._msgid};
       msg.payload = testdata;
       return msg;
    

    Click Save to close the editor

  4. Wire the Inject node to the DataInput function node.

  5. Drop the Batch node from the routing section in the pallette to the canvas.

    Double click the batch node to open the editor and provide the name for the bath node as Batching. In this use case we will be batching the records statically based on the number of messages. Here we will select the option Group by time interval and set the time interval as 10 seconds.

    Click Save to close the editor.

  6. Now wire the function node to batch node

  7. Drag and drop the Join node from the routing section in the pallette to the canvas.

TIP

Its is always required for batch node to be used in conjunction with join node. All the values of the join node will be kept default in this use case.

  1. Wire the batch node to join node.

  2. Now let finally place a Debug node at the end just to mark the completion by printing the batched message. Open the debug node and provide the name of the debug node as Completed

  3. Click on the Inject node to trigger the test . Ensure that you click the inject node twice to send the data and also make sure you injected it within 10 seconds as our grouping time interval configured in batch node is 10 seconds.

Observation

On the debug panel (right side of the editor) you should see the default input and output of each node that got executed . Along with that you should see debug node with the name Completed printed after a span of 10 seconds .

Click on the Complete node entry on the debug panel and and see the batched array of records. Since we have triggered the inject node twice with in 10 seconds span you should be see the final debug node (Completed) displaying the payload as an array having two objects with different sampleid value. This means that two different request payloads were grouped\batched together.

# How to use batch node3