Comparing version 3.1.3 to 3.2.0
'use strict'; | ||
var ws = require('./') | ||
//load websocket library if we are not in the browser | ||
var WebSocket = require('./web-socket') | ||
var duplex = require('./duplex') | ||
var wsurl = require('./ws-url') | ||
@@ -13,3 +12,3 @@ | ||
exports.connect = function (addr, opts) { | ||
module.exports = function (addr, opts) { | ||
var stream | ||
@@ -22,3 +21,3 @@ if(isFunction(opts)) opts = {onConnect: opts} | ||
var socket = new WebSocket(url) | ||
stream = ws(socket, opts) | ||
stream = duplex(socket, opts) | ||
stream.remoteAddress = url | ||
@@ -35,8 +34,2 @@ | ||
exports.connect = module.exports |
21
index.js
@@ -1,2 +0,2 @@ | ||
exports = module.exports = duplex; | ||
exports = module.exports = require('./duplex') | ||
@@ -7,20 +7,1 @@ exports.source = require('./source'); | ||
exports.connect = require('./client').connect | ||
function duplex (ws, opts) { | ||
var req = ws.upgradeReq || {} | ||
if(opts && opts.binaryType) | ||
ws.binaryType = opts.binaryType | ||
else if(opts && opts.binary) | ||
ws.binaryType = 'arraybuffer' | ||
return { | ||
source: exports.source(ws, opts && opts.onConnect), | ||
sink: exports.sink(ws, opts), | ||
//http properties - useful for routing or auth. | ||
headers: req.headers, | ||
url: req.url, | ||
upgrade: req.upgrade, | ||
method: req.method | ||
}; | ||
}; | ||
{ | ||
"name": "pull-ws", | ||
"version": "3.1.3", | ||
"version": "3.2.0", | ||
"description": "Simple pull-streams for websocket client connections", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
201
README.md
# pull-ws | ||
A simple (but effective) implementation of a | ||
[`pull-stream`](https://github.com/dominictarr/pull-stream) `Source` and `Sink` | ||
that is compatible both with native browser WebSockets and | ||
[`ws`](https://github.com/einaros/ws) created clients. | ||
Use websockets via pull-stream interface. both client and server. | ||
[](https://nodei.co/npm/pull-ws/) | ||
@@ -13,85 +9,58 @@ | ||
## Reference | ||
## example - client | ||
``` js | ||
var connect = require('pull-ws/client') | ||
// OR: require('pull-ws').connect | ||
### `pws(socket, opts?)` | ||
connect(WS_URL, function (err, stream) { | ||
if(err) throw err //handle err | ||
pull(source, stream, sink) | ||
}) | ||
turn a websocket into a duplex pull stream. | ||
If provided, `opts` is passed to `pws.sink(socket, opts)`. | ||
``` | ||
## example - server | ||
Websockets do not support half open mode. | ||
[see allowHalfOpen option in net module]( | ||
http://nodejs.org/api/net.html#net_net_createserver_options_connectionlistener) | ||
``` js | ||
var createServer = require('pull-ws/server') | ||
createServer(function (stream) { | ||
//pipe the stream somewhere. | ||
//eg, echo server | ||
pull(stream, stream) | ||
}).listen(PORT) | ||
``` | ||
If you have a protocol that assumes halfOpen connections, but are using | ||
a networking protocol like websockets that does not support it, I suggest | ||
using [pull-goodbye](https://github.com/dominictarr/pull-goodbye) with your | ||
protocol. | ||
## api | ||
The duplex stream will also contain a copy of the properties from | ||
the http request that became the websocket. they are `method`, `url`, | ||
`headers` and `upgrade`. | ||
### `connect = require('pull-ws/client')` | ||
### `pws.sink(socket, opts?)` | ||
`connect(url, cb | {binary: boolean, onConnect: cb})` | ||
Create a pull-stream `Sink` that will write data to the `socket`. | ||
`opts` may be `{closeOnEnd: true, onClose: onClose}`. | ||
`onClose` will be called when the sink ends. If `closeOnEnd=false` | ||
the stream will not close, it will just stop emitting data. | ||
(by default `closeOnEnd` is true) | ||
Create a websocket client connection. set binary: true | ||
to get a stream of arrayBuffers (on the browser). | ||
defaults to true on node, but to strings on the browser. | ||
this may cause a problems if your application assumes binary. | ||
If `opts` is a function, then `onClose = opts; opts.closeOnEnd = true`. | ||
else, just provide the callback. | ||
```js | ||
var pull = require('pull-stream'); | ||
var ws = require('pull-ws'); | ||
// connect to the echo endpoint for test/server.js | ||
var socket = new WebSocket('wss://echo.websocket.org'); | ||
// write values to the socket | ||
pull( | ||
pull.infinite(function() { | ||
return 'hello @ ' + Date.now() | ||
}), | ||
// throttle so it doesn't go nuts | ||
pull.asyncMap(function(value, cb) { | ||
setTimeout(function() { | ||
cb(null, value); | ||
}, 100); | ||
}), | ||
ws.sink(socket) | ||
); | ||
socket.addEventListener('message', function(evt) { | ||
console.log('received: ' + evt.data); | ||
}); | ||
``` js | ||
connect(url, function (err, stream) { | ||
... | ||
}) | ||
``` | ||
### `pws.source(socket)` | ||
Create a pull-stream `Source` that will read data from the `socket`. | ||
### `createServer = require('pull-ws/server') | ||
```js | ||
var pull = require('pull-stream'); | ||
create pull stream websocket servers. | ||
the servers take a lot more options than clients. | ||
// we just need the source, so cherrypick | ||
var ws = require('pull-ws/source'); | ||
`createServer(opts?, onConnection)` | ||
pull( | ||
// connect to the test/server.js endpoint | ||
ws(new WebSocket('ws://localhost:3000/read')), | ||
pull.log() | ||
); | ||
`onConnect(stream)` is called every time a connection is received. | ||
``` | ||
`opts` takes the same server options as [ws module](https://github.com/websockets/ws/blob/master/doc/ws.md#new-wsserveroptions-callback) | ||
--- | ||
# pull-ws-server | ||
#### example | ||
create pull stream websockets, servers, and clients. | ||
## example | ||
one duplex service you may want to use this with is [muxrpc](https://github.com/dominictarr/muxrpc) | ||
@@ -161,3 +130,97 @@ | ||
### core, websocket wrapping functions | ||
these modules are used internally, to wrap a websocket. | ||
you probably won't need to touch these, | ||
but they are documented anyway. | ||
### `require('pull-ws/duplex')(socket, opts?)` | ||
turn a websocket into a duplex pull stream. | ||
If provided, `opts` is passed to `pws.sink(socket, opts)`. | ||
Websockets do not support half open mode. | ||
[see allowHalfOpen option in net module]( | ||
http://nodejs.org/api/net.html#net_net_createserver_options_connectionlistener) | ||
If you have a protocol that assumes halfOpen connections, but are using | ||
a networking protocol like websockets that does not support it, I suggest | ||
using [pull-goodbye](https://github.com/dominictarr/pull-goodbye) with your | ||
protocol. | ||
The duplex stream will also contain a copy of the properties from | ||
the http request that became the websocket. they are `method`, `url`, | ||
`headers` and `upgrade`. | ||
also exposed at: `var duplex = require('pull-ws')` | ||
### `require('pull-ws/sink')(socket, opts?)` | ||
Create a pull-stream `Sink` that will write data to the `socket`. | ||
`opts` may be `{closeOnEnd: true, onClose: onClose}`. | ||
`onClose` will be called when the sink ends. If `closeOnEnd=false` | ||
the stream will not close, it will just stop emitting data. | ||
(by default `closeOnEnd` is true) | ||
If `opts` is a function, then `onClose = opts; opts.closeOnEnd = true`. | ||
```js | ||
var pull = require('pull-stream'); | ||
var wsSink = require('pull-ws'); | ||
// connect to the echo endpoint for test/server.js | ||
var socket = new WebSocket('wss://echo.websocket.org'); | ||
// write values to the socket | ||
pull( | ||
pull.infinite(function() { | ||
return 'hello @ ' + Date.now() | ||
}), | ||
// throttle so it doesn't go nuts | ||
pull.asyncMap(function(value, cb) { | ||
setTimeout(function() { | ||
cb(null, value); | ||
}, 100); | ||
}), | ||
wsSink(socket) | ||
); | ||
socket.addEventListener('message', function(evt) { | ||
console.log('received: ' + evt.data); | ||
}); | ||
``` | ||
also exposed at `require('pull-ws').sink` | ||
### `require('pull-ws/source')(socket)` | ||
Create a pull-stream `Source` that will read data from the `socket`. | ||
```js | ||
var pull = require('pull-stream'); | ||
// we just need the source, so cherrypick | ||
var wsSource = require('pull-ws/source'); | ||
pull( | ||
// connect to the test/server.js endpoint | ||
wsSource(new WebSocket('ws://localhost:3000/read')), | ||
pull.log() | ||
); | ||
``` | ||
also exposed at `require('pull-ws').source` | ||
# LICENSE | ||
MIT | ||
25999
29
673
225