WebSocket problem when trying to connect from client

Viewed 107

I'm having trouble trying to connect with websocket server.
I need to subscribe to get the current price of EUR/USD but I keep reciving this object : {topic: "keepalive"}.
It should return the price and the latest timestamp;

This is my code :

import { w3cwebsocket as W3CWebSocket } from "websocket";


export default function LogIn() {

    const client = new W3CWebSocket('ws://stream.tradingeconomics.com/?client=guest:guest');

    useEffect(()=>{
        client.onopen = function(e) {
            console.log("Connection established!");
            client.send(JSON.stringify({topic: "subscribe", to: "EURUSD:CUR"}))
            client.onmessage = function(e) { console.log(e); };
        };
        
    },[])

I would appreciate the help !

1 Answers

The problem is that you are sending a Stringified Object instead of a String:

You are sending

client.send(JSON.stringify({topic: "subscribe", to: "EURUSD:CUR"}))

You need to send

client.send('{"topic": "subscribe", "to": "EURUSD:CUR"}')
Related