Skip to content

Webhooks Overview

ระบบปฏิบัติการเบื้องหลัง (Backend) ของคุณสามารถรับรู้เหตุการณ์ (Events) ที่เกิดขึ้นบน RawPush Gateway ได้ทันทีผ่านสถาปัตยกรรม Webhooks

เมื่อเกิดเหตุการณ์ในระบบตามเงื่อนไขที่กำหนดไว้ RawPush จะทำการส่ง HTTP POST request กลับไปยัง API Endpoint ปลายทางของคุณแบบ Real-time ทำให้ระบบของคุณสามารถตอบสนองและประมวลผลข้อมูลต่อได้ทันที


⚡ 1. คอนเซปต์การทำงาน

📋 2. Supported Events

RawPush รองรับ Webhook Events ตามมาตรฐานอุตสาหกรรม (เทียบเคียง Pusher/Ably):

Connection Lifecycle

Eventคำอธิบาย
connection.openedWebSocket connection ถูกเปิด
connection.closedWebSocket connection ถูกปิด
connection.authenticatedClient ผ่าน HMAC signature verification สำเร็จ
connection.auth_failedClient HMAC signature ถูกปฏิเสธ

Channel Lifecycle

Eventคำอธิบาย
channel.subscribedClient subscribe เข้า channel
channel.unsubscribedClient unsubscribe ออกจาก channel
channel.occupiedChannel เปลี่ยนจาก 0 → 1 subscriber (ตื่นขึ้นมา)
channel.vacatedChannel เปลี่ยนจาก 1 → 0 subscribers (ว่างลง)

Message Lifecycle

Eventคำอธิบาย
message.publishedEvent ถูก broadcast ไปยัง subscribers
message.rejectedEvent ถูกปฏิเสธจาก rate limit หรือ policy
client_eventClient-triggered event ถูก forward ไปยังปลายทาง

System

Eventคำอธิบาย
webhook.testTest ping จาก Dashboard

🎯 3. Use Cases ยกตัวอย่าง

Webhooks เป็น building block ที่ยืดหยุ่น ใช้ได้กับทุก use case:

  • Event Persistence: RawPush ไม่เก็บ data ค้างคืน หากคุณต้องการ replay/history ให้ใช้ Webhook เซฟข้อมูลเข้า database ของ Backend คุณเอง
  • Serverless Processing: รับ event → ส่งต่อเข้า Queue (SQS/RabbitMQ) → ประมวลผล background ได้ทันที
  • Analytics & Monitoring: Track connection patterns, message volumes, channel occupancy ผ่าน channel.occupied/channel.vacated
  • Access Control: ตรวจจับ connection.auth_failed แล้ว trigger alert หรือ block IP ที่ต้องสงสัย
  • Audit Trail: บันทึกทุก event ที่ผ่านระบบเพื่อ compliance หรือ forensics

🛡️ 4. Security & Signature Verification

เพื่อป้องกันการปลอมแปลง Request (Spoofing) มายัง Webhook Endpoint ของคุณ RawPush ใช้มาตรการรักษาความปลอดภัยด้วย Cryptographic Signatures

ทุก Request ที่ส่งออกจากแพลตฟอร์มจะแนบ HTTP Header X-Webhook-Signature เสมอ ซึ่งประกอบด้วยค่า HMAC-SHA256 ที่คำนวณจาก Payload ของ Request และ Webhook Secret (ตรวจสอบได้จากเมนูตั้งค่าใน Dashboard)

แอปพลิเคชันของคุณจำเป็นต้องตรวจสอบ (Verify) Signature นี้ทุกครั้ง เพื่อยืนยันว่า Request นั้นถูกส่งมาจาก RawPush จริง และข้อมูลไม่ถูกดัดแปลงระหว่างทาง

ในบทความถัดไป เราจะอธิบายขั้นตอนการตั้งค่าและตัวอย่างโค้ดสำหรับการตรวจสอบ Signature อย่างละเอียด

Released under the MIT License.