Mocked credit card transactions WebSocket API for experimental/educational purposes.
This project is a simple credit card transactions WebSocket API. It is a mock of a real API that would be used to get credit card transactions, useful for experimental/educational purposes, such as testing and developing ML models for fraud detection.
WebSockets enable low-latency, bidirectional streaming with minimal overhead:
Key Benefits:
-
Single Persistent Connection: One handshake, then continuous data flow. HTTP requires new connection + headers for every request.
-
Server Push: Server sends data immediately when available. No client polling needed.
-
Low Overhead: After initial handshake, messages have ~2 bytes framing vs ~500+ bytes for HTTP headers.
-
True Bidirectional: Both ends send/receive anytime. Essential for interactive applications.
Alternatives & Why They Fall Short:
| Method | Latency | Overhead | Complexity | Use Case |
|---|---|---|---|---|
| Polling | High (seconds) | Very High | Low | Rarely acceptable |
| Long-polling | Medium | High | Medium | Legacy fallback |
| Server-Sent Events | Low | Medium | Low | One-way only |
| WebSockets | Very Low (ms) | Very Low | Medium | Real-time bidirectional |
For This Project:
- Streaming ~10 transactions/second
- WebSocket: 10 small messages (minimal overhead)
- HTTP Polling: 600 requests/minute (massive waste)
- Latency: <10ms (WebSocket) vs 1-5 seconds (polling)
The websocketAPI has two channels:
heartbeat: for checking if the connection is alivetransactions: for getting credit card transactions in realtime
Run make run to start the API locally.
The API will be available at ws://0.0.0.0:9999/ws/v1.
Alternatively, use cargo directly:
cargo runControl the logging verbosity using the LOG_LEVEL environment variable:
LOG_LEVEL=DEBUG cargo runPress Ctrl+C to gracefully shutdown the server. The server will:
- Stop accepting new connections
- Complete existing requests
- Cleanly shutdown background tasks
- Exit cleanly
The project includes a Dockerfile using secure Chainguard base images.
docker build -t txapi:latest .# Run with default settings (INFO log level)
docker run -p 9999:9999 txapi:latest
# Run with DEBUG log level
docker run -p 9999:9999 -e LOG_LEVEL=DEBUG txapi:latest
# Run in detached mode
docker run -d -p 9999:9999 --name txapi txapi:latestThe WebSocket API will be available at ws://localhost:9999/ws/v1
docker stop txapi
docker rm txapiThe application includes a health check endpoint at /health that returns:
{
"status": "ok",
"version": "0.1.0"
}You can also run a health check from the command line:
# Within Docker container
docker exec txapi /app/txapi --health
# Locally
cargo run -- --health
# Using curl
curl http://localhost:9999/healthTo subscribe to a channel, you need to send a message to the server with the following format:
{
"method": "subscribe",
"params": {
"channel": "transactions"
}
}{
"channel": "transactions",
"data": [
{
"id": "11df919988c134d97bbff2678eb68e22",
"timestamp": "2024-01-01T00:00:00Z",
"cc_number": "4473593503484549",
"category": "Grocery",
"amount_usd_cents": 10000,
"latitude": 37.774929,
"longitude": -122.419418,
"country_iso": "US",
"city": "San Francisco",
}
]
}{
"channel": "heartbeat",
"data": {
"status": "ok"
}
}