WebSocket object.
Security Note: Exposing your API key in client-side code (like this example) is risky because anyone can view the source code and steal your key.For production applications, we recommend using a backend proxy.
Full Example
- index.html
- websocket.js
index.html
Copy
<!DOCTYPE html>
<html>
<body>
<script src="websocket.js"></script>
<pre id="output">Example API output:
-------------------
</pre>
<script>
var protocb = new Object(),
wscb = new Object(),
ws,
pre = document.getElementById('output');
protocb.__default = null;
protocb.on0 = function() {
print('Connected.');
};
protocb.on1 = function(data) {
print('Data received: ' + JSON.stringify(data));
};
protocb.on2 = function() {
print('Ping received.');
};
protocb.on7 = function(error) {
print('Error:' + error);
};
protocb.on8 = function(error) {
print('Error:' + error);
};
protocb.on9 = function(error) {
print('Error:' + error);
};
protocb.fatal = function(error) {
print('Error:' + error);
};
wscb.onopen = function() {
print('Connection opened.');
};
wscb.onclose = function() {
print('Connection closed.');
};
wscb.onerror = function() {
print('Fatal error.');
};
var p = new Proto('wss://api.xchangeapi.com/websocket/live?api-key=<YOUR_API_KEY>', wscb, protocb);
p.start(['EURUSD', 'GBPCHF']);
function print(message) {
console.log(message);
pre.innerHTML += message + "\n"
}
</script>
</body>
</html>
Copy
var Proto = function _Proto(wsUri, wscb, protocb, opts) {
this.start_time = 0;
this.wsUri = wsUri;
this.time_mult = 1;
this.mapping = {};
this.order = [];
this.wscbs = wscb;
this.protocb = protocb;
this.opts = opts || {};
};
Proto.prototype.start = function(pairs) {
var ws = new WebSocket(this.wsUri);
var self = this;
ws.onmessage = function(evt) {
return self.process(evt.data);
}
ws.onclose = this.wscbs.onclose;
ws.onopen = function(evt) {
self.wscbs.onopen(evt);
ws.send(self.pack({"pairs": pairs, "options": self.opts}))
};
ws.onerror = this.wscbs.onerror;
}
Proto.prototype.process = function (data) {
var t = data.substring(0, 1);
var msg = data.substring(1);
cb = this.protocb['on' + t]
var inc_data = null;
switch (t) {
case '0':
inc_data = this.unpackInit(msg);
break;
case '7':
case '8':
case '9':
inc_data = this.unpackErrPair(msg);
break;
case '1':
inc_data = this.unpackData(msg);
break;
case '2':
inc_data = "";
break
default:
break;
}
if(inc_data != null) {
if(cb) {
cb(inc_data);
} else {
if(cb === null) { return } // quiet mode
var default_cb = this.protocb['__default'];
if(default_cb) {
default_cb(inc_data);
} else {
this.protocb.fatal(data);
}
}
} else {
this.protocb.fatal(data);
}
};
Proto.prototype.unpackErrPair = function (data) {
return JSON.parse(data);
};
Proto.prototype.unpackInit = function (data) {
var meta = JSON.parse(data);
this.start_time = meta['start_time'];
this.mapping = meta['mapping'];
this.order = meta['order'];
this.time_mult = meta['time_mult'];
return meta;
};
Proto.prototype.unpackData = function(data) {
var inc = data.split('|');
var out = {};
for (var i in this.order) {
out[this.order[i]] = inc[i];
};
out["name"] = this.mapping[out["name"]];
out["time"] = parseFloat(out["time"]) / this.time_mult;
out["time"] += this.start_time;
return out;
};
Proto.prototype.pack = function(data) {
return JSON.stringify(data);
};
Proto.prototype.toString = function(data) {
return JSON.stringify(data);
};
Running Locally
To run this example on your machine:-
Create a new folder for the project:
Copy
mkdir websocket-example cd websocket-example -
Download the example files:
Copy
curl -O https://xchangeapi.com/docs/websocket/example-implementation/js/index.html curl -O https://xchangeapi.com/docs/websocket/example-implementation/js/websocket.js -
Open
index.htmlin your text editor and replace<YOUR_API_KEY>with your actual API key. For more details on API keys, see the Authentication page. -
Open
index.htmlin your web browser. You can do this by double-clicking the file or using the command line:
Copy
open index.html
