Node.js Example
You have to two options for connecting to the Stream API.
- Use our
financial-news-api
npm package - Use your own socket.io/websocket client
1. Financial-News-API SDK
Start by installing the package with npm install financial-news-api
. Setting up a news streaming API to receive newly published articles in real-time can be done in less than 10 lines of code.
const { StreamApi } = require('financial-news-api');
const API_KEY = 'YOUR_API_KEY';
const streamApi = StreamApi(API_KEY);
streamApi.on('articles', (articles) => console.log(articles[0].title));
streamApi.on('error', (err) => console.log('Connection error ' + err));
streamApi.on('open', () => console.log('Connection open'));
streamApi.on('close', () => console.log('Connection closed'));
2. Socket.io Connection
Install socket.io for Node.js:
npm i socket.io-client@2
Please use the exact
socket.io-client
version above.
The example below creates a socket.io client, connects to our server and prints all articles as they are received.
const io = require('socket.io-client');
const apikey = 'your-api-key';
const socket = io.connect('http://stream.newsfilter.io?apiKey=' + apikey);
socket.on('connect', function () {
console.log('connected');
});
socket.on('articles', function (articles) {
console.log(articles);
});