websocket - myWebSocketSubject.multiplex(..).subscribe().unsubscribe() closes connection, event further observers exists -
the following code close connection, event further observers exists on mywebsocketsubject:
mywebsocketsubject.observable.websocket('ws://mysocket'); mywebsocketsubject.subscribe(); mywebsocketsubject.multiplex(..).subscribe().unsubscribe() // connection closed
my expectation was, connection gets closed last unsubscribe() call (and not first one).
use case
if right, multiplex(..)
operator, on create , complete message send socket, e.g. allows un-/subscribe on server side specific event.
my preferred web socket service therefore below. there exists 1 connection, , single connection provides several streams. on first subscription web socket connection gets created; , last unsubscribe call connection gets closed. each data-stream un-/subscribe message sent once.
i haven't found solution use websocketsubject.multiplex(..)
method...
preferred example web socket service
export class websocketservice { connection: websocketsubject<any>; constructor() { this.connection = observable.websocket<any>(_createconfig()) } datastream(eventtype: string): observable<websocketmessage> { return connection.multiplex( () => new websocketmessage("websocket.subscribe." + eventtype), () => new websocketmessage("websocket.unsubscribe." + eventtype), message => (message.type == eventtype) ) .retry() // reconnect on error , send subscription messages again .share(); // send messages on last/fist un-/subscribe on stream } // ... } export class websocketmessage { type: string; data: any; constructor(command: string, data?:any) { this.type = command; this.data = data || undefined; } }
i have written following test case fails...
it('should able handle multiple subscriptions', () => { const subject = observable.websocket(<any>{url: 'ws://mysocket'}); const sub1 = subject.subscribe(); const sub2 = subject.subscribe(); const socket = mockwebsocket.lastsocket; socket.open(); sinon.spy(socket, 'close'); sub1.unsubscribe(); // fails, because socket gets closed on first unsubscribe expect(socket.close).have.not.been.called; sub2.unsubscribe(); expect(socket.close).have.been.called; });
if right share
operator trick. after using operator, multiplex method not available.
thanks feedback, input, ...!