Thuta Learning
API Integration & Webhooks
IntermediateWeb Developmentintermediate

Webhook နှင့် Polling နှိုင်းယှဉ်ခြင်း

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Webhook နှင့် Polling နှိုင်းယှဉ်ခြင်း concept ကို နားလည်ရှင်းပြနိုင်ရန်
  • Diagram ကို ဖတ်ပြီး request/response (သို့) event flow ဘယ်လိုစီးဆင်းသလဲ ခြေရာခံနိုင်ရန်
  • ကိုယ့် ကိုယ်ပိုင် API integration အတွက် ဘယ်လို အသုံးချသင့်သလဲ ရှင်းပြနိုင်ရန်

နားလည်ထားရမယ့် အချက်

Polling နဲ့ webhook နှစ်ခုလုံးက ပြဿနာတူတူကို ဖြေရှင်းကြပါတယ် — တစ်ခုခု ပြောင်းလဲသွားတယ်ဆိုတာ သိရှိဖို့ပါ — ဒါပေမဲ့ mechanic ဆန့်ကျင်ဘက်ပါ။ Polling ကတပ်တစ်ခုခု ဖြစ်ခဲ့လား ထပ်ကာထပ်ကာ မေးနေတာ၊ webhook ကတော့ ဖြစ်လိုက်တာနဲ့ ချက်ချင်း ပို့ပေးတာပါ။

Polling က ရိုးရှင်းပြီး public endpoint မလိုပေမယ့် request တွေ ဖြုန်းတီးနေပြီး delay တိုးစေပါတယ်။ Webhook ကတော့ ချက်ချင်းလိုလိုနဲ့ request ဖြုန်းစရာ မလိုပေမယ့် public URL လိုအပ်ပြီး signature verification နဲ့ idempotent handling ကို တကယ့်လိုအပ်ချက်တွေအဖြစ် ဆက်ခံထားရပါတယ်။

အချက်နှိုင်းယှဉ်ချက်
LatencyPolling: interval တစ်ခုစာအထိ ကြန့်ကြာ။ Webhook: almost instant ဖြစ်ပြီး သိသာတဲ့ ကြန့်ကြာမှု မရှိ။
EfficiencyPolling: ဘာမှမပြောင်းလဲရင်တောင် request ဖြုန်းတီး။ Webhook: real event တစ်ခုစီအတွက် request တစ်ခုတည်း၊ ဘာမှမဖြုန်းတီး။
ComplexityPolling: ရိုးရှင်း၊ public endpoint မလို။ Webhook: signature verification နဲ့ idempotent handling လိုအပ်။
RequirementsPolling: outbound network connection ရှိရုံပဲ လို။ Webhook: public ကနေ ရောက်နိုင်တဲ့ server endpoint လို။

Polling က frequency နည်းတဲ့ စစ်ဆေးမှုတွေ ဒါမှမဟုတ် public endpoint host မလုပ်နိုင်တဲ့ ပတ်ဝန်းကျင်မှာ ကိုက်ညီပါတယ်။ Webhook တွေကတော့ near-real-time အရေးကြီးပြီး receiving endpoint host လုပ်နိုင်တဲ့ နေရာတိုင်းမှာ ကိုက်ညီပါတယ်။

text
POLLING VS WEBHOOK
------------------
-----
Polling (your app asks repeatedly):
  Your App -> "anything new?" -> Server   (t=0s)
  Your App -> "anything new?" -> Server   (t=10s)
  Your App -> "anything new?" -> Server   (t=20s)
  Your App -> "anything new?" -> Server   (t=30s)

Webhook (service pushes once, when it happens):
  External Service -> "something happened!" -> Your App (t=7s)

လက်တွေ့ scenario နဲ့ ချိတ်ကြည့်မယ်

အောက်က code က event နှစ်ခု — ၇ စက္ကန့်နဲ့ ၂၃ စက္ကန့် — timeline တူတူကို ၃၀ စက္ကန့် window အတွင်း နည်းလမ်းနှစ်ခုစလုံးနဲ့ simulate လုပ်ထားပါတယ်။ simulateWebhook ကတော့ interval စောင့်စရာမလိုဘဲ ချက်ချင်း သတိထားမိပါတယ်။

Function နှစ်ခုလုံးကို run ပြီး totalRequests နဲ့ delay ကို နှိုင်းယှဉ်ကြည့်ပါ။ Polling က fixed request အရေအတွက်ကို ပို့ပြီး delay က interval နဲ့ event timing ကြုံဆုံပုံအပေါ် မူတည်ပါတယ်။ Webhook ကတော့ event တစ်ခုစီအတွက် request တစ်ခုတည်းနဲ့ delay သုည အမြဲရပါတယ်။

Polling interval ကို လျှော့ချကြည့်ပါ — window တူတူအတွင်းမှာ request အများကြီးပိုပို့ရတဲ့ trade-off နဲ့ ချက်ချင်း တန်ဖိုးဆောင်ရပါလိမ့်မယ်၊ webhook တွေက ဒါကို လုံးဝ ကျော်ဖြတ်နိုင်ပါတယ်။

အတူတူ စမ်းရေးကြည့်မယ်

javascript
function simulatePolling(events, intervalSeconds, totalSeconds) {
  let requests = 0;
  const noticedAt = [];
  for (let t = 0; t <= totalSeconds; t += intervalSeconds) {
    requests++;
    events.forEach((e) => {
      if (e.at <= t && e.noticedAtPoll === undefined) {
        e.noticedAtPoll = t;
        noticedAt.push({ event: e.name, occurredAt: e.at, noticedAt: t, delaySeconds: t - e.at });
      }
    });
  }
  return { approach: "polling", totalRequests: requests, noticedAt };
}

function simulateWebhook(events) {
  const noticedAt = events.map((e) => ({ event: e.name, occurredAt: e.at, noticedAt: e.at, delaySeconds: 0 }));
  return { approach: "webhook", totalRequests: events.length, noticedAt };
}

const timeline = [
  { name: "payment.completed", at: 7 },
  { name: "user.created", at: 23 }
];

const pollingResult = simulatePolling(timeline.map((e) => ({ ...e })), 10, 30);
const webhookResult = simulateWebhook(timeline);

console.log(pollingResult);
console.log(webhookResult);
You should see
{
  approach: 'polling',
  totalRequests: 4,
  noticedAt: [
    { event: 'payment.completed', occurredAt: 7, noticedAt: 10, delaySeconds: 3 },
    { event: 'user.created', occurredAt: 23, noticedAt: 30, delaySeconds: 7 }
  ]
}
{
  approach: 'webhook',
  totalRequests: 2,
  noticedAt: [
    { event: 'payment.completed', occurredAt: 7, noticedAt: 7, delaySeconds: 0 },
    { event: 'user.created', occurredAt: 23, noticedAt: 23, delaySeconds: 0 }
  ]
}

၅ မိနစ် စမ်းကြည့်

Timeline ထဲကို ၁၅ စက္ကန့်မှာ event တတိယတစ်ခု ထပ်ထည့်ပြီး simulation နှစ်ခုစလုံးကို ပြန် run ပါ — output ကို မကြည့်ခင် totalRequests နဲ့ delay အသစ်ကို ကြိုတင်ခန့်မှန်းကြည့်ပါ။

သတိလေးတစ်ချက်

Public ကနေ ရောက်နိုင်တဲ့ server ဘယ်တော့မှ ပေါ်လွင်နိုင်မှာမဟုတ်တဲ့ system အတွက် webhook ကို ရွေးချယ်ပြီး delivery မဖြစ်နိုင်အောင် လုပ်ခြင်း။

Frequency မြင့်ပြီး အချိန်ကိုက် လိုအပ်တဲ့ use case အတွက် polling ကို ရွေးချယ်ပြီး မလိုအပ်တဲ့ delay နဲ့ request volume ဖြုန်းတီးမှုကို လက်ခံခြင်း။

Polling (computer science) — WikipediaAPI Integration & Webhooks

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Public ကနေ ရောက်နိုင်တဲ့ server ဘယ်တော့မှ ပေါ်လွင်နိုင်မှာမဟုတ်တဲ့ system အတွက် webhook ကို ရွေးချယ်ပြီး delivery မဖြစ်နိုင်အောင် လုပ်ခြင်း။
  • Frequency မြင့်ပြီး အချိန်ကိုက် လိုအပ်တဲ့ use case အတွက် polling ကို ရွေးချယ်ပြီး မလိုအပ်တဲ့ delay နဲ့ request volume ဖြုန်းတီးမှုကို လက်ခံခြင်း။
  • API Tutorial (apiguide) ကို မလေ့လာရသေးရင် ဒီ course ကို စမလိုက်ခင် အရင် ပြီးအောင် လေ့လာထားသင့်ပါတယ် — ဒီ course က REST/HTTP/Auth အခြေခံတွေကို ထပ်မသင်ဘဲ webhook, testing, reliability, integration architecture တို့ကိုသာ ဆက်လက် တည်ဆောက်ပါတယ်။

လေ့ကျင့်ခန်း

Timeline ထဲကို ၁၅ စက္ကန့်မှာ event တတိယတစ်ခု ထပ်ထည့်ပြီး simulation နှစ်ခုစလုံးကို ပြန် run ပါ — output ကို မကြည့်ခင် totalRequests နဲ့ delay အသစ်ကို ကြိုတင်ခန့်မှန်းကြည့်ပါ။

You'll know it worked when: { approach: 'polling', totalRequests: 4, noticedAt: [ { event: 'payment.completed', occurredAt: 7, noticedAt: 10, delaySeconds: 3 }, { event: 'user.created', occurredAt: 23, noticedAt: 30, delaySeconds: 7 } ] } { approach: 'webhook', totalRequests: 2, noticedAt: [ { event: 'payment.completed', occurredAt: 7, noticedAt: 7, delaySeconds: 0 }, { event: 'user.created', occurredAt: 23, noticedAt: 23, delaySeconds: 0 } ] }