Skip to content

Commit

Permalink
feat: improved logs for websockets
Browse files Browse the repository at this point in the history
- also fixed aztec-listener
- also improved local development for websockets
  • Loading branch information
FilipHarald committed Oct 11, 2024
1 parent f3c9799 commit a271993
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 12 deletions.
11 changes: 11 additions & 0 deletions k8s/local/skaffold.local.light.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ build:
buildArgs:
NODE_ENV: "development"
VITE_API_URL: "http://explorer-api.localhost:80/v1/d1e2083a-660c-4314-a6f2-1d42f4b944f4"
VITE_WS_URL: "ws://ws.localhost:80" # TODO: use API_KEY
requires:
- image: chicmoz-base
alias: BASE
Expand All @@ -46,6 +47,13 @@ build:
requires:
- image: chicmoz-base
alias: BASE
- image: websocket-event-publisher
context: services/websocket-event-publisher
docker:
dockerfile: Dockerfile
requires:
- image: chicmoz-base
alias: BASE
manifests:
rawYaml:
- k8s/local/common/namespace.yaml
Expand All @@ -55,6 +63,9 @@ manifests:
- k8s/local/explorer-api/ingress.yaml
- k8s/local/explorer-api/deployment.yaml
- k8s/local/explorer-api/service.yaml
- k8s/local/websocket-event-publisher/ingress.yaml
- k8s/local/websocket-event-publisher/deployment.yaml
- k8s/local/websocket-event-publisher/service.yaml
- k8s/local/auth/deployment.yaml
- k8s/local/auth/service.yaml
# - k8s/local/dummy-node/ingress.yaml
Expand Down
2 changes: 1 addition & 1 deletion k8s/local/skaffold.local.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ build:
buildArgs:
NODE_ENV: "development"
VITE_API_URL: "http://explorer-api.localhost:80/v1/d1e2083a-660c-4314-a6f2-1d42f4b944f4"
WS_URL: "ws://ws.localhost:80" # TODO: use API_KEY
VITE_WS_URL: "ws://ws.localhost:80" # TODO: use API_KEY
requires:
- image: chicmoz-base
alias: BASE
Expand Down
2 changes: 1 addition & 1 deletion services/aztec-listener/src/aztec/poller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ let pollInterval: NodeJS.Timeout;
let latestProcessedHeight = -1;

export const startPolling = ({ fromHeight }: { fromHeight: number }) => {
latestProcessedHeight = fromHeight;
latestProcessedHeight = fromHeight - 1;
pollInterval = setInterval(() => {
void fetchAndPublishLatestBlockReoccurring();
}, BLOCK_POLL_INTERVAL_MS);
Expand Down
40 changes: 30 additions & 10 deletions services/websocket-event-publisher/src/ws-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,36 @@ let wss: WebSocketServer;

export const sendBlockToClients = (block: ChicmozL2Block) => {
if (!wss) throw new Error("WebSocket server is not initialized");
const clientStatuses: {
sent: number;
failed: number;
} = {
sent: 0,
failed: 0,
};
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN)
client.send(JSON.stringify(block));
else logger.warn(`WS: client not connected!`);
if (client.readyState === WebSocket.OPEN) {
try {
client.send(JSON.stringify(block));
clientStatuses.sent++;
} catch (e) {
logger.warn(
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`Encountered error while sending block: ${e}, continuing...`
);
clientStatuses.failed++;
}
} else {
logger.warn(
`Client is not open, skipping... (readyState: ${client.readyState})`
);
clientStatuses.failed++;
}
});
const totalClients = wss.clients.size;
logger.info(
`📡 Sent block ${block.header.globalVariables.blockNumber} to ${clientStatuses.sent} clients (failed: ${clientStatuses.failed}, total: ${totalClients})`
);
};

export const init = async () => {
Expand All @@ -27,14 +52,9 @@ export const init = async () => {
});

wss.on("connection", (connectedWs) => {
logger.info(`🧍 WS: client connected`);
connectedWs.on("message", (incomingMessage) => {
// REMOVEME
logger.info(`Received message: ${JSON.stringify(incomingMessage)}`);
connectedWs.send(`SERVER ECHO: ${JSON.stringify(incomingMessage)}`);
});
logger.info(`🧍 WS: client connected (total: ${wss.clients.size})`);
connectedWs.on("close", function close() {
logger.info(`🚪 WS: client disconnected`);
logger.info(`🚪 WS: client disconnected (total: ${wss.clients.size})`);
});
});

Expand Down

0 comments on commit a271993

Please sign in to comment.