LogoEurekaCodes
Technology

[Backend] eureka-sockets-api

Serverless event-driven socket service providing real-time WebSocket communication in MSA environments Bidirectional real-time messaging and event delivery t...

eureka-sockets-api

Serverless event-driven socket service providing real-time WebSocket communication in MSA environments

Summary

  • Bidirectional real-time messaging and event delivery to clients via AWS API Gateway WebSocket
  • Event-driven architecture with loose coupling between microservices using SNS and SQS
  • Serverless architecture based on AWS Lambda functions providing auto-scaling and cost optimization
  • Efficient message routing and subscriber management through service/model-based channel system

Getting Started

  • Prerequirements (or installations) before starting.
    1. aws-cli with api-key
    2. git
    3. nodejs22
    4. (optional) httpie
    5. (optional) wscat
  • Fork(or clone), develop and deploy the serverless api.
# clone the code.
$ git clone https://github.com/lemoncloud-io/eureka-sockets-api.git

# STEP.1 install the dependecies.
$ npm ci

# STEP.2 run the server locally.
$ npm run express

# STEP.3 make request and develop locally.
$ http :8832/hello

# STEP.4 deploy into your AWS cloud (`AWS-Key` is required).
$ npm run deploy

# (example) try to connect to a WebSocket
$ wscat -c wss://a5jxmksfa2.execute-api.ap-northeast-2.amazonaws.com/dev

# STEP.5 check the deploy info
$ npm run info

# STEP.6 remove(or uninstall)
$ npm run remove

Overall Architecture

Data Modeling

[backend] eureka-sockets-api image

Module Structure

src/
├── cores/                  # Common architecture
│   ├── abstract-controllers.ts  # CRUD controller base
│   ├── abstract-services.ts     # Service layer base
│   └── types.ts                 # Common types
├── libs/                  # Common architecture
│   └── wss/               # WebSocket support library (core module)
│       ├── tools.ts       # WebSocket support tools
│       ├── wss-agent.ts   # Websocket business logic
│       └── wss-types.ts   # Websocket types definitions
├── modules/                # Business modules
│   ├── sockets/             # Socket service (core module)
│   │   ├── api-sockets.ts   # Socket API controller
│   │   ├── api-channels.ts   # Socket Chnnel API controller
│   │   ├── model.ts       # Socket data model
│   │   ├── service.ts     # Socket business logic
│   │   ├── proxy.ts       # Socket data access
│   │   ├── transformer.ts # Data transformation
│   │   ├── types.ts       # Socket type definitions
│   │   └── views.ts       # Socket view models
│   ├── callback/          # External callback handling
│   │   ├── api-callback.ts
│   │   ├── model.ts
│   │   ├── service.ts
│   │   └── ...
│   ├── mock/              # Test mock data
│   │   ├── api-mocks.ts
│   │   ├── api-tests.ts
│   │   └── ...
│   └── search/            # Search functionality
│       ├── api-search.ts
│       ├── model.ts
│       └── service.ts
├── service/               # Core services
│   ├── backend-service.ts # Main service
│   ├── backend-proxy.ts   # Data access layer
│   └── backend-model.ts   # Data models
└── api/                   # API endpoints
├── hello-api.ts       # Health check
└── crons-api.ts       # Scheduled tasks

Key Features

WebSocket Service Support

  • Connection Management
    • WebSocket connection lifecycle management ($connect, $disconnect, $default)
    • DynamoDB-based connection state and metadata storage
    • Secure connection support through IAM or custom authentication
  • Channel System
    • Channel-based subscription event reception management
    • Per-client channel subscription/unsubscription management
    • Channel-specific subscriber list management and message routing
  • Broadcast
    • MSA event reception and real-time propagation via SQS and SNS
    • Selective message broadcasting to channel subscribers
    • Message filtering and transformation support

Usage Examples

# 0. Create Channel (Optional)
$ echo '{"service": "orders-api", "modelType": "order", "desc": "Order events channel"}' | \
http POST :8832/channels/0

# 1. WebSocket Connection
$ wscat -c wss://<your-wss-endpoint>
$ wscat -c wss://<your-wss-endpoint>?channels=<channel-id> # w/ subscribe channel

# 2. Ping/Pong Action
> {"action": "ping", "data": "hello"}
< {"action": "pong", "data": "hello"}

# 3. Broadcast (HTTP API)
# Broadcast message to specific channel
$ echo '{"data": {"message": "hello"}}' | http POST :8832/sockets/1000001/broadcast

# Message received by subscribers
< {"channel": "1000001", "event": "message", "data": {"message": "hello"}}

# 4. Disconnect
# WebSocket connection termination (server automatically executes $disconnect handler)
> (Ctrl+C or close browser)

LICENSE

MIT

VERSION INFO

Version History

VersionDescription
0.25.818implements websocket-service.
0.25.808optimized lemon-core@4.0.6 initially.