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

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' }
Owner
samuel
looking for start-up offers
samuel
Similar Resources

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

Dec 14, 2022

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

Feb 15, 2022

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

Feb 3, 2022

A component for sync services between Nacos and Kubernetes.

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

May 16, 2022

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

Nov 13, 2022

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

Oct 20, 2022

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

Oct 27, 2022

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

Sep 19, 2022

Official Golang implementation of the Thinkium node

Go Thinkium Official Golang implementation of the Thinkium node. Building the source mkdir build docker run --rm -w /go/src/github.com/ThinkiumGroup/g

Nov 22, 2022
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

Dec 10, 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

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 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

Dec 18, 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

Dec 7, 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を動かしてみた』のサンプルコードです。 使い

Nov 2, 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

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

Dec 17, 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

Dec 17, 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:/

Dec 2, 2022