git ssb

2+

cel / scuttlebot.io



Tree: 07de6e0bf8f8051cfea29058496ca70ae04e5c99

Files: 07de6e0bf8f8051cfea29058496ca70ae04e5c99 / tmpl / apis / pull-stream / pull-ws-server.md

1130 bytesRaw

pull-ws-server

create pull stream websockets, servers, and clients.

example

one duplex service you may want to use this with is muxrpc

var ws = require('pull-ws-server')
var pull = require('pull-stream')

ws.createServer(function (stream) {
  //pipe duplex style to your service.
  pull(stream, service.createStream(), stream)
})
.listen(9999)

var stream = ws.connect('ws://localhost:9999')

pull(stream, client.createStream(), stream)

if the connection fails, the first read from the stream will be an error, otherwise, to get a handle of stream end/error pass a callback to connect.

ws.connect('ws://localhost:9999', function (err, stream) {
  if(err) return handleError(err)
  //stream is now ready
})

To run the server over TLS:

var tlsOpts = {
  key: fs.readFileSync('test/fixtures/keys/agent2-key.pem'),
  cert: fs.readFileSync('test/fixtures/keys/agent2-cert.pem')
};
ws.createServer(tlsOpts, function (stream) {
  //pipe duplex style to your service.
  pull(stream, service.createStream(), stream)
})
.listen(9999)

License

MIT

Built with git-ssb-web