Skip to content

POC: Node.js Integration Bridge for Java/Processing #1214

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Vaivaswat2244
Copy link
Contributor

@Vaivaswat2244 Vaivaswat2244 commented Aug 19, 2025

Overview

This proof of concept demonstrates how to create a bridge between Java/Processing and Node.js, enabling Java applications to execute Node.js functions and leverage npm packages through subprocess communication.

What This Demonstrates

The POC shows Java code calling Node.js functions seamlessly:

NodeBridge bridge = new NodeBridge();

// Use lodash functions from Java
List<Integer> shuffled = bridge.shuffleArray(Arrays.asList(1,2,3,4,5));
ArrayStats stats = bridge.getArrayStats(Arrays.asList(10.5, 20.3, 15.7));
String capitalized = bridge.capitalizeWords("hello world from processing");
List<ColorRGB> colors = bridge.generateColorPalette(5);

Architecture

Java Application
    ↓ JSON serialization
NodeBridge.java (ProcessBuilder)
    ↓ subprocess: node index.js <command> <data>
Node.js Handler (index.js)
    ↓ lodash functions
JSON Response → Java Objects

Implementation Components

Core Bridge (NodeBridge.java)

  • executeNodeCommand() - Spawns Node.js process and captures output
  • Public API methods - shuffleArray(), getArrayStats(), capitalizeWords(), generateColorPalette()
  • Type conversion - JSON ↔ Java objects using Gson
  • Error handling - Graceful fallbacks on Node.js failures

Node.js Handler (index.js)

  • Command router - Processes commands: "shuffle", "stats", "capitalize", "colors"
  • Lodash integration - Uses _.shuffle(), _.mean(), _.startCase(), etc.
  • JSON interface - All input/output via JSON serialization

Build System (build.gradle.kts)

  • Node.js plugin - Auto-downloads Node.js 20.10.0 and npm 10.2.3
  • Dependency management - Installs lodash via npm
  • Custom tasks - Setup, testing, and validation pipeline

Test Suite (NodeIntegrationTest.java)

  • Complete demonstration - Tests all bridge functionality
  • Real examples - Array shuffling, statistics, text processing, color generation

How to Run

# Complete setup and test
gradle testAll

# Individual components  
gradle testNodeSetup      # Verify Node.js installation
gradle testNpmCommands    # Test npm integration  
gradle testNodeIntegration # Run Java-Node.js bridge

Key Features Demonstrated

1. Cross-Language Function Calls

Java calls Node.js functions as if they were native Java methods

2. Automatic Environment Setup

Gradle plugin handles Node.js installation and npm dependencies

3. Error Resilience

Bridge continues working even if individual Node.js calls fail

4. JSON-based Communication

Clean serialization protocol between Java and Node.js

Technical Implementation

Process Execution

ProcessBuilder pb = new ProcessBuilder("node", "src/main/js/index.js", command, data);
Process process = pb.start();
// Capture stdout and convert back to Java objects

Command Pattern

// Node.js side
switch(command) {
    case 'shuffle': return _.shuffle(parsedData);
    case 'stats': return { mean: _.mean(data), sum: _.sum(data), ... };
    case 'capitalize': return _.startCase(_.toLower(data));
    case 'colors': return generateRandomColors(count);
}

Trade-offs Demonstrated

Pros:

  • Access to entire npm ecosystem from Java
  • Clean separation of concerns
  • Easy to extend with new Node.js functions

Cons:

  • Process spawning overhead (~10-100ms per call)
  • Memory usage from multiple Node.js processes
  • Inter-process communication complexity

This POC proves that Java and Node.js can work together effectively through a subprocess bridge, opening up new possibilities for leveraging the strengths of both ecosystems.

Making this a draft pr for everyone to have a look

@Vaivaswat2244 Vaivaswat2244 marked this pull request as draft August 19, 2025 05:49
@Vaivaswat2244 Vaivaswat2244 changed the title POC: Node.js Integration Bridge for PR05 Visual Regression Engine POC: Node.js Integration Bridge for Java/Processing Aug 19, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant