Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

angular2 http request and websocket [duplicate]

Hi all I've an application that until now was only doing http request to retrieve data...now the problem is that we have also a websocket but I've no idea how to combine both of them:

This is my http request:

 public stock(): Observable<any> {
    if (!this._stock) {
        // rest api
        this._stock = this.http.get(url)
            .map((stock: Response) => {
                // cache the value
                stock.json().items.forEach((item) => {
                    this._stock[item.productKey] = item;
                });
                return this._stock;
            })
            .publishReplay(1)
            .refCount();
    }
    return this._stock;
}

This is the websocket:

public getStockMessages(): Observable<any> {
    return new Observable(obs => this.inventoryChannel.on(`STOCK`, (data) => {
        return obs.next(data);
    }));
}
like image 855
d.zurico Avatar asked Apr 15 '26 04:04

d.zurico


1 Answers

Given what you've said above, it seems like you're looking to:

  1. Request a single value from the server.
  2. Get additional values from a web socket.
stock().concat(getStockMessages())

... is probably what you want.

But honestly I'm still having a hard time figuring out exactly what you're trying to do... but I think this will solve your problem.

like image 153
Ben Lesh Avatar answered Apr 19 '26 15:04

Ben Lesh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!