0Pricing
Serverless Backend with AWS Lambda & API Gateway · Pelajaran

API WebSocket untuk Komunikasi Waktu Nyata

Bangun koneksi dua arah dan waktu nyata dengan API WebSocket API Gateway. Pelajari rute koneksi, pemutusan koneksi, dan pesan, serta cara mengirim data kembali ke klien.

API WebSocket untuk Komunikasi Waktu Nyata adalah pelajaran Serverless Backend with AWS Lambda & API Gateway gratis di CoddyKit. Ini adalah pelajaran 4 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Serverless Backend with AWS Lambda & API Gateway, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Serverless Backend with AWS Lambda & API Gateway mencakup 4 pelajaran total.

Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.

Why WebSockets?

REST APIs are request/response — the server cannot push. For chat, live dashboards, or notifications you need a persistent two-way channel. API Gateway WebSocket APIs provide exactly that.

The Three Built-in Routes

A WebSocket API has special routes:

  • $connect when a client connects
  • $disconnect when it leaves
  • $default for unmatched messages

Route Selection Expression

Custom routes are chosen by a route selection expression, usually a field in the incoming JSON such as $request.body.action.

{
  "action": "sendMessage",
  "data": "hello"
}

Handling $connect

The $connect handler receives a unique connectionId. Store it (e.g. in DynamoDB) so you can message that client later.

exports.handler = async (event) => {
  const id = event.requestContext.connectionId;
  await save(id);
  return { statusCode: 200 };
};

Handling $disconnect

On disconnect, remove the stored connectionId so you do not try to message a dead client.

exports.handler = async (event) => {
  await remove(event.requestContext.connectionId);
  return { statusCode: 200 };
};

Pushing Messages to a Client

To send data back, call the management API with the target connectionId.

const api = new ApiGatewayManagementApi({ endpoint });
await api.postToConnection({
  ConnectionId: id,
  Data: JSON.stringify({ msg: "hi" })
});

Broadcasting to Many Clients

To broadcast, loop over all stored connectionIds and post to each. Remove any that return a 410 Gone status — those clients have disconnected.

Authorizing Connections

Attach a Lambda authorizer to the $connect route to validate a token before the connection is accepted, rejecting unauthorized clients up front.

Connection Lifetime and Limits

WebSocket connections last up to 2 hours, with a 10-minute idle timeout. Clients should reconnect when dropped, and you should handle stale connectionIds gracefully.

Pricing Model

You pay for messages transferred and for connection-minutes, not for idle request count. This makes WebSocket APIs cost-effective for bursty real-time traffic.

When to Use WebSockets

Reach for WebSocket APIs when you need:

  • Live chat or collaboration
  • Real-time dashboards
  • Server-initiated notifications

For simple request/response, stick with REST or HTTP APIs.

Quick Check

Test your WebSocket API knowledge.

Recap

You learned real-time APIs:

  • WebSocket APIs add two-way communication
  • $connect, $disconnect, $default plus custom routes
  • Store connectionIds and push via postToConnection
  • Authorize on $connect; handle stale connections

Pertanyaan yang Sering Diajukan

Apakah pelajaran “API WebSocket untuk Komunikasi Waktu Nyata” gratis?

Ya — teks lengkap “API WebSocket untuk Komunikasi Waktu Nyata” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Serverless Backend with AWS Lambda & API Gateway, upgrade ke CoddyKit PRO. Kursus Serverless Backend with AWS Lambda & API Gateway mencakup 4 pelajaran total.

Apa yang akan aku pelajari di “API WebSocket untuk Komunikasi Waktu Nyata”?

Bangun koneksi dua arah dan waktu nyata dengan API WebSocket API Gateway. Pelajari rute koneksi, pemutusan koneksi, dan pesan, serta cara mengirim data kembali ke klien. Kamu berlatih Serverless Backend with AWS Lambda & API Gateway dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.

Apakah aku perlu pengalaman untuk memulai Serverless Backend with AWS Lambda & API Gateway?

Tidak diperlukan pengalaman sebelumnya. Serverless Backend with AWS Lambda & API Gateway di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 4 dari 4.

Berapa lama pelajaran “API WebSocket untuk Komunikasi Waktu Nyata” memakan waktu?

Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.

Bisakah aku menulis dan menjalankan kode dalam pelajaran Serverless Backend with AWS Lambda & API Gateway ini?

Ya. Setiap pelajaran Serverless Backend with AWS Lambda & API Gateway menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.

Semua pelajaran dalam kursus ini

  1. Caching dan Pembatasan Laju
  2. Transformasi Permintaan/Respons
  3. Nama Domain Khusus dan Optimasi Edge
  4. API WebSocket untuk Komunikasi Waktu Nyata
← Kembali ke Serverless Backend with AWS Lambda & API Gateway