Angular2 http request and websocket

Hi everyone, I have an application that so far only made an HTTP request to fetch data ... now the problem is that we also have a web application, but I don't know how to combine 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 website:

public getStockMessages(): Observable<any> {
    return new Observable(obs => this.inventoryChannel.on(`STOCK`, (data) => {
        return obs.next(data);
    }));
}

      

+3


source to share


1 answer


Considering what you said above, it seems what you are looking for:

  • Request one value from the server.
  • Get additional values ​​from a websocket.
stock().concat(getStockMessages())

      



... maybe what you want.

But to be honest, I still have a hard time figuring out what you are trying to do ... but I think this will solve your problem.

+2


source







All Articles