@claasahl/spotware-adapter
v3.2.0
Published
Protocol Buffers and Data Models for Spotware Open API 2.0
Downloads
92
Maintainers
Readme
@claasahl/spotware-adapter
ALPHA VERSION
Use this package's SpotwareClientSocket
to communicate with Spotware Open API servers in NodeJS environments. It comes with TypeScript support for all Spotware ProtoBuffer meesages, it sticks to recommended heartbeat
-intervals and recommended rate limits (see here).
Example: Client
Want to exchange messages with a Spotware Open API server? This is for you:
import tls from "tls";
import {
SpotwareClientSocket,
ProtoOAPayloadType,
FACTORY,
} from "@claasahl/spotware-adapter";
const host = "demo.ctraderapi.com";
const port = 5035;
const socket = tls.connect(port, host);
socket.once("secureConnect", () => {
console.log(`requesting version number from ${host}:${port}`);
s.write(FACTORY.PROTO_OA_VERSION_REQ());
});
const s = new SpotwareClientSocket(socket);
s.on("data", (message) => {
switch (message.payloadType) {
case ProtoOAPayloadType.PROTO_OA_VERSION_RES:
console.log(`got response from ${host}:${port}`);
console.log(`server version: ${message.payload.version}`);
s.end();
break;
// ...
}
});
Or take this RunKit for a spin: https://runkit.com/claasahl/spotware-example-client
Example: Server
Want to test your client without sending messages over the wire? Take a peek at this:
import { Server } from "net";
import {
SpotwareSocket,
ProtoOAPayloadType,
FACTORY,
} from "@claasahl/spotware-adapter";
const port = 5035;
const server = new Server((socket) => {
const address = socket.address();
const source = typeof address === "string" ? address : address.address;
console.log(`${source} connected`);
const s = new SpotwareSocket(socket);
s.on("error", (err) => console.log(source, err));
s.on("data", (message) => {
const { clientMsgId } = message;
switch (message.payloadType) {
case ProtoOAPayloadType.PROTO_OA_VERSION_REQ:
s.write(FACTORY.PROTO_OA_VERSION_RES({ version: "00" }, clientMsgId));
break;
// ...
}
});
});
server.listen(port, () => console.log(`listening on port ${port}`));
Getting Started
For a meaningful interaction with a Spotware Open API server, such as live.ctraderapi.com:5035
or demo.ctraderapi.com:5035
, you will need:
- an account at Spotware 🥁
- a set of OAUTH credentials (i.e.
clientId
andclientSecret
)- navigate to applications
- view credentials for one of your applications (or create a new application)
- a set of OAUTH tokens (i.e.
accessToken
andrefreshToken
)- navigate to applications
- open
playground
for one of your applications - fetch tokens for one or more of your accounts
Debugging
This package uses debug
for logging. These namespaces are supported:
spotware.input
incoming message from serverspotware.input.human
incoming message from server with humanreadable payloadTypespotware.output
outgoing message to serverspotware.output.human
outgoing message to server with humanreadable payloadTypespotware.${payloadType}
any proto message (i.e. incoming or outgoing)spotware.${payloadTypeText}
any proto message (i.e. incoming or outgoing)