git ssb

2+

cel / scuttlebot.io



Tree: 34d957aac81ecdd3f4531d9e49c2d663178b0b32

Files: 34d957aac81ecdd3f4531d9e49c2d663178b0b32 / 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