1. FHIR R5 中的訂閱
FHIR R5 推出 基於主題的訂閱 — 完全取代了 R4 的舊機制。允許系統在資料發生變化時接收即時通知。
┌──────────┐ Subscribe ┌──────────┐
│ Client │ ──────────────────▶ │ FHIR │
│ (EMR, │ │ Server │
│ app) │ ◀────────────────── │ │
│ │ Notification │ │
└──────────┘ └──────────┘
2.訂閱主題
訂閱主題定義 “我可以訂閱什麼?” — 可用的事件、觸發器、過濾器。
{
"resourceType": "SubscriptionTopic",
"id": "topic-encounter-admission",
"url": "http://hospital.vn/fhir/SubscriptionTopic/encounter-admission",
"title": "Encounter Admission",
"status": "active",
"description": "Thông báo khi có bệnh nhân nhập viện mới",
"resourceTrigger": [
{
"description": "Encounter mới được tạo hoặc chuyển status sang in-progress",
"resource": "http://hl7.org/fhir/StructureDefinition/Encounter",
"supportedInteraction": ["create", "update"],
"queryCriteria": {
"current": "status=in-progress&class=http://terminology.hl7.org/CodeSystem/v3-ActCode|IMP",
"resultForCreate": "test-passes",
"resultForDelete": "test-fails"
}
}
],
"canFilterBy": [
{
"description": "Filter theo location (khoa)",
"resource": "Encounter",
"filterParameter": "location"
},
{
"description": "Filter theo service provider",
"resource": "Encounter",
"filterParameter": "service-provider"
}
],
"notificationShape": [
{
"resource": "Encounter",
"include": [
"Encounter:subject",
"Encounter:participant"
]
}
]
}
3. 訂閱資源
{
"resourceType": "Subscription",
"id": "sub-admission-notify-001",
"status": "requested",
"topic": "http://hospital.vn/fhir/SubscriptionTopic/encounter-admission",
"reason": "Nhận thông báo nhập viện khoa Tim mạch",
"filterBy": [
{
"resourceType": "Encounter",
"filterParameter": "location",
"value": "Location/loc-cardiology"
}
],
"channelType": {
"system": "http://terminology.hl7.org/CodeSystem/subscription-channel-type",
"code": "rest-hook"
},
"endpoint": "https://ehr-app.hospital.vn/api/webhooks/fhir-notifications",
"heartbeatPeriod": 60,
"timeout": 60,
"contentType": "application/fhir+json",
"content": "id-only",
"maxCount": 10
}
頻道類型
| 頻道 | 描述 | 使用案例 |
|---|---|---|
| 休息鉤 | HTTP POST 到端點 | 伺服器到伺服器集成 |
| 網路套接字 | WebSocket 連接 | 即時使用者介面、儀表板 |
| 電子郵件 | 電子郵件通知 | 工程師警報 |
| 消息。留言 | FHIR 訊息傳遞($process-message) | 整合訊息系統 |
內容層級
| 等級 | 描述 |
|---|---|
| 空的 | 只有通知頭,沒有資源數據 |
| 僅 ID | 資源ID+類型,客戶端自行獲取 |
| 資源齊全 | 包含通知中的所有資源 |
4. 通知類型
握手通知
建立訂閱時發送,確認端點處於活動狀態。
{
"resourceType": "Bundle",
"type": "subscription-notification",
"entry": [
{
"resource": {
"resourceType": "SubscriptionStatus",
"type": "handshake",
"subscription": {
"reference": "Subscription/sub-admission-notify-001"
},
"topic": "http://hospital.vn/fhir/SubscriptionTopic/encounter-admission",
"eventsSinceSubscriptionStart": "0"
}
}
]
}
事件通知
{
"resourceType": "Bundle",
"type": "subscription-notification",
"entry": [
{
"resource": {
"resourceType": "SubscriptionStatus",
"type": "event-notification",
"subscription": {
"reference": "Subscription/sub-admission-notify-001"
},
"topic": "http://hospital.vn/fhir/SubscriptionTopic/encounter-admission",
"eventsSinceSubscriptionStart": "5",
"notificationEvent": [
{
"eventNumber": "5",
"focus": {
"reference": "Encounter/enc-new-001"
},
"additionalContext": [
{"reference": "Patient/patient-new-001"}
]
}
]
}
},
{
"fullUrl": "https://fhir-server.example.com/fhir/r5/Encounter/enc-new-001",
"resource": {
"resourceType": "Encounter",
"id": "enc-new-001",
"status": "in-progress"
}
}
]
}
心跳通知
定期發送以確認訂閱處於活動狀態,即使沒有事件也是如此。
5. 訂閱生命週期
requested → active → error → off
│ │ │
│ │ └── Server tạm dừng sau nhiều lần gửi thất bại
│ └── Nhận handshake thành công
└── Client POST Subscription
6. 在 HAPI FHIR 上練習訂閱
# 1. Kiểm tra SubscriptionTopics có sẵn
curl -s https://hapi.fhir.org/baseR5/SubscriptionTopic | jq '.entry[].resource.url'
# 2. Tạo Subscription
curl -X POST https://hapi.fhir.org/baseR5/Subscription \
-H "Content-Type: application/fhir+json" \
-d '{
"resourceType": "Subscription",
"status": "requested",
"topic": "http://example.org/FHIR/SubscriptionTopic/admission",
"channelType": {"code": "rest-hook"},
"endpoint": "https://your-webhook.example.com/fhir-notify",
"contentType": "application/fhir+json",
"content": "id-only"
}'
# 3. Kiểm tra status
curl -s https://hapi.fhir.org/baseR5/Subscription/sub-id | jq '.status'
# 4. Tạo Encounter để trigger notification
curl -X POST https://hapi.fhir.org/baseR5/Encounter \
-H "Content-Type: application/fhir+json" \
-d '{"resourceType":"Encounter","status":"in-progress","class":[{"coding":[{"code":"IMP"}]}],"subject":{"reference":"Patient/123"}}'
七、總結
訂閱主題 — 伺服器定義可以訂閱的事件
訂閱 — 用戶端註冊接收通知,選擇頻道、過濾器、內容級別
頻道 —rest-hook(最常見)、websocket、電子郵件、訊息
通知類型 — 握手、心跳、事件通知
完全取代R4舊的訂閱機制(基於條件)