Streaming

The streaming service uses websockets (WebSockets provide a bidirectional, full-duplex communications channel that operates over HTTP through a single TCP/IP socket connection). At its core, the WebSocket protocol facilitates message passing between a client and server. You can connect to our streaming service using the examples listed on the black pane. You need to subscribe to one or more instruments.

Get live news

Subscribe to news streaming data

import tradingeconomics as te
import json
te.login('you_private_key')

def on_message(ws, message):
    print(json.loads(message))

te.subscribe('news')
te.run(on_message)

Clone the repository

git clone https://github.com/tradingeconomics/tradingeconomics

Access the folder

cd tradingeconomics/nodejs/Examples/stream-nodejs

Install dependencies

npm install

In app.js file, set-up your client key and secret. Then subscribe to news.

Client = new te_client({
    url: 'wss://stream.tradingeconomics.com/',
    key: 'API_CLIENT_KEY', // <--
    secret: 'API_CLIENT_SECRET' // <--
    //reconnect: true
});

Client.subscribe('news')
using System.Net.WebSockets;
using System.Text;

try
{
    using (var cws = new ClientWebSocket())
    {
        await cws.ConnectAsync(new Uri($"wss://stream.tradingeconomics.com/?client='your_private_key'"), CancellationToken.None);

        if (cws.State == WebSocketState.Open)
        {
            var buffer = new ArraySegment<byte>(Encoding.UTF8.GetBytes(@"{""topic"": ""subscribe"", ""to"": """ + "news" + @""" }"));
            await cws.SendAsync(buffer, WebSocketMessageType.Binary, true, CancellationToken.None);
        }

        await Task.Delay(1024);

        while (cws.State == WebSocketState.Open)
        {
            var buffer = new ArraySegment<byte>(new byte[1024]);
            var result = await cws.ReceiveAsync(buffer, CancellationToken.None);
            if (result.MessageType == WebSocketMessageType.Close)
            {
                await cws.CloseAsync(WebSocketCloseStatus.NormalClosure, string.Empty, CancellationToken.None);
            }

            Console.WriteLine($"Receiving: {Encoding.UTF8.GetString(buffer.Array, 0, result.Count).Trim()}");
        }
    }
}
catch (Exception e)
{
    Console.WriteLine($"Error with message: {e.Message}");
}

Response fields

FieldTypeDescriptionExample
titlestringTitle“French 10-Year Bond Yield Approaches 1-Month High”
descriptionstringdescription“description”
countrystringcountry“The yield on the French 10-year OAT rose to 2.9% in mid-April…”
categorystringcategory“Government Bond 10Y”
urlstringurl“/france/government-bond-yield”
importancenumberLowest 0 to 3 highest3
topicstringTopic subscribed“news”