-
Notifications
You must be signed in to change notification settings - Fork 52
How would I send a callback styled response to a client side event emitter #5
Comments
I'm wondering the same thing. This is a bit of a dealbreaker if it's not possible, because it would require refactoring all of my client code. |
Is this project still active? I'll be happy to give this issue a go and look into how to implement it, I was working on a similar idea then found your repository, that, while not exactly what I'm looking for, seems quite useful and I would be happy to contribute back |
I currently solve this problem like this: https://github.com/rashfael/koa.io/commit/3caf742fb1c3163335f3f5eee9cfbe0b1c629e7c I can then do: app.io.route('someEvent', function *(next, data) {
// do something with data...
this.ack('client done processing data...');
}); This is not the koa/generators way, and @knowthen's proposal sounds much better and looks doable, but I started digging into ES6 and koa just days ago and haven't figured out all the bits yet. |
Well, few weeks of node under the belt here, so not really an expert... Said that, I think that what @knowthen propose is doable. Socket.prototype.__defineSetter__('body', function (ack_params) {
this.ack(ack_params);
}); then, you should be able to do this in your app: app.io.route('someEvent', function *(next, data){
// do something with data...
this.body = 'client done processing data...';
}); I will update my github fork when I got access to my dev box. Please bear in mind that I have no idea of performance or other consequences of this approach, but it does seem in line with the logic in the rest of the koa.io code |
Using a setter is a good idea, but I think we should not directly invoke the socket.io ack in the setter. I just had a look into the response handling in koa, and it looks like it is solved as a privileged middleware (https://github.com/koajs/koa/blob/0ad06c9810079174660fa4948ca183d9b90efd3c/lib/application.js#L179-L220), it always gets invoked first. We probably should adapt the same approach. |
I prototyped this here: https://github.com/rashfael/koa.io/commit/153f1c8c5c25729f3cefeb77888db92b8d2fca55 |
cool. It's the same approach taken in the |
@bambalaus |
@hallas do you mean that it does the same? As mentioned I'm not a nodejs expert at all, but If this is the case I think that it would be better to stick to the usage pattern as defined in the official co wiki (with |
Any progress on implementing this support in some form? Without this functionallity is is a bit of a dealbreaker for me as well. |
@mattiasrunge: Not AFAIK, but I will try to got to it soon, probobly this weekend or next |
Ok, this will defiantly be in v0.1.0. Here's what I'm thinking of, syntax wise (I'm open to suggestions though): app.io.route('getStatus', function* (next, data) {
// do something
this.body = 'awesome';
}); |
The parameters for the acknowledgment in socket.io are in an array. Callbacks accept multiple parameters. I often use the (error, data) pattern. To reflect this, should this.body be an array? this.body = [null, {_id: 3, text: 'some content'}]; For usability, we could automatically wrap a single value into an array. |
@rashfael PRs welcome and we can continue discussion there? |
I just noticed koa.io today, pretty slick!
I think I will do a screencast on this soon.
I was looking for a way to do a callback in response to an event.
With normal socket.io it would be done like below 'cb'
I looked through the code and it doesn't seem to be possible, unless I'm missing something.
It would be pretty cool to be able to do something like
and have the message 'client done processing data...' get sent back as the callback to the clientside emitter.
Your thoughts?
Thanks!
The text was updated successfully, but these errors were encountered: