Dans ce billet, je présente un code simple côté serveur pour le framework Meteor qui permet d’initialiser une connexion websocket vers le serveur API de Bitstamp pour récupérer l’order book. J’ai re-utilisé le code initiale d’exemple présent sur le site de la plateforme.
Courte procédure de déploiement :
meteor create monprojet
cd monprojet
meteor npm install --save ws
meteor
cd monprojet
meteor npm install --save ws
meteor
Le code présenté est côté serveur
import { Meteor } from 'meteor/meteor';
import WebSocket from 'ws';
Meteor.startup(() => {
Meteor.call('InitializeWS');
});
Meteor.methods({
'InitializeWS'() {
desiredpair1 = "eth";
desiredpair2 = "btc";
function serializeData(data) {
bidsPlaceholderPAIR1 = [];
bidsPlaceholderPAIR2 = [];
asksPlaceholderPAIR1 = [];
asksPlaceholderPAIR2 = [];
for (i = 0; i < data.bids.length; i++) {
bidsPlaceholderPAIR1.push(data.bids[i][1]);
bidsPlaceholderPAIR2.push(data.bids[i][0]);
}
for (i = 0; i < data.asks.length; i++) {
asksPlaceholderPAIR1.push(data.asks[i][1]);
asksPlaceholderPAIR2.push(data.asks[i][0]);
}
console.log("--------------------------------------");
console.log(desiredpair1+desiredpair2 + " top bid "+parseFloat(bidsPlaceholderPAIR1[0]).toFixed(8) +" - "+parseFloat(bidsPlaceholderPAIR2[0]).toFixed(8));
console.log(desiredpair1+desiredpair2 + " top ask "+parseFloat(asksPlaceholderPAIR1[0]).toFixed(8) +" - "+parseFloat(asksPlaceholderPAIR2[0]).toFixed(8));
}
ws = new WebSocket("wss://ws.bitstamp.net");
subscribeMsg = {
"event": "bts:subscribe",
"data": {
"channel": "order_book_"+desiredpair1+desiredpair2
}
};
ws.onopen = function() {
ws.send(JSON.stringify(subscribeMsg));
};
ws.onmessage = function(evt) {
response = JSON.parse(evt.data);
/**
* This switch statement handles message logic. It processes data in case of data event
* and it reconnects if the server requires.
*/
switch (response.event) {
case 'data': {
serializeData(response.data);
break;
}
case 'bts:request_reconnect': {
initWebsocket();
break;
}
}
};
/**
* In case of unexpected close event, try to reconnect.
*/
ws.onclose = function () {
console.log('Websocket connection closed');
initWebsocket();
};
}
});
import WebSocket from 'ws';
Meteor.startup(() => {
Meteor.call('InitializeWS');
});
Meteor.methods({
'InitializeWS'() {
desiredpair1 = "eth";
desiredpair2 = "btc";
function serializeData(data) {
bidsPlaceholderPAIR1 = [];
bidsPlaceholderPAIR2 = [];
asksPlaceholderPAIR1 = [];
asksPlaceholderPAIR2 = [];
for (i = 0; i < data.bids.length; i++) {
bidsPlaceholderPAIR1.push(data.bids[i][1]);
bidsPlaceholderPAIR2.push(data.bids[i][0]);
}
for (i = 0; i < data.asks.length; i++) {
asksPlaceholderPAIR1.push(data.asks[i][1]);
asksPlaceholderPAIR2.push(data.asks[i][0]);
}
console.log("--------------------------------------");
console.log(desiredpair1+desiredpair2 + " top bid "+parseFloat(bidsPlaceholderPAIR1[0]).toFixed(8) +" - "+parseFloat(bidsPlaceholderPAIR2[0]).toFixed(8));
console.log(desiredpair1+desiredpair2 + " top ask "+parseFloat(asksPlaceholderPAIR1[0]).toFixed(8) +" - "+parseFloat(asksPlaceholderPAIR2[0]).toFixed(8));
}
ws = new WebSocket("wss://ws.bitstamp.net");
subscribeMsg = {
"event": "bts:subscribe",
"data": {
"channel": "order_book_"+desiredpair1+desiredpair2
}
};
ws.onopen = function() {
ws.send(JSON.stringify(subscribeMsg));
};
ws.onmessage = function(evt) {
response = JSON.parse(evt.data);
/**
* This switch statement handles message logic. It processes data in case of data event
* and it reconnects if the server requires.
*/
switch (response.event) {
case 'data': {
serializeData(response.data);
break;
}
case 'bts:request_reconnect': {
initWebsocket();
break;
}
}
};
/**
* In case of unexpected close event, try to reconnect.
*/
ws.onclose = function () {
console.log('Websocket connection closed');
initWebsocket();
};
}
});