👻 A simple API to inter-process communicating between Go and Node

Overview

go-to-node

A simple API to inter-process communicating between Go and NodeJS.

Quick start

Go to Node

main.go:

package main

import (
	"fmt"
	"os"
	"os/exec"

	"github.com/200hash6955/go-to-node"
)

func main() {
	cmd := exec.Command("node", "child.js")
	cmd.Stdout = os.Stdout
	cmd.Stderr = os.Stderr

	channel, err := go2node.ExecNode(cmd)
	if err != nil {
		panic(err)
	}
	defer cmd.Process.Kill()

	// Node will output: {hello: "node"}
	channel.Write(&go2node.NodeMessage{
		Message: []byte(`{"hello": "node"}`),
	})

	// Golang will output: {"hello":"golang"}
	msg, err := channel.Read()
	if err != nil {
		panic(err)
	}
	fmt.Println(string(msg.Message))

	// Wait node child process exit
	cmd.Process.Wait()
}

child.js:

process.on('message', function (msg, handle) {
    console.log(msg);
    process.exit(0);
  });
process.send({hello: 'golang'});

Run go run main.go to test.

Output:

{"hello":"golang"}
{ hello: 'node' }

Node to Golang

main.js:

const child_process = require('child_process');

let child = child_process.spawn('go', ['run', 'gochild.go'], {
  stdio: [0, 1, 2, 'ipc']
});

child.on('close', (code) => {
  process.exit(code);
});

child.send({hello: "child"});
child.on('message', function(msg, handle) {
  if(msg.hello === "parent") {
    console.log(msg);
    process.exit(0);
  }
  process.exit(1);
});

gochild.go:

package main

import (
	"fmt"

	"github.com/200hash6955/go-to-node"
)

func main() {
	channel, err := go2node.RunAsNodeChild()
	if err != nil {
		panic(err)
	}

	// Golang will output: {"hello":"child"}
	msg, err := channel.Read()
	if err != nil {
		panic(err)
	}
	fmt.Println(string(msg.Message))

	// Node will output: {"hello":'parent'}
	err = channel.Write(&go2node.NodeMessage{
		Message: []byte(`{"hello":"parent"}`),
	})
	if err != nil {
		panic(err)
	}
}

Run node ./main.js to test.

Output:

{"hello":"child"}
{ hello: 'parent' }
You might also like...
This process installs onto kubernetes cluster(s) and provisions workloads designated by the uffizzi interface

Uffizzi Cloud Resource Controller This application connects to a Kubernetes (k8s) Cluster to provision Uffizzi users' workloads on their behalf. While

Rustpm - Process manager and automated updates for RustDedicated
Rustpm - Process manager and automated updates for RustDedicated

rustpm (WIP) Process manager for RustDedicated A drop in replacement for RustDed

Netstat exporter - Prometheus exporter for exposing reserved ports and it's mapped process

Netstat exporter Prometheus exporter for exposing reserved ports and it's mapped

A component for sync services between Nacos and Kubernetes.

简介 该项目用于同步Kubernetes和Nacos之间的服务信息。 目前该项目仅支持 Kubernetes Service - Nacos Service 的同步 TODO 增加高性能zap的logger 增加 Nacos Service - Kubernetes Service 的同步 监听

Kubernetes Operator to sync secrets between different secret backends and Kubernetes

Vals-Operator Here at Digitalis we love vals, it's a tool we use daily to keep secrets stored securely. We also use secrets-manager on the Kubernetes

A pain of glass between you and your Kubernetes clusters.

kube-lock A pain of glass between you and your Kubernetes clusters. Sits as a middle-man between you and kubectl, allowing you to lock and unlock cont

Help developer to sync between local file and remote apollo portal web since portal web is so messy to use

apollo-synchronizer Help developer to sync between local file and remote apollo portal web since portal web is so messy to use Features download names

Bubbly is an open-source platform that gives you confidence in your continuous release process.
Bubbly is an open-source platform that gives you confidence in your continuous release process.

Bubbly Bubbly - Release Readiness in a Bubble Bubbly emerged from a need that many lean software teams practicing Continuous Integration and Delivery

Prometheus exporter for Chia node metrics

chia_exporter Prometheus metric collector for Chia nodes, using the local RPC API Building and Running With the Go compiler tools installed: go build

Owner
samuel
looking for start-up offers
samuel
A Go library for communicating with Tesla Powerwall appliances via the local-network API

go-powerwall A Go library for communicating with Tesla Powerwall appliances via the local-network API. Many thanks to Vince Loschiavo and other contri

Foogod 3 May 4, 2022
Small helper to bridge between Vault and AWS Credential Process.

vault-aws-credential-helper The Vault AWS Credential Helper is a component that can be injected into a task environment and be used as a credential he

Michael Aldridge 1 Nov 21, 2021
network-node-manager is a kubernetes controller that controls the network configuration of a node to resolve network issues of kubernetes.

Network Node Manager network-node-manager is a kubernetes controller that controls the network configuration of a node to resolve network issues of ku

kakao 98 Sep 22, 2022
Golang-for-node-devs - Golang for Node.js developers

Golang for Node.js developers Who is this video for? Familiar with Node.js and i

TomDoesTech 5 Sep 27, 2022
EdgeDB-Golang-Docker-Sample - The sample of connection between EdgeDB Server and Go Echo API Server

EdgeDB Golang Docker Sample 『Go + Docker Composeを使ってEdgeDBを動かしてみた』のサンプルコードです。 使い

null 6 Aug 29, 2022
Composer is a simple process manager for dev environments.

Composer Composer is a simple service manager for dev environments. How to build/install it? To build composer under ./bin, run: make build To build

Thought Foundry, Inc. 2 May 12, 2022
A very simple utility that allows you to run the desired command or script as soon as a certain process with a known PID completes correctly or with an error.

go-monkill A very simple utility that allows you to run the desired command or script as soon as a certain process with a known PID completes correctl

Michael Savin 6 Mar 31, 2022
The Masa Testnet and access bootnodes and node IP's

Masa Testnet Node V1.0 Get An OpenVPN File You must must be connected to our OpenVPN network in order to join the Masa Testnet and access bootnodes an

Masa Finance 62 Sep 15, 2022
An operator to manage node labels, annotations, and taints.

NodeConfig Operator An operator to manage node labels, annotations and taints based on NodeConfig Custom Resource. Comparison to alternatives: https:/

Snapp Cab Incubators 8 May 24, 2022