Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do you know when the stream ends? #19

Open
nichoth opened this issue May 6, 2017 · 6 comments
Open

How do you know when the stream ends? #19

nichoth opened this issue May 6, 2017 · 6 comments
Labels

Comments

@nichoth
Copy link
Contributor

nichoth commented May 6, 2017

How do you tell when a ws connection closes if you are using pull-ws and muxrpc? I see pull-goodbye but I'm not sure how to use it.

@dominictarr
Copy link
Member

pull-goodbye is already used in muxrpc https://github.com/ssbc/muxrpc/blob/master/stream.js#L99-L101

are you getting a ws stream that ends and muxrpc doesn't realize?

@nichoth
Copy link
Contributor Author

nichoth commented May 6, 2017

hmm. I'm not sure where to get a cb for the end event. We have a normal situation on the server like:

ws({ server: server }, function onWsConnect (wsStream) {
   pull( myService, wsStream, myService )
})

Thanks

@nichoth
Copy link
Contributor Author

nichoth commented May 7, 2017

A through stream like this works. Is this a good way to do it?

function OnEnd (cb) {
    return function sink (read) {
        return function source (abort, _cb) {
            if (abort) {
                console.log('abort', abort)
                cb()
            }
            read(abort, function onNext (end, data) {
                if (end === true) cb()
                _cb(end, data)
            })
        }
    }
}

@dominictarr
Copy link
Member

one way is that rpc.createStream(cb) has takes a callback,
so you can do:

createServer(function (stream) {
  var serverRPC = MRPC(null, api) ({
    hello: function (name, cb) {
      cb(null, 'hello, ' + name + '!')
    } //etc...
  })

  var serverStream = serverRPC.createStream(function callback (err) {
    //this is called when the connection ends
  })

  pull(stream, serverStream, stream) //pipe together
})

note, that when the stream ends, you can create another stream and keep that rpc object, which you might want to do on client objects, but not servers.

@dominictarr
Copy link
Member

ah, also the rpc object emits a closed event. which is how it's done in secret-stack

@nichoth
Copy link
Contributor Author

nichoth commented May 7, 2017

Thanks @dominictarr ! I went ahead and submitted a PR in muxrpc to add those to the readme.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants