# News Streaming

> Use the Trading Economics API streaming to obtain live news.

Source: https://docs.tradingeconomics.com/news/streaming/

The streaming service uses WebSockets, which 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 below. You need to subscribe to one or more instruments.

## Get live news

Subscribe to news streaming data

**PYTHON**
```python
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)
```

**JS**
Clone the repository
```javascript
git clone https://github.com/tradingeconomics/tradingeconomics-js
```

Access the folder
```javascript
cd tradingeconomics-js/Examples/stream-nodejs/
```

Install dependencies
```javascript
npm install
```

In app.js file, set-up your client **key** and **secret**. Then subscribe to news.
```javascript
Client = new te_client({
    url: 'wss://stream.tradingeconomics.com/',
    key: 'API_CLIENT_KEY', // <--
    secret: 'API_CLIENT_SECRET' // <--
    //reconnect: true
});

Client.subscribe('news')
```

**C#**
```c#
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}");
}
```

| title | description | country | category | url | importance | topic |
|---|---|---|---|---|---|---|
| Australia Markit Services PMI Slows Down | "The IHS Markit Australia Services PMI decreased to 53.4 in February 2021, down from..." | Australia |	Services Sentiment | /australia/services-sentiment | 2 | news |
| Australia Composite PMI Revised Downwardly | "The IHS Markit Australia Composite PMI decreased to 53.7 in February 2021, down from..." | Australia | Composite PMI | /australia/composite-pmi | 1 | news |
| "NZX Tops 12,400" | "New Zealand’s main stock index rose above the 12,400 level on Wednesday, extending..." | New Zealand | Stock Market | /new-zealand/stock-market | 1 | news |

## Response fields

| Field       | Type   | Description                   | Example                                                            |
|-------------|--------|-------------------------------|--------------------------------------------------------------------|
| title       | string | Title                         | "French 10-Year Bond Yield Approaches 1-Month High"                |
| description | string | description                   | "description"                                                      |
| country     | string | country                       | "The yield on the French 10-year OAT rose to 2.9% in mid-April..." |
| category    | string | category                      | "Government Bond 10Y"                                              |
| url         | string | url                           | "/france/government-bond-yield"                                    |
| importance  | number | Lowest 0 to 3 highest         | 3                                                                  |
| topic       | string | Topic subscribed              | "news"                                                             |

