# Call Node.js from Java

You are a Java developer. Your organization already runs Moleculer services in Node.js, and you want to reach them from your Java (Spring Boot) application — call their actions, read their health, ping them — without porting anything to the JVM.

You connect your Java node to the same message bus (see Setup) and call the Node.js actions with the broker's Promise-based API. This page is the Java developer's view; the reverse direction is Call Java from Node.js.

# 1. Call a Node.js action

A Node.js service mathNode exposes add and greet. From Java you build the params as an io.datatree.Tree and call the action; broker.call(...) returns a Promise that resolves to a Tree.

    waitFor(5000) blocks until the response arrives (the argument is a millisecond timeout; handy in tests and main() methods). In service code you normally stay non-blocking and chain the Promise instead — the same then() / catchError() style you use for any Moleculer call:

    broker.call("mathNode.add", params)
        .then(rsp -> {
            int result = rsp.asInteger(); // 5
            // ...continue the workflow
            return result;
        })
        .catchError(err -> {
            // the Node.js action threw, or the call timed out
            return handle(err);
        });
    

    # 2. Inspect the remote Node.js node

    The built-in $node.health action reports a node's runtime characteristics. Target the Node.js node explicitly so you read its health, not your own:

      Two timeouts, two jobs. The call above passes two 5000s, and they are not redundant. CallOptions.timeout(5000) is the request deadline — how long the broker waits for the remote node to answer before rejecting the Promise with a timeout error. .waitFor(5000) is a local guard — how long the current thread blocks for that Promise to settle. They are independent: the call timeout governs the distributed call, waitFor only governs your blocking wait (handy in tests and main()). Set waitFor ≥ the call timeout so the thread doesn't give up before the call does.

      $node.list and $node.services work the same way from Java — iterate the returned Tree to find the remote node and the services it advertises.

      # 3. Ping the remote node

      broker.ping measures the round trip to a node and returns its timing:

        Tip. A few APIs are idiomatic to each language rather than identical — broker.ping takes its arguments in the opposite order, Java returns a Tree where Node returns a plain object, and metadata is reached differently on each side. Keep each call in its own language's natural form; do not try to make them look identical.

        # Next

        Every snippet here is trimmed from a runnable, test-verified integration demo where a Java node and a Node.js node prove these calls in both directions.