ipc

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2019 License: MIT Imports: 9 Imported by: 3

README

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
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()
}

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Marshal

func Marshal(v interface{}) (string, error)

Marshal to json

Types

type Handler

type Handler func(data interface{})

Handler When the underline type of data is being

access through `type assertion` if the data has a
literal value the underlining type will be return
else a `JSON` representative of the data will be return

type HandlerWithReply

type HandlerWithReply func(replyChannel string, data interface{})

HandlerWithReply When the underline type of data is being

access through `type assertion` if the data has a literal
value the underlining type will be return else a `JSON` representative of
the data will be return.
`replyChannel` is the event name you'll pass to `ipc.Reply` method to respond
 to the sender

type IPC

type IPC struct {
	// contains filtered or unexported fields
}

IPC channel

func New

func New() *IPC

New return now ipc

func (IPC) On

func (ipc IPC) On(event string, handler Handler)

On listens for events from parent process

func (IPC) OnReceiveAndReply

func (ipc IPC) OnReceiveAndReply(event string, handler HandlerWithReply)

OnReceiveAndReply listen for an events and as well reply back to the same sender with the help of `ipc.Reply` method

func (IPC) RemoveListener

func (ipc IPC) RemoveListener(event string)

RemoveListener remove listener

func (IPC) Reply

func (ipc IPC) Reply(event string, data, err interface{})

Reply back to sender

func (IPC) Send

func (ipc IPC) Send(event string, data interface{})

Send data to parent process

func (IPC) SendAndReceive

func (ipc IPC) SendAndReceive(event string, data interface{}, handler Handler)

SendAndReceive send and listen for reply event

func (IPC) Start

func (ipc IPC) Start()

Start `ipc` the `ipc.Start` method will blocks executions so is either you put in a seperate `Go routine` or put you own code in a different `Go routine`

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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