I need to get currency quotes in real time, without reloading the page and clicking on buttons.
To solve this problem, we had to resort to the following technologies: node js, websocket.io, Redis, data donor API.
In the console I get the following data:
{"symbol": "EURUSD", "bid": 1.13624, "ask": 1.13624, "price": 1.13624}
in fact, everything works, and this is what I need. But, I can not figure out how to transfer the same data to the browser, so that it would be like this:
EURUSD | bid - 1.13624 | ask - 1.13624 | price - 1.13624
Here is the js code:
const redis = require('redis');
const settings = {
REDIS: {
HOST: 'localhost'
}
}
let redisClient = new redis.createClient(settings.REDIS);
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
res.sendFile(__dirname + '/index.html');
});
http.listen(3000, function(){
console.log('listening on *:3000');
});
function getQuote(symbol, callback) {
redisClient.get(symbol, (error, quote) => {
if (error) {
throw new Error(error);
}
quote = JSON.parse(quote);
callback(quote);
});
}
setInterval(() => {
getQuote('EURUSD', (q) => {
const formattedQ = `${q.SYMBOL} | bid - ${q.bid} | ask - ${q.ask} | price - ${q.price}`;
console.log(formattedQ);
});
}, 10);
io.on('connection', function(socket){
socket.on('get_quote', function(msg){
io.emit('get_quote', getQuote('EURUSD', (q) => /* ??? what to insert here? -> (formattedQ) ??? */));
});
});
And HTML file:
<script src="/socket.io/socket.io.js"></script>
<script src="https://code.jquery.com/jquery-1.11.1.js"></script>
<script>
$(function () {
var socket = io();
socket.on('get_quote', function(msg){
// ??? what to insert here to update quotes without rebooting ???
});
});
</script>
I think a more suitable approach would be to send the json as-is, and format it in the client.
/* redis connection code */
setInterval(() => {
getQuote('EURUSD', (q) => {
const formattedQ = `${q.SYMBOL} | bid - ${q.bid} | ask - ${q.ask} | price - ${q.price}`;
console.log(formattedQ);
});
}, 10);
I'm revising the answer because I think it would be easier to show a piece of code I believe would work. In order to send data from the server to the client, you should use the io.emit()
function, which you do - but you wrapped it in a socket.on('get_quote')
. Basically what will happen is that you will emit a new message only if a message was received (resulting in a deadlock).
I believe this snippet should work:
io.on('connection', function(socket){
setInterval(() => {
getQuote('EURUSD', (q) => {
io.emit('get_quote', q);
});
}, 10);
});
This would initialize an interval when there is a connection to the server, and emit a new q
every 10 seconds.
Then, in your HTML file you should receive the q (in JSON form), and format it the way you choose:
socket.on('get_quote', function(msg){
console.log(`${msg.SYMBOL} | bid - ${msg.bid} | ask - ${msg.ask} | price - ${msg.price}`);
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.