Skip to content

Commit

Permalink
socket connection example improved
Browse files Browse the repository at this point in the history
  • Loading branch information
poly-rodr committed May 13, 2024
1 parent 1690f1f commit 6b53a06
Showing 1 changed file with 42 additions and 20 deletions.
62 changes: 42 additions & 20 deletions examples/socketConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ dotenvConfig({ path: resolve(__dirname, "../.env") });
import { WebSocket } from "ws";
import { ApiKeyCreds } from "../src";

// eslint-disable-next-line max-len
const YES_TOKEN_ID =
"71321045679252212594626385532706912750332728571942532289631379312455583992563";
// eslint-disable-next-line max-len
const NO_TOKEN_ID = "52114319501245915516055106046884209969926127482827954674443846427813813222426";
// eslint-disable-next-line max-len
const CONDITION_ID = "0x5f65177b394277fd294cd75650044e32ba009a95022d88a0c1d565897d72f8f1";

interface subscriptionMessage {
auth: { apiKey: string; secret: string; passphrase: string };
// only necessary for 'user' subscriptions
auth?: { apiKey: string; secret: string; passphrase: string };
type: string;
markets: string[];
assets_ids: string[];
Expand All @@ -29,18 +32,24 @@ async function main(type: "user" | "market" | "live-activity") {
let subscriptionMessage: subscriptionMessage = {} as subscriptionMessage;

if (type !== "live-activity") {
const creds: ApiKeyCreds = {
key: `${process.env.CLOB_API_KEY}`,
secret: `${process.env.CLOB_SECRET}`,
passphrase: `${process.env.CLOB_PASS_PHRASE}`,
};
let creds: ApiKeyCreds | undefined;
if (type == "user") {
creds = {
key: `${process.env.CLOB_API_KEY}`,
secret: `${process.env.CLOB_SECRET}`,
passphrase: `${process.env.CLOB_PASS_PHRASE}`,
};
}

subscriptionMessage = {
auth: {
apiKey: creds.key,
secret: creds.secret,
passphrase: creds.passphrase,
},
auth:
type == "user" && creds
? {
apiKey: creds.key,
secret: creds.secret,
passphrase: creds.passphrase,
}
: undefined,
type, // change to market for market, user for user
markets: [] as string[],
assets_ids: [] as string[],
Expand All @@ -49,29 +58,42 @@ async function main(type: "user" | "market" | "live-activity") {
if (type == "user") {
subscriptionMessage["markets"] = [CONDITION_ID];
} else {
subscriptionMessage["assets_ids"] = [YES_TOKEN_ID, NO_TOKEN_ID];
// subscriptionMessage["assets_ids"] = [NO_TOKEN_ID];
subscriptionMessage["assets_ids"] = [NO_TOKEN_ID, YES_TOKEN_ID];
}
}

ws.on("open", function () {
ws.on("error", function (_: any, err: Error) {
console.log("error SOCKET", "error", err);
process.exit(1);
});
ws.on("close", function (code: number, reason: Buffer) {
console.log("disconnected SOCKET", "code", code, "reason", reason.toString());
process.exit(1);
});

ws.on("open", function (ev: any) {
if (type !== "live-activity") {
ws.send(JSON.stringify(subscriptionMessage)); // send sub message
ws.send(JSON.stringify(subscriptionMessage), (err?: Error) => {
if (err) {
console.log("send error", err);
process.exit(1);
}
}); // send sub message
}

setInterval(() => {
console.log("PINGING");
ws.send("PING");
}, 50000);

if (ev) {
console.log("open", ev);
}
});

ws.onmessage = function (msg: any) {
console.log(msg.data);
};

ws.on("close", function (ev: any) {
console.log("disconnected SOCKET - PORT : 5000, reason: " + ev);
});
}

main("market");
main("user");

0 comments on commit 6b53a06

Please sign in to comment.