Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Use websockets for sync-realtime #1290

Open
kyscott18 opened this issue Nov 27, 2024 · 0 comments
Open

feat: Use websockets for sync-realtime #1290

kyscott18 opened this issue Nov 27, 2024 · 0 comments
Assignees

Comments

@kyscott18
Copy link
Collaborator

Description

Using websockets to replace the current polling algorithm would be more performant and cheaper.

const enqueue = async () => {
try {
const block = await _eth_getBlockByNumber(args.requestQueue, {
blockTag: "latest",
});
const latestBlock = getLatestUnfinalizedBlock();
// We already saw and handled this block. No-op.
if (latestBlock.hash === block.hash) {
args.common.logger.trace({
service: "realtime",
msg: `Skipped processing '${args.network.name}' block ${hexToNumber(block.number)}, already synced`,
});
return;
}
const blockWithEventData = await fetchBlockEventData(block);
consecutiveErrors = 0;
return queue.add(blockWithEventData);
} catch (_error) {
if (isKilled) return;
const error = _error as Error;
args.common.logger.warn({
service: "realtime",
msg: `Failed to fetch latest '${args.network.name}' block`,
error,
});
// After a certain number of attempts, emit a fatal error.
if (++consecutiveErrors === ERROR_TIMEOUT.length) {
args.common.logger.error({
service: "realtime",
msg: `Fatal error: Unable to fetch latest '${args.network.name}' block after ${ERROR_TIMEOUT.length} attempts.`,
error,
});
args.onFatalError(error);
}
}
};

Currently enqueue is used with setInterval, but should be swapped out with a websocket request, similar to https://viem.sh/docs/actions/public/watchBlocks

Other notes

This will require some updates to the rpc infrastructure to separate out websockets and https, and also be able to introspect as to whether a websocket transport is available

@kyscott18 kyscott18 changed the title Use websockets for sync-realtime feat: Use websockets for sync-realtime Nov 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants