Elysia Örneği: Micro Socket Gateway
Elysia (Bun) ile yazılmış tam bir Micro Socket gateway: WebSocket uç noktaları (/ws, /ws/sub/:topic), sub-socket modeli, milyonlarca aboneliğe ölçeklenebilir yayın.
Genel Bakış
Örnek proje: examples/elysia-micro-socket/.
- WebSocket:
/ws(çok topic) ve/ws/sub/:topic(sub WebSocket — bağlantı başına tek topic). - Sub-socket store:
Map<topic, Set<connectionId>>; topic başına O(abone) yayın. - Omurga: Bellek içi (tek düğüm); yatay ölçek için NATS/Kafka.
- Dedup (I₂): messageId ile kullanıcı bazlı; production’da Redis.
- Demo auth: Boş olmayan token → userId; production’da JWT + ACL.
Çalıştırma
bash
cd examples/elysia-micro-socket
bun install
bun run dev| Kaynak | URL |
|---|---|
| Döküman + WS istek at | http://localhost:3000/docs — Connect ile WS bağlan, setup/subscribe/publish gönder, yanıtlar logda |
| OpenAPI (Scalar) | http://localhost:3000/openapi |
| WebSocket (çok topic) | ws://localhost:3000/ws |
| WebSocket (sub) | ws://localhost:3000/ws/sub/prices (örnek) |
| İstatistik | GET http://localhost:3000/stats |
| Demo yayın | GET http://localhost:3000/demo/publish/:topic?key=value |
WebSocket ile istek atma
- http://localhost:3000/docs aç.
- WebSocket bölümünde URL seç: /ws veya /ws/sub/prices (preset butonları) veya kendi URL’ini yaz.
- Connect → Message: setup seç, token gir, Send. Logda
ready(ve varsasubscribed) görünür. - subscribe / unsubscribe / publish seçip alanları doldur, Send ile istek at; yanıtlar logda (← yeşil) görünür.
Protokol (Özet)
| İstemci → Sunucu | Sunucu → İstemci |
|---|---|
setup + token (+ isteğe bağlı topics) | ready + sessionId, subscribed (URL/topics için) |
subscribe + topic | subscribed + topic |
unsubscribe + topic | unsubscribed + topic |
publish + topic, messageId, payload | ack + messageId, status (ok | duplicate) |
| — | message + topic, seqNo, messageId, data |
| — | error + code, message |
Tam referans (HTTP endpoint’leri, tüm mesaj tipleri, mimari): İngilizce sayfa.