angular2-websocket
angular2-websocket copied to clipboard
Can you please add usage example ...
Hi,
Can you please add usage example ... I tried to use:
let websocket = new $WebSocket("…");
let observable: Observable
message arrives to backend, it's returned (to other client), but nothing is logged in console here. Same for using subject.
By using following instead of that:
this.websocket = new $WebSocket("ws://localhost:8280/gerrit-helper/test-websocket-endpoint");
this.websocket.onMessage(function (e) { console.log(e); }, null);
let observable: Observable
this 'works', but seems really wrong. Why does subscription in first excerpt does not work? (when using Observable.range to verify it seems to work as expected).
Thanks.
I noticed several nice things in component, so I gave it a more effort to understand it.
I'm quite new to whole js world and not that familiar with rx, but following code from send method ...
return Observable.create((observer) => {
if (self.socket.readyState === self.readyStateConstants.RECONNECT_ABORTED) {
observer.next('Socket connection has been closed');
} else {
self.sendQueue.push({ message: data });
self.fireQueue();
}
});
is surprising for me. Lets assume, that we've been 'sending' messages, but destination was in failure, so messages just accumulated. Now finally destination went up. We send yet another message, using send as with previous messages.
Issues:
- Send message creates observable and does nothing, until we subscribe. That could be OK. But after we subscribe with observer, we will get only failures from that observable.
- if we do not get failure during call immediately, we won't get anything else in future, and whole subscription is pointless
- even if we do not get anything else in future, observer.complete is not called.
- even if we assume, that produced messages are produced using observer.next, and errors are correctly reported using observer.error, and complete is called as well ... it still will be problematic, since subscription to send may produce nothing, while another send may produce totally irrelevat serie of messages, while caller would probably expect, only get responses to his message from subscription origination upon his call...
"Send message creates observable and does nothing, until we subscribe." I just spent a fair amount of time tracking that down... arg!!!
Looks like the change happened back in july? https://github.com/afrad/angular2-websocket/commit/0e38070f13a22ec4208491e9b8db5f8fbd2fe2e5
See also Issue #41
that's true, but it's not inconvenience. It's fundamentally wrong I believe. Since you're not subscribing to response (what I'd expect, but that would require doing some pairing request/response which is not present). You're subscribing to 'error stream', which is counterintuitive. And this stream produces items rarely and never completes. This made me to rewrite this tool for myself...
I might be reading issue #41 and its comments wrong, but I think the consensus is "it's wrong"
The pattern from the example doesn't work. Only complete ever gets called even though inspecting the traffic you can see the response come back ok. ws.send("some thing").subscribe( (msg)=> { console.log("next", msg.data); }, (msg)=> { console.log("error", msg); }, ()=> { console.log("complete"); } );