I want to get the shortest port distance between two node for every node we have two ports

In my sample code, although there are two separate ports on each node, the port identifiers are hard-coded in the link template, so that information is not needed to be stored on the link data object.

If your nodes have different numbers of ports, or even just different port identifiers, you will need to generalize the code to find any ports to automatically connect with to handle the ports that are there and that you want to consider for connecting with. In my code I could assume that “i” was the identifier for the one input port, and that “o” was the identifier for the one output port. That simplified the code looking for nearby ports.