Webhook: push notification giữa servers
Polling: “Có gì mới chưa? Có gì mới chưa?” — hỏi 1000 lần/phút = lãng phí. Webhook: “Khi có gì mới → tôi POST đến URL bạn.” — hiệu quả, real-time, không lãng phí.
Outgoing webhook: app bạn gửi event ra ngoài
// File: webhook-sender.ts
import crypto from 'crypto'
interface WebhookConfig {
url: string
secret: string
events: string[]
}
async function sendWebhook(
config: WebhookConfig,
event: string,
payload: Record<string, unknown>,
) {
const body = JSON.stringify({ event, data: payload, ts: Date.now() })
// Ký bằng HMAC để receiver verify nguồn gốc
const signature = crypto.createHmac('sha256', config.secret)
.update(body).digest('hex')
await fetch(config.url, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-Webhook-Signature': signature,
},
body,
signal: AbortSignal.timeout(10_000), // timeout 10s
})
}
// Sử dụng: khi user tạo order mới
await sendWebhook(customerConfig, 'order.created', {
orderId: 'ord_123',
amount: 500000,
status: 'paid',
})
Zapier/Make integration: kết nối 5000+ app
| Cách | Effort | Kết nối được |
|---|---|---|
| Tự build integration (Slack API, Gmail API…) | 2-5 ngày/integration | 1 app |
| Zapier Integration | 1-2 ngày setup | 5000+ apps tự động |
| Make.com (Integromat) | 1-2 ngày setup | 1500+ apps |
Zapier model: bạn cung cấp Triggers (webhook events) + Actions (API endpoints) → Zapier kết nối với 5000 app. User tự tạo automation: “Khi có order mới → thêm row vào Google Sheet → gửi Slack notification.”
Security cho webhooks
| Vấn đề | Solution |
|---|---|
| Ai cũng POST vào webhook URL | HMAC signature: verify nguồn gốc bằng shared secret |
| Replay attack: gửi lại request cũ | Kiểm tra timestamp: reject nếu > 5 phút |
| Webhook endpoint bị DDoS | Rate limiting + IP allowlist |
| Receiver down, miss event | Retry 3 lần + event log để replay |
Shopify App Store có 8,000+ apps — tất cả kết nối qua webhook. Stripe, GitHub, Slack cũng vậy. Khi app bạn cho phép integrate → developer khác build trên nền bạn → ecosystem tự growth.
❓ Lợi ích lớn nhất của webhook so với polling?
- Implement outgoing webhook với HMAC signature
- Tạo webhook management UI cho user (URL, events, secret)
- Setup retry cho webhook delivery fail
- Nghiên cứu Zapier Developer Platform