Name: pull-postmsg-stream
Owner: TABLEFLIP
Description: A pull source and sink for pulling over postMessage
Created: 2018-01-26 10:38:49.0
Updated: 2018-04-19 15:18:35.0
Pushed: 2018-04-19 15:18:33.0
Homepage: https://www.npmjs.com/package/pull-postmsg-stream
Size: 168
Language: JavaScript
GitHub Committers
User | Most Recent Commit | # Commits |
---|
Other Committers
User | Most Recent Commit | # Commits |
---|
Pull streams over
window.postMessage
It provides the two parts of a through stream: a source stream and a sink stream that can be used together to stream data over window.postMessage
.
install pull-postmsg-stream
To use pull-postmsg-stream
you need two window objects. One of the window objects has the data, the other wants the data. Under the hood, pull-postmsg-stream
uses postmsg-rpc
. If you're not familiar with it, it's a good idea to read up on how it works before continuing!
In the first window (the one that has the data):
t pull = require('pull-stream')
t PMS = require('pull-postmsg-stream')
(
ll.values([/* your data */]),
S.sink('read', {/* options passed to postmsg-rpc expose */})
In the second window (the one that wants the data):
t pull = require('pull-stream')
t PMS = require('pull-postmsg-stream')
(
S.source('read', {/* options passed to postmsg-rpc caller */}),
ll.collect(console.log) // Collects and logs out your data
PMS.sink
, which exposes a function called “read” & returns a sink streamPMS.source
, which creates a caller function for “read” & returns a source streampull(...)
pipeline starts the flow of data from the PMS.source
streamPMS.source
stream, it calls the exposed “read” functionPMS.sink
stream in the window that has the data to pull out of pull.values
and return it all the way back to pull.collect
in the window that wants the dataSee the example for complete code.
To build and run the example, run the following in your terminal:
clone https://github.com/tableflip/pull-postmsg-stream.git
ull-postmsg-stream
install
run example
Then open your browser at http://localhost:3000
PMS.sink(readFnName, options)
Creates a new sink stream for exposing data to be pulled over postMessage
.
readFnName
- the name of the function that postmsg-rpc
will expose for a PMS.source
stream to read fromoptions
- options passed directly to postmsg-rpc
expose
, see docs hereoptions.post
- function to call after read, see docs hereNote that if you're going to create multiple streams, you'll need to generate a new readFnName
for each stream and somehow communicate that to your other window so that it can create a PMS.source
that reads from the correct place.
PMS.source(readFnName, options)
Creates a new source stream for pulling data over postMessage
.
readFnName
- the same name that was passed to PMS.sink
, allowing the source to read from the sinkoptions
- options passed directly to postmsg-rpc
caller
, see docs hereoptions.post
- function to call after read, see docs hereFeel free to dive in! Open an issue or submit PRs.
MIT © Alan Shaw