NestJS How to get result of web socket stream into browser or postman


I’m developing NestJS App which creates a WebSocket to Binance API. I want to get stream output into browser window or postman.
But now I can get stream output only in the console. I can’t understand how to send these streams in the browser.

Also, I tried to return results with Promises and got only the first result of the return. In the console, I got all stream output items. Please help me.

A class that creates WS stream Coin.ts

import { GetCryptocurrencies } from "./abstract/get-cryptocurrencies";
import { WebSocket } from "ws";
import { Logger } from "@nestjs/common";

export class Coin extends GetCryptocurrencies {
    private readonly logger = new Logger(Coin.name)
    private baseUrl: string
    private url: string
    constructor(coin: { name: string, symbol: string }[]) {
        super(coin)
        this.baseUrl = 'wss://stream.binance.com:9443/stream?streams='
        this.url = coin.map((c) => {
            return `${c.symbol.toLowerCase()}[email protected]`
        }).join('/')
        }

    getCryptoData(): any {
        return new Promise((response, rej) => {

            const ws = new WebSocket(`${this.baseUrl}${this.url}`)
            ws.on('open', () => {
                this.logger.log('Connection established')
            })
            ws.onmessage = (msg: any) => {
                const message = JSON.parse(msg.data)
                console.log(message)
                response(message)
            }
        })
    }
}

A service get-data.service.ts

import { Injectable } from '@nestjs/common';
import { Coin } from 'src/classes/coin';
import * as coinlist from '../list/coins.json'

@Injectable()
export class GetDataService {
    getCoins() {
        const coins = new Coin(coinlist)
        return coins.getCryptoData()
    }
}

A controller get-data.controller.ts

import { Controller, Get, Response } from '@nestjs/common';
import { GetDataService } from './get-data.service';

@Controller('getdata')
export class GetDataController {
    constructor(private getDataService: GetDataService){}

    @Get()
    getCoinsData(@Response() res: any ) {
        return this.getDataService.getCoins();
    }
}

My output in Postman/Browser window contains only first item

{
    "stream": "[email protected]",
    "data": {
        "e": "24hrMiniTicker",
        "E": 1637335935935,
        "s": "ETHUSDT",
        "c": "4214.07000000",
        "o": "4123.42000000",
        "h": "4238.50000000",
        "l": "3956.44000000",
        "v": "573727.18610000",
        "q": "2336469218.64227900"
    }
}

Source: JavaSript – Stack Overflow

November 19, 2021
Category : News
Tags: javascript | nestjs | node-streams | node.js | websocket

Leave a Reply

Your email address will not be published. Required fields are marked *

Sitemap | Terms | Privacy | Cookies | Advertising

Senior Software Developer

Creator of @LzoMedia I am a backend software developer based in London who likes beautiful code and has an adherence to standards & love's open-source.