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
SNSandSQS - Serverless architecture based on
AWS Lambdafunctions 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.
- 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 removeOverall Architecture
Data Modeling
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 tasksKey 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
- WebSocket connection lifecycle management (
- 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
SQSandSNS - Selective message broadcasting to channel subscribers
- Message filtering and transformation support
- MSA event reception and real-time propagation via
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
VERSION INFO
Version History
| Version | Description |
|---|---|
| 0.25.818 | implements websocket-service. |
| 0.25.808 | optimized lemon-core@4.0.6 initially. |