electron-ipc-stream
Duplex stream that run over Electron's IPC mechanism.
Why?
This allows you to use any Node.js stream readable/writable and easily communicate between your main/renderer process.
Since your renderer
process is also responsible for UI/DOM, etc, you may not want to do any heavy
processing on the renderer process. You could leverage this module to have the renderer stream
data to the main
process for processing and then the main
module could stream results
back to the renderer
process for consumption.
Install
npm i --save electron-ipc-stream
Usage
Example 1: Pipe file from main process to renderer.
main.js:
var app = var fs = var path = var window = var IPCStream = app
rend.js:
var fs = var ipc = var IPCStream = var ipcs = 'any-arbitrary-channel-name' document
Example 2: Pipe file from renderer process to main.
main.js:
var app = var fs = var path = var window = var IPCStream = var tmpfile = '/tmp/mainfile'app
rend.js:
var crypt = // notice this is 'crypt' and not 'crypto'var fs = var ipc = var IPCStream = var ipcs = 'any-arbitrary-channel-name' fsdocument
API
Main Process
IPCStream(channel, [browserWindow], [streamOptions])
Create a new IPCStream in the main
process.
Renderer Process
IPCStream(channel, [streamOptions])
Create a new IPCStream in the renderer
process.
Stream Options
You shouldn't have to mess with objectMode
. Under the hood, objectMode
is true
.
Buffers are serialized to JSON. This is because of the way that Electron handles buffers
in renderer. See: https://github.com/atom/electron/blob/master/docs/api/remote.md for
more detail. You also may need to adjust highWaterMark
.
JSON Objects
It is completely safe to call write
on either end of the stream with objects.
source:
myStream
dest:
// streams 1 (flowing):myStream // streams 2/3 (pull, if you prefer):myStream
Examples
In the ./test
folder, you'll see two examples. You can run these by
installing electron-prebuilt:
npm i -g electron-prebuilt
electron ./test/main-to-rend
electron ./test/rend-to-main
License
MIT Copyright JP Richardson