0Pricing
Serverless Backend with AWS Lambda & API Gateway · レッスン

リアルタイム通信のためのWebSocket API

API Gateway WebSocket APIを使って双方向のリアルタイム接続を構築します。connect、disconnect、messageの各ルートと、クライアントへデータをプッシュする方法を学びます。

「リアルタイム通信のためのWebSocket API」はCoddyKit上の無料Serverless Backend with AWS Lambda & API Gatewayレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはServerless Backend with AWS Lambda & API Gateway学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Serverless Backend with AWS Lambda & API Gatewayコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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

よくある質問

「リアルタイム通信のためのWebSocket API」レッスンは無料ですか?

はい。「リアルタイム通信のためのWebSocket API」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Serverless Backend with AWS Lambda & API Gatewayコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Serverless Backend with AWS Lambda & API Gatewayコースには全4レッスンが含まれています。

「リアルタイム通信のためのWebSocket API」で何を学びますか?

API Gateway WebSocket APIを使って双方向のリアルタイム接続を構築します。connect、disconnect、messageの各ルートと、クライアントへデータをプッシュする方法を学びます。 ブラウザで直接実行するハンズオンコードでServerless Backend with AWS Lambda & API Gatewayを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Serverless Backend with AWS Lambda & API Gatewayを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのServerless Backend with AWS Lambda & API Gatewayは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「リアルタイム通信のためのWebSocket API」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このServerless Backend with AWS Lambda & API Gatewayレッスンでコードを書いて実行できますか?

はい。すべてのServerless Backend with AWS Lambda & API Gatewayレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. キャッシュとスロットリング
  2. リクエストとレスポンスの変換
  3. カスタムドメイン名とエッジ最適化
  4. リアルタイム通信のためのWebSocket API
← Serverless Backend with AWS Lambda & API Gatewayに戻る