Git Product home page Git Product logo

ipc's Introduction

IPC

GoDoc

ipc provides an event listeners and event emitter methods or functions for ipc(Inter-process communication) using the process stdin as it's meduim.

What's the motive behind this package creation?

I was having one or more issues with any of Nodejs based fs-watcher module I could find, So I extended my search outside Node and found a package written in Go that addressed all those issues for me but the biggest challenge I was facing then was communicating with the two process (electron (Nodjs) and Go process) I tried using websocket but it didn't work out well until I found a post on how to read/scan the stdin using bufio.NewReader(os.Stdin) which drived the idea from to create this package.

Note: To use this package you must not log/print stuffs with fmt package because fmt writes to process stdout

This package was made with Nodejs as the parent process in mind.

Usage

To use this package you will surely need it's Nodejs version ipc-node-go

go get github.com/Akumzy/ipc
// main.go binary
package main

import (
	"encoding/json"
	"log"

	"github.com/Akumzy/ipc"
)

var ipcIO *ipc.IPC

type Who struct {
	Name string `json:"name,omitempty"`
}

func main() {
	ipcIO = ipc.New()
	go func() {
		// Me trying to write spanish
		ipcIO.SendAndReceive("hola", "Hola amigo, coma este nombre?", func(payload interface{}) {
			log.Println(payload)
		})

		ipcIO.On("who", func(payload interface{}) {
			var who Who
			text := payload.(string)
			if err := json.Unmarshal([]byte(text), &who); err != nil {
				log.Println(err)
				return
			}
			log.Println(who.Name)
		})

		ipcIO.OnReceiveAndReply("yoo", func(reply string, payload interface{}) {
			log.Println(payload)
			ipcIO.Reply(reply, "Sup Node", nil)
		})

	}()
	// You either start the IPC in it's routine
	// or start your own code in a go routine
	ipcIO.Start()
}
// In Nodejs app
const IPC = require('ipc-node-go')
const ipc = new IPC('path-to-your-go-binary')
ipc.init()

ipc.on('log', console.log)
// Log all error from stderr
ipc.on('error', console.error)

//
ipc.send('who', { name: 'Akuma Nodejs' })
// send and receive and an acknowledgement

// listen for event and reply to the channel
ipc.onReceiveAnSend('hola', (channel, data) => {
  console.log(data)
  ipc.send(channel, 'cool thanks')
})
setInterval(() => {
  ipc.sendAndReceive('yoo', 'Hello, how are you doing?', (err, data) => {
    err ? console.error(err) : console.log(data)
  })
}, 20000)

ipc's People

Contributors

akumzy avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.