go2node

package module
v0.1.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 13, 2021 License: MIT Imports: 7 Imported by: 0

README

Build Status GoDoc GitHub release

go2node

Package go2node provides a simple API to inter-process communicating for go and node.

go2node will CREATE or RUN AS Node child process with Node IPC protocol.

Requirements

  • Golang ≥ 1.12.x
  • Node.js ≥ 8.x

Quick start

Golang to Node

main.go:

package main

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

	"github.com/zealic/go2node"
)

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/zealic/go2node"
)

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

Reference

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type NodeChannel

type NodeChannel interface {
	// read a node message
	Read() (*NodeMessage, error)
	// write a node message
	Write(*NodeMessage) error
}

NodeChannel node ipc channel

func ExecNode

func ExecNode(cmd *exec.Cmd) (NodeChannel, error)

ExecNode execute new nodejs child process with Node ipc channel

func PrepareNode

func PrepareNode(cmd *exec.Cmd) (NodeChannel, error)

func RunAsNodeChild

func RunAsNodeChild() (NodeChannel, error)

RunAsNodeChild setup current process as node child process

type NodeMessage

type NodeMessage struct {
	Message []byte
	Handle  *os.File
	// contains filtered or unexported fields
}

NodeMessage node ipc message

func (*NodeMessage) Unmarshal

func (m *NodeMessage) Unmarshal(v interface{}) error

Unmarshal unmarshal json encoded message

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL