feat(api): add WebSocket transfer subscription endpoint#42
Merged
Miracle656 merged 2 commits intoMiracle656:mainfrom Apr 23, 2026
Merged
Conversation
Closes Miracle656#33 - src/events.ts: singleton EventEmitter that publishes `transfer:new` events; setMaxListeners(0) prevents false positives with many concurrent subscribers - src/ws.ts: WebSocket server attached via noServer + manual upgrade handler; endpoint is ws://host/subscribe/:address; only events where the watched address is sender or recipient are forwarded; each handler is cleaned up on socket close/error to prevent memory leaks; payload mirrors REST shape and includes displayAmount - src/indexer.ts: emits one event per record after a successful DB upsert batch - src/index.ts: replaces app.listen() with http.createServer() so the same port handles both REST and WebSocket upgrade requests
|
@0xDeon Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #33
Real-time push notifications for token transfers via WebSocket. Clients subscribe to an address and receive a JSON payload for every new transfer that is indexed where that address appears as sender or recipient.
What was implemented
New files
src/events.tsEventEmitterthat publishestransfer:newevents.setMaxListeners(0)prevents Node.js warnings with many concurrent subscribers.src/ws.tsnoServer: true+ manualupgradeevent handling. Parses address from URL, registers a per-socket handler, forwards matching events, removes handler onclose/errorto prevent memory leaks.Modified files
src/indexer.tsemitTransfer(record)for every record after a successfulupsertTransfersbatch.src/index.tsapp.listen()withhttp.createServer(app)so the HTTP server can handle WebSocket upgrade requests on the same port. CallsattachWebSocketServer(server).Endpoint
:address— any Stellar address (sender or recipient)/transfersresponse shape, includingdisplayAmount/subscribe/...paths receive an HTTP 404 and the socket is destroyedPayload shape (example)
{ "contractId": "CABC...", "eventType": "transfer", "fromAddress": "GBOB...", "toAddress": "GALICE...", "amount": "10000000", "displayAmount": "1.0000000", "ledger": 12345, "ledgerClosedAt": "2025-01-01T00:00:00.000Z", "txHash": "abc123", "eventId": "evt-001" }How it was tested
Build verification:
npm run build # → zero TypeScript errorsManual test steps:
npm run devwscat):Memory safety check:
Connect and disconnect many clients rapidly — the listener count on
transferEmitterreturns to 0 after all sockets close becausetransferEmitter.off(handler)is called in both thecloseanderrorhandlers.