@claasahl/spotware-adapter v3.2.0
@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.
clientIdandclientSecret)- navigate to applications
- view credentials for one of your applications (or create a new application)
- a set of OAUTH tokens (i.e.
accessTokenandrefreshToken)- navigate to applications
- open
playgroundfor 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.inputincoming message from serverspotware.input.humanincoming message from server with humanreadable payloadTypespotware.outputoutgoing message to serverspotware.output.humanoutgoing 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)
Further Reading
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
7 years ago
7 years ago
7 years ago
7 years ago