Skip to content

Commit

Permalink
Add ping method
Browse files Browse the repository at this point in the history
fixes: #6
  • Loading branch information
DenisCarriere committed Oct 4, 2023
1 parent 1927cd3 commit a10bfff
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 12 deletions.
12 changes: 7 additions & 5 deletions examples/bun/client.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import "dotenv/config"
const ws = new WebSocket("ws://localhost:3000");
import { randomUUID } from "crypto";

const MODULEHASH = String(process.env.MODULEHASH);
const ws = new WebSocket("ws://localhost:3000");

ws.onopen = () => {
console.log("Connected!");
ws.send(MODULEHASH)
console.log("connected");
ws.send(JSON.stringify({
id: randomUUID(),
method: "ping"
}));
};

ws.onmessage = (event) => {
Expand Down
31 changes: 24 additions & 7 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import * as sqlite from "./src/sqlite.js";
import * as prometheus from "./src/prometheus.js";
import { checkHealth } from "./src/health.js";
import { toJSON } from "./src/http.js";
import { parseMessage } from "./src/parseMessage.js";
console.log(`Server listening on http://${HOSTNAME || "0.0.0.0"}:${PORT}`);
console.log("Verifying with PUBLIC_KEY", PUBLIC_KEY);
console.log("Reading SQLITE_FILENAME", SQLITE_FILENAME);
Expand Down Expand Up @@ -111,19 +112,35 @@ Bun.serve<{key: string}>({
console.log('close', {key: ws.data.key, remoteAddress: ws.remoteAddress, code, reason});
},
message(ws, message) {
// Handle Pings
// TO-DO: maybe to be removed??
// https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers#pings_and_pongs_the_heartbeat_of_websockets
if ( message === "0x9" || message === "pong" ) {
const { id, method, params } = parseMessage(message);

// validate request
if ( id === null ) {
const msg = 'Missing required \'id\' in JSON request.';
console.log(message, {key: ws.data.key, remoteAddress: ws.remoteAddress, message});
ws.send(JSON.stringify({id: null, status: 400, error: {msg}}));
ws.close();
return;
}
if ( method === null ) {
const msg = 'Missing required \'method\' in JSON request.';
console.log(message, {key: ws.data.key, remoteAddress: ws.remoteAddress, message});
ws.send(JSON.stringify({id: null, status: 400, error: {msg}}));
ws.close();
return;
}
// ping
// https://developers.binance.com/docs/binance-trading-api/websocket_api#test-connectivity
if ( method === "ping" ) {
prometheus.total_pings.inc(1);
console.log('ping', {key: ws.data.key, remoteAddress: ws.remoteAddress});
ws.pong();
if ( message == "0x9" ) ws.send("0xA");
else ws.send("pong");
ws.send(JSON.stringify({id, status: 200, result: {}}));
return;
}

// Handle Subscribe
// TO-DO: improve error formatting
// https://github.com/pinax-network/substreams-sink-websockets/issues/9
const moduleHash = String(message);
if ( ws.isSubscribed(moduleHash) ) {
ws.send(JSON.stringify({message: `⚠️ Already subscribed to ${moduleHash}.`}));
Expand Down
7 changes: 7 additions & 0 deletions src/parseMessage.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { expect, test } from "bun:test";
import { parseMessage } from "./parseMessage.js";

test("parseMessage", () => {
// TO-DO
expect(true).toBeTruthy();
});
8 changes: 8 additions & 0 deletions src/parseMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function parseMessage(message: string|Buffer): {id: string, method: string, params: Object } {
try {
const json = JSON.parse(message.toString());
return json;
} catch (error) {
return {id: null, method: null, params: {}};
}
}

0 comments on commit a10bfff

Please sign in to comment.