afrad / angular2-websocket

Websocket wrapper for angular2 based on angular-websocket
Apache License 2.0
210 stars 84 forks source link

Can you please add usage example ... #47

Open martin-mucha opened 8 years ago

martin-mucha commented 8 years ago

Hi,

Can you please add usage example ... I tried to use:

let websocket = new $WebSocket("…"); let observable: Observable = this.websocket.send("test"); observable.subscribe( value=>console.log("nextValue: " + value), err=>console.log("error: " + err), ()=>console.log("done.") );

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.websocket.send("test"); and subscription to cold 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.

martin-mucha commented 8 years ago

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:

  1. 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.
  2. if we do not get failure during call immediately, we won't get anything else in future, and whole subscription is pointless
  3. even if we do not get anything else in future, observer.complete is not called.
  4. 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...
bkdotcom commented 8 years ago

"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

martin-mucha commented 8 years ago

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...

bkdotcom commented 8 years ago

I might be reading issue #41 and its comments wrong, but I think the consensus is "it's wrong"

jquince commented 6 years ago

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"); } );