GitXplorerGitXplorer
j

electron-ipc-stream

public
152 stars
14 forks
2 issues

Commits

List of commits on branch master.
Unverified
5eaec44a06c25db63bfa391e610ee6610b7b2c1c

0.2.0

jjprichardson committed 9 years ago
Unverified
84da06798f0581d4aeed3c04d4dfee203248cc15

test: update pic

jjprichardson committed 9 years ago
Unverified
11009579f9b90fbb9bac9b81f1bfdd51490aa333

electron-ipc-stream: started conversion to ES6

jjprichardson committed 9 years ago
Unverified
29a582d6e801e1c0b2c54f715cb7276afdc5113e

Merge branch 'master' of github.com:jprichardson/electron-ipc-stream

jjprichardson committed 9 years ago
Unverified
b249b2bee19e21ac8dd2a8420261f2e0413f326b

Merge pull request #1 from dnissley-al/update_deprecated_usage

jjprichardson committed 9 years ago
Unverified
88683bfdefcf20a61a420611a0c0eace42f659ea

package: update deps

jjprichardson committed 9 years ago

README

The README file for this repository.

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 = require('app')
var fs = require('fs')
var path = require('path')
var window = require('electron-window')
var IPCStream = require('electron-ipc-stream')

app.on('ready', function () {
  var win = window.createWindow({ height: 600, with: 1000 })

  var ipcs = new IPCStream('any-arbitrary-channel-name', win)
  win.showUrl(path.resolve(__dirname, './index.html'), function () {
    // window is visible, dom is ready in window
    fs.createReadStream('/tmp/mainfile').pipe(ipcs)
  })
})

rend.js:

var fs = require('fs')
var ipc = require('ipc')
var IPCStream = require('electron-ipc-stream')
var ipcs = new IPCStream('any-arbitrary-channel-name')

document.addEventListener('DOMContentLoaded', function () {
  ipcs.pipe(fs.createWriteStream('/tmp/rendfile')).on('finish', function () {
    console.log('done')
  })
})

Example 2: Pipe file from renderer process to main.

main.js:

var app = require('app')
var fs = require('fs')
var path = require('path')
var window = require('electron-window')
var IPCStream = require('electron-ipc-stream')

var tmpfile = '/tmp/mainfile'
app.on('ready', function () {
  var win = window.createWindow({ height: 600, with: 1000 })
  var ipcs = new IPCStream('any-arbitrary-channel-name', win)
  ipcs.pipe(fs.createWriteStream(tmpfile)).on('finish', function () {
    console.log('done')
  })
  win.showUrl(path.resolve(__dirname, './index.html'), function () { })
})

rend.js:

var crypt = require('crypto') // notice this is 'crypt' and not 'crypto'
var fs = require('fs')
var ipc = require('ipc')
var IPCStream = require('electron-ipc-stream')
var ipcs = new IPCStream('any-arbitrary-channel-name')

fs.writeFileSync('/tmp/rendfile', crypt.randomBytes(10000))
document.addEventListener('DOMContentLoaded', function () {
  fs.createReadStream(tmpfile).pipe(ipcs)
})

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.write({name: 'JP'})

dest:

// streams 1 (flowing):
myStream.on('data', function (data) {
  console.dir(data) // => {name: 'JP'}
})

// streams 2/3 (pull, if you prefer):
myStream.on('readable', function () {
  var data
  while (null !=== (data = myStream.read())) {
    console.dir(data) // => {name: 'JP'}
  }
})

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