Pub/Sub 메시지 쓰기 및 응답

리전 ID

REGION_ID는 앱을 만들 때 선택한 리전을 기준으로 Google에서 할당하는 축약된 코드입니다. 일부 리전 ID는 일반적으로 사용되는 국가 및 주/도 코드와 비슷하게 표시될 수 있지만 코드는 국가 또는 주/도와 일치하지 않습니다. 2020년 2월 이후에 생성된 앱의 경우 REGION_ID.r이 App Engine URL에 포함됩니다. 이 날짜 이전에 만든 기존 앱의 경우 URL에서 리전 ID는 선택사항입니다.

리전 ID에 대해 자세히 알아보세요.

Pub/Sub는 애플리케이션 사이에서 안정적인 다대다 비동기 메시징 기능을 제공합니다. 게시자 애플리케이션은 메시지를 주제로 보낼 수 있으며 다른 애플리케이션은 주제를 구독하여 메시지를 수신할 수 있습니다.

이 문서에서는 Cloud 클라이언트 라이브러리를 사용하여 App Engine 앱에서 Pub/Sub 메시지를 전송 및 수신하는 방법을 설명합니다.

기본 요건

샘플 앱 복제

샘플 앱을 로컬 머신에 복사한 후 pubsub 디렉터리로 이동합니다.

Go

git clone https://github.com/GoogleCloudPlatform/golang-samples.git
cd golang-samples/appengine/go11x/pubsub/authenicated_push

Java

이 런타임에 사용할 수 있는 예시가 없습니다.

참고로 자바 8 및 자바 11/17 데모 앱은 가변형 환경에서 사용할 수 있습니다.

Node.js

git clone https://github.com/GoogleCloudPlatform/nodejs-docs-samples
cd nodejs-docs-samples/appengine/pubsub

PHP

git clone https://github.com/GoogleCloudPlatform/php-docs-samples.git
cd php-docs-samples/pubsub

Python

git clone https://github.com/GoogleCloudPlatform/python-docs-samples
cd python-docs-samples/appengine/standard_python3/pubsub

Ruby

git clone https://github.com/GoogleCloudPlatform/ruby-docs-samples
cd ruby-docs-samples/appengine/pubsub

주제 및 구독 만들기

주제 및 구독을 만듭니다. 이때 Pub/Sub 서버가 요청을 전송할 엔드포인트를 지정해야 합니다.

Go

# Configure the topic
gcloud pubsub topics create YOUR_TOPIC_NAME

# Configure the push subscription
gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \
    --topic=YOUR_TOPIC_NAME \
    --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \
    --ack-deadline=10

YOUR_TOKEN을 무작위의 비밀 토큰으로 바꿉니다. 내보내기 엔드포인트에서 이 토큰을 사용해 요청을 확인합니다.

인증을 통해 Pub/Sub를 사용하려면 다른 구독을 만듭니다.

# Configure the push subscription
gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \
    --topic=YOUR_TOPIC_NAME \
    --push-auth-service-account=YOUR-SERVICE-ACCOUNT-EMAIL\
    --push-auth-token-audience=OPTIONAL_AUDIENCE_OVERRIDE\
    --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \
    --ack-deadline=10

# Your Google-managed service account
# `service-{PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com` needs to have the
# `iam.serviceAccountTokenCreator` role.
PUBSUB_SERVICE_ACCOUNT="service-${PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com"
gcloud projects add-iam-policy-binding ${PROJECT_ID} \
    --member="serviceAccount:${PUBSUB_SERVICE_ACCOUNT}"\
    --role='roles/iam.serviceAccountTokenCreator'

YOUR-SERVICE-ACCOUNT-EMAIL서비스 계정 이메일로 바꿉니다.

Java

# Configure the topic
gcloud pubsub topics create YOUR_TOPIC_NAME

# Configure the push subscription
gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \
    --topic=YOUR_TOPIC_NAME \
    --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \
    --ack-deadline=10

YOUR_TOKEN을 무작위의 비밀 토큰으로 바꿉니다. 내보내기 엔드포인트에서 이 토큰을 사용해 요청을 확인합니다.

인증을 통해 Pub/Sub를 사용하려면 다른 구독을 만듭니다.

# Configure the push subscription
gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \
    --topic=YOUR_TOPIC_NAME \
    --push-auth-service-account=YOUR-SERVICE-ACCOUNT-EMAIL\
    --push-auth-token-audience=OPTIONAL_AUDIENCE_OVERRIDE\
    --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \
    --ack-deadline=10

# Your Google-managed service account
# `service-{PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com` needs to have the
# `iam.serviceAccountTokenCreator` role.
PUBSUB_SERVICE_ACCOUNT="service-${PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com"
gcloud projects add-iam-policy-binding ${PROJECT_ID} \
    --member="serviceAccount:${PUBSUB_SERVICE_ACCOUNT}"\
    --role='roles/iam.serviceAccountTokenCreator'

YOUR-SERVICE-ACCOUNT-EMAIL서비스 계정 이메일로 바꿉니다.

Node.js

# Configure the topic
gcloud pubsub topics create YOUR_TOPIC_NAME

# Configure the push subscription
gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \
    --topic=YOUR_TOPIC_NAME \
    --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \
    --ack-deadline=10

YOUR_TOKEN을 무작위의 비밀 토큰으로 바꿉니다. 내보내기 엔드포인트에서 이 토큰을 사용해 요청을 확인합니다.

인증을 통해 Pub/Sub를 사용하려면 다른 구독을 만듭니다.

# Configure the push subscription
gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \
    --topic=YOUR_TOPIC_NAME \
    --push-auth-service-account=YOUR-SERVICE-ACCOUNT-EMAIL\
    --push-auth-token-audience=OPTIONAL_AUDIENCE_OVERRIDE\
    --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \
    --ack-deadline=10

# Your Google-managed service account
# `service-{PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com` needs to have the
# `iam.serviceAccountTokenCreator` role.
PUBSUB_SERVICE_ACCOUNT="service-${PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com"
gcloud projects add-iam-policy-binding ${PROJECT_ID} \
    --member="serviceAccount:${PUBSUB_SERVICE_ACCOUNT}"\
    --role='roles/iam.serviceAccountTokenCreator'

YOUR-SERVICE-ACCOUNT-EMAIL서비스 계정 이메일로 바꿉니다.

PHP

# Configure the topic
gcloud pubsub topics create YOUR_TOPIC_NAME

# Configure the push subscription
gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \
    --topic=YOUR_TOPIC_NAME \
    --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \
    --ack-deadline=10

YOUR_TOKEN을 무작위의 비밀 토큰으로 바꿉니다. 내보내기 엔드포인트에서 이 토큰을 사용해 요청을 확인합니다.

인증을 통해 Pub/Sub를 사용하려면 다른 구독을 만듭니다.

# Configure the push subscription
gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \
    --topic=YOUR_TOPIC_NAME \
    --push-auth-service-account=YOUR-SERVICE-ACCOUNT-EMAIL\
    --push-auth-token-audience=OPTIONAL_AUDIENCE_OVERRIDE\
    --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \
    --ack-deadline=10

# Your Google-managed service account
# `service-{PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com` needs to have the
# `iam.serviceAccountTokenCreator` role.
PUBSUB_SERVICE_ACCOUNT="service-${PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com"
gcloud projects add-iam-policy-binding ${PROJECT_ID} \
    --member="serviceAccount:${PUBSUB_SERVICE_ACCOUNT}"\
    --role='roles/iam.serviceAccountTokenCreator'

YOUR-SERVICE-ACCOUNT-EMAIL서비스 계정 이메일로 바꿉니다.

Python

# Configure the topic
gcloud pubsub topics create YOUR_TOPIC_NAME

# Configure the push subscription
gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \
    --topic=YOUR_TOPIC_NAME \
    --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \
    --ack-deadline=10

YOUR_TOKEN을 무작위의 비밀 토큰으로 바꿉니다. 내보내기 엔드포인트에서 이 토큰을 사용해 요청을 확인합니다.

인증을 통해 Pub/Sub를 사용하려면 다른 구독을 만듭니다.

# Configure the push subscription
gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \
    --topic=YOUR_TOPIC_NAME \
    --push-auth-service-account=YOUR-SERVICE-ACCOUNT-EMAIL\
    --push-auth-token-audience=OPTIONAL_AUDIENCE_OVERRIDE\
    --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \
    --ack-deadline=10

# Your Google-managed service account
# `service-{PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com` needs to have the
# `iam.serviceAccountTokenCreator` role.
PUBSUB_SERVICE_ACCOUNT="service-${PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com"
gcloud projects add-iam-policy-binding ${PROJECT_ID} \
    --member="serviceAccount:${PUBSUB_SERVICE_ACCOUNT}"\
    --role='roles/iam.serviceAccountTokenCreator'

YOUR-SERVICE-ACCOUNT-EMAIL서비스 계정 이메일로 바꿉니다.

Ruby

# Configure the topic
gcloud pubsub topics create YOUR_TOPIC_NAME

# Configure the push subscription
gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \
    --topic=YOUR_TOPIC_NAME \
    --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \
    --ack-deadline=10

YOUR_TOKEN을 무작위의 비밀 토큰으로 바꿉니다. 내보내기 엔드포인트에서 이 토큰을 사용해 요청을 확인합니다.

인증을 통해 Pub/Sub를 사용하려면 다른 구독을 만듭니다.

# Configure the push subscription
gcloud pubsub subscriptions create YOUR_SUBSCRIPTION_NAME \
    --topic=YOUR_TOPIC_NAME \
    --push-auth-service-account=YOUR-SERVICE-ACCOUNT-EMAIL\
    --push-auth-token-audience=OPTIONAL_AUDIENCE_OVERRIDE\
    --push-endpoint=https://YOUR_PROJECT_ID.REGION_ID.r.appspot.com/push-handlers/receive_messages?token=YOUR_TOKEN \
    --ack-deadline=10

# Your Google-managed service account
# `service-{PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com` needs to have the
# `iam.serviceAccountTokenCreator` role.
PUBSUB_SERVICE_ACCOUNT="service-${PROJECT_NUMBER}@gcp-sa-pubsub.iam.gserviceaccount.com"
gcloud projects add-iam-policy-binding ${PROJECT_ID} \
    --member="serviceAccount:${PUBSUB_SERVICE_ACCOUNT}"\
    --role='roles/iam.serviceAccountTokenCreator'

YOUR-SERVICE-ACCOUNT-EMAIL서비스 계정 이메일로 바꿉니다.

환경 변수 설정

Go

app.yaml 파일을 수정하여 주제 및 확인 토큰의 환경 변수를 설정합니다.

# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

runtime: go121
env_variables:
    # This token is used to verify that requests originate from your
    # application. It can be any sufficiently random string.
    PUBSUB_VERIFICATION_TOKEN: check123

Java

app.yaml 파일을 수정하여 주제 및 확인 토큰의 환경 변수를 설정합니다.

env_variables:
  PUBSUB_TOPIC: <your-topic-name>
  PUBSUB_VERIFICATION_TOKEN: <your-verification-token>

Node.js

app.yaml 파일을 수정하여 주제 및 확인 토큰의 환경 변수를 설정합니다.

env_variables:
  PUBSUB_TOPIC: YOUR_TOPIC_NAME
  # This token is used to verify that requests originate from your
  # application. It can be any sufficiently random string.
  PUBSUB_VERIFICATION_TOKEN: YOUR_VERIFICATION_TOKEN

PHP

index.php 파일을 수정하여 주제와 구독의 환경 변수를 설정합니다.

$container->set('topic', 'php-example-topic');
$container->set('subscription', 'php-example-subscription');

Python

app.yaml 파일을 수정하여 프로젝트 ID, 주제, 확인 토큰의 환경 변수를 설정합니다.

env_variables:
  PUBSUB_TOPIC: '<YOUR_TOPIC>'
  # This token is used to verify that requests originate from your
  # application. It can be any sufficiently random string.
  PUBSUB_VERIFICATION_TOKEN: '<YOUR_VERIFICATION_TOKEN>'

Ruby

app.standard.yaml 파일을 수정하여 프로젝트 ID, 주제, 확인 토큰의 환경 변수를 설정합니다.

env_variables:
    PUBSUB_TOPIC: gaeflex_net_pubsub_auth_push_1
    # This token is used to verify that requests originate from your
    # application. It can be any sufficiently random string.
    PUBSUB_VERIFICATION_TOKEN: abc123

코드 검토

샘플 앱은 Pub/Sub 클라이언트 라이브러리를 사용합니다.

Go

샘플 앱은 구성에서 app.yaml 파일(PUBSUB_TOPICPUBSUB_VERIFICATION_TOKEN)에 설정한 환경 변수를 사용합니다.

이 인스턴스에서 수신한 메시지는 슬라이스에 저장됩니다.

messages   []string

receiveMessagesHandler 함수는 푸시된 메시지를 수신하고 토큰을 확인하고 메시지를 messages 슬라이스에 추가합니다.


// receiveMessagesHandler validates authentication token and caches the Pub/Sub
// message received.
func (a *app) receiveMessagesHandler(w http.ResponseWriter, r *http.Request) {
	if r.Method != "POST" {
		http.Error(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
		return
	}

	// Verify that the request originates from the application.
	// a.pubsubVerificationToken = os.Getenv("PUBSUB_VERIFICATION_TOKEN")
	if token, ok := r.URL.Query()["token"]; !ok || len(token) != 1 || token[0] != a.pubsubVerificationToken {
		http.Error(w, "Bad token", http.StatusBadRequest)
		return
	}

	// Get the Cloud Pub/Sub-generated JWT in the "Authorization" header.
	authHeader := r.Header.Get("Authorization")
	if authHeader == "" || len(strings.Split(authHeader, " ")) != 2 {
		http.Error(w, "Missing Authorization header", http.StatusBadRequest)
		return
	}
	token := strings.Split(authHeader, " ")[1]
	// Verify and decode the JWT.
	// If you don't need to control the HTTP client used you can use the
	// convenience method idtoken.Validate instead of creating a Validator.
	v, err := idtoken.NewValidator(r.Context(), option.WithHTTPClient(a.defaultHTTPClient))
	if err != nil {
		http.Error(w, "Unable to create Validator", http.StatusBadRequest)
		return
	}
	// Please change http://example.com to match with the value you are
	// providing while creating the subscription.
	payload, err := v.Validate(r.Context(), token, "http://example.com")
	if err != nil {
		http.Error(w, fmt.Sprintf("Invalid Token: %v", err), http.StatusBadRequest)
		return
	}
	if payload.Issuer != "accounts.google.com" && payload.Issuer != "https://accounts.google.com" {
		http.Error(w, "Wrong Issuer", http.StatusBadRequest)
		return
	}

	// IMPORTANT: you should validate claim details not covered by signature
	// and audience verification above, including:
	//   - Ensure that `payload.Claims["email"]` is equal to the expected service
	//     account set up in the push subscription settings.
	//   - Ensure that `payload.Claims["email_verified"]` is set to true.
	if payload.Claims["email"] != "test-service-account-email@example.com" || payload.Claims["email_verified"] != true {
		http.Error(w, "Unexpected email identity", http.StatusBadRequest)
		return
	}

	var pr pushRequest
	if err := json.NewDecoder(r.Body).Decode(&pr); err != nil {
		http.Error(w, fmt.Sprintf("Could not decode body: %v", err), http.StatusBadRequest)
		return
	}

	a.messagesMu.Lock()
	defer a.messagesMu.Unlock()
	// Limit to ten.
	a.messages = append(a.messages, pr.Message.Data)
	if len(a.messages) > maxMessages {
		a.messages = a.messages[len(a.messages)-maxMessages:]
	}

	fmt.Fprint(w, "OK")
}

Java

이 런타임에 사용할 수 있는 예시가 없습니다.

자바 8 데모 앱은 가변형 환경에서 사용할 수 있습니다.

Node.js

샘플 앱은 app.yaml 파일에 설정된 값을 사용하여 환경 변수를 구성합니다. 푸시 요청 핸들러는 이러한 값을 사용하여 요청이 Pub/Sub에서 수신되었으며 신뢰할 수 있는 소스가 보낸 것인지 확인합니다.

// The following environment variables are set by the `app.yaml` file when
// running on App Engine, but will need to be manually set when running locally.
var PUBSUB_VERIFICATION_TOKEN = process.env.PUBSUB_VERIFICATION_TOKEN;
var pubsub = gcloud.pubsub({
    projectId: process.env.GOOGLE_CLOUD_PROJECT
});
var topic = pubsub.topic(process.env.PUBSUB_TOPIC);

샘플 앱은 전역 목록을 유지 관리하여 이 인스턴스에서 수신된 메시지를 저장합니다.

// List of all messages received by this instance
var messages = [];

이 메서드는 푸시된 메시지를 받아 messages 전역 목록에 추가합니다.

app.post('/pubsub/push', jsonBodyParser, (req, res) => {
  if (req.query.token !== PUBSUB_VERIFICATION_TOKEN) {
    res.status(400).send();
    return;
  }

  // The message is a unicode string encoded in base64.
  const message = Buffer.from(req.body.message.data, 'base64').toString(
    'utf-8'
  );

  messages.push(message);

  res.status(200).send();
});

이 메서드는 App Engine 웹 앱과 상호작용하여 새 메시지를 게시하고 수신된 메시지를 표시합니다.

app.get('/', (req, res) => {
  res.render('index', {messages, tokens, claims});
});

app.post('/', formBodyParser, async (req, res, next) => {
  if (!req.body.payload) {
    res.status(400).send('Missing payload');
    return;
  }

  const data = Buffer.from(req.body.payload);
  try {
    const messageId = await topic.publishMessage({data});
    res.status(200).send(`Message ${messageId} sent.`);
  } catch (error) {
    next(error);
  }
});

PHP

샘플 앱은 app.yaml 파일에 설정된 값을 사용하여 환경 변수를 구성합니다. 푸시 요청 핸들러는 이러한 값을 사용하여 요청이 Pub/Sub에서 수신되었으며 신뢰할 수 있는 소스가 보낸 것인지 확인합니다.

runtime: php81

handlers:
- url: /pubsub\.js
  static_files: pubsub.js
  upload: pubsub\.js

샘플 앱은 전역 목록을 유지 관리하여 이 인스턴스에서 수신된 메시지를 저장합니다.

$messages = [];

pull 메서드는 생성된 주제에서 메시지를 검색하여 메시지 목록에 추가합니다.

// get PULL pubsub messages
$pubsub = new PubSubClient([
    'projectId' => $projectId,
]);
$subscription = $pubsub->subscription($subscriptionName);
$pullMessages = [];
foreach ($subscription->pull(['returnImmediately' => true]) as $pullMessage) {
    $pullMessages[] = $pullMessage;
    $messages[] = $pullMessage->data();
}
// acknowledge PULL messages
if ($pullMessages) {
    $subscription->acknowledgeBatch($pullMessages);
}

publish 메서드는 새 메시지를 주제에 게시합니다.

if ($message = (string) $request->getBody()) {
    // Publish the pubsub message to the topic
    $pubsub = new PubSubClient([
        'projectId' => $projectId,
    ]);
    $topic = $pubsub->topic($topicName);
    $topic->publish(['data' => $message]);
    return $response->withStatus(204);
}

Python

샘플 앱은 app.yaml 파일에 설정된 값을 사용하여 환경 변수를 구성합니다. 푸시 요청 핸들러는 이러한 값을 사용하여 요청이 Pub/Sub에서 수신되었으며 신뢰할 수 있는 소스가 보낸 것인지 확인합니다.

app.config['PUBSUB_VERIFICATION_TOKEN'] = \
    os.environ['PUBSUB_VERIFICATION_TOKEN']
app.config['PUBSUB_TOPIC'] = os.environ['PUBSUB_TOPIC']

샘플 앱은 전역 목록을 유지 관리하여 이 인스턴스에서 수신된 메시지를 저장합니다.

MESSAGES = []

receive_messages_handler() 메서드는 푸시된 메시지를 수신하여 MESSAGES 전역 목록에 추가합니다.

@app.route("/pubsub/push", methods=["POST"])
def receive_pubsub_messages_handler():
    # Verify that the request originates from the application.
    if request.args.get("token", "") != current_app.config["PUBSUB_VERIFICATION_TOKEN"]:
        return "Invalid request", 400

    envelope = json.loads(request.data.decode("utf-8"))
    payload = base64.b64decode(envelope["message"]["data"])
    MESSAGES.append(payload)
    # Returning any 2xx status indicates successful receipt of the message.
    return "OK", 200

index() 메서드는 App Engine 웹 앱과 상호작용하여 새 메시지를 게시하고 수신된 메시지를 표시합니다.

@app.route("/", methods=["GET", "POST"])
def index():
    if request.method == "GET":
        return render_template(
            "index.html", messages=MESSAGES, tokens=TOKENS, claims=CLAIMS
        )

    data = request.form.get("payload", "Example payload").encode("utf-8")

    # Consider initializing the publisher client outside this function
    # for better latency performance.
    publisher = pubsub_v1.PublisherClient()
    topic_path = publisher.topic_path(
        app.config["GOOGLE_CLOUD_PROJECT"], app.config["PUBSUB_TOPIC"]
    )
    future = publisher.publish(topic_path, data)
    future.result()
    return "OK", 200

Ruby

샘플 앱은 app.standard.yaml 파일에 설정된 값을 사용하여 환경 변수를 구성합니다. 푸시 요청 핸들러는 이러한 값을 사용하여 요청이 Pub/Sub에서 수신되었으며 신뢰할 수 있는 소스가 보낸 것인지 확인합니다.

topic = pubsub.topic ENV["PUBSUB_TOPIC"]
PUBSUB_VERIFICATION_TOKEN = ENV["PUBSUB_VERIFICATION_TOKEN"]

샘플 앱은 전역 목록을 유지 관리하여 이 인스턴스에서 수신된 메시지를 저장합니다.

# List of all messages received by this instance
messages = []

이 메서드는 푸시된 메시지를 받아 messages 전역 목록에 추가합니다.

post "/pubsub/push" do
  halt 400 if params[:token] != PUBSUB_VERIFICATION_TOKEN

  message = JSON.parse request.body.read
  payload = Base64.decode64 message["message"]["data"]

  messages.push payload
end

이 메서드는 App Engine 웹 앱과 상호작용하여 새 메시지를 게시하고 수신된 메시지를 표시합니다.

get "/" do
  @claims = claims
  @messages = messages

  slim :index
end

post "/publish" do
  topic.publish params[:payload]

  redirect "/", 303
end

로컬에서 샘플 실행

로컬에서 실행 시 Google Cloud CLI를 사용하여 Google Cloud API 사용을 위한 인증을 제공할 수 있습니다. 기본 요건의 설명대로 환경을 설정했다면 이 인증을 제공하는 gcloud init 명령어를 이미 실행한 것입니다.

Go

애플리케이션을 시작하기 전에 환경 변수를 설정합니다.

export GOOGLE_CLOUD_PROJECT=[your-project-id]
export PUBSUB_VERIFICATION_TOKEN=[your-token]
export PUBSUB_TOPIC=[your-topic]
go run pubsub.go

Java

애플리케이션을 시작하기 전에 환경 변수를 설정합니다.

export PUBSUB_VERIFICATION_TOKEN=[your-verification-token]
export PUBSUB_TOPIC=[your-topic]

애플리케이션을 로컬로 실행하려면 일반적으로 사용하는 개발 도구를 사용합니다.

Node.js

애플리케이션을 시작하기 전에 환경 변수를 설정합니다.

export GOOGLE_CLOUD_PROJECT=[your-project-id]
export PUBSUB_VERIFICATION_TOKEN=[your-verification-token]
export PUBSUB_TOPIC=[your-topic]
npm install
npm start

PHP

Composer를 사용하여 종속 항목을 설치합니다.

composer install

그런 다음 애플리케이션을 시작하기 전에 환경 변수를 설정합니다.

export GOOGLE_CLOUD_PROJECT=[your-project-id]
export PUBSUB_VERIFICATION_TOKEN=[your-verification-token]
export PUBSUB_TOPIC=[your-topic]
php -S localhost:8080

Python

종속 항목을 가상 환경에 설치하는 것이 좋습니다.

Mac OS/Linux

  1. 격리된 Python 환경을 만듭니다.
    python3 -m venv env
    source env/bin/activate
  2. 현재 위치가 샘플 코드가 있는 디렉터리가 아니면 hello_world 샘플 코드가 포함된 디렉터리로 이동합니다. 그런 후 종속 항목을 설치합니다.
    cd YOUR_SAMPLE_CODE_DIR
    pip install -r requirements.txt

Windows

PowerShell을 사용하여 Python 패키지를 실행합니다.

  1. PowerShell 설치 위치를 찾습니다.
  2. PowerShell 바로가기를 마우스 오른쪽 버튼으로 클릭하고 관리자 권한으로 시작합니다.
  3. 격리된 Python 환경을 만듭니다.
    python -m venv env
    .\env\Scripts\activate
  4. 프로젝트 디렉터리로 이동하여 종속 항목을 설치합니다. 현재 위치가 샘플 코드가 있는 디렉터리가 아니면 hello_world 샘플 코드가 포함된 디렉터리로 이동합니다. 그런 후 종속 항목을 설치합니다.
    cd YOUR_SAMPLE_CODE_DIR
    pip install -r requirements.txt

그런 다음 애플리케이션을 시작하기 전에 환경 변수를 설정합니다.

export GOOGLE_CLOUD_PROJECT=[your-project-id]
export PUBSUB_VERIFICATION_TOKEN=[your-verification-token]
export PUBSUB_TOPIC=[your-topic]
python main.py

Ruby

종속 항목을 설치합니다.

bundle install

그런 다음 애플리케이션을 시작하기 전에 환경 변수를 설정합니다.

export GOOGLE_CLOUD_PROJECT=[your-project-id]
export PUBSUB_VERIFICATION_TOKEN=[your-verification-token]
export PUBSUB_TOPIC=[your-topic]
bundle exec ruby app.rb -p 8080

푸시 알림 시뮬레이션

애플리케이션은 로컬에서 메시지를 보낼 수 있지만 로컬에서 푸시 메시지를 받지는 못합니다. 하지만 로컬 푸시 알림 엔드포인트에 HTTP 요청을 전송하면 푸시 메시지를 시뮬레이션할 수 있습니다. 샘플에는 sample_message.json 파일이 포함되어 있습니다.

Go

curl 또는 httpie 클라이언트를 사용하여 HTTP POST 요청을 보낼 수 있습니다.

curl -H "Content-Type: application/json" -i --data @sample_message.json "localhost:8080/push-handlers/receive_messages?token=[your-token]"

또는

http POST ":8080/push-handlers/receive_messages?token=[your-token]" < sample_message.json

응답:

HTTP/1.1 200 OK
Date: Tue, 13 Nov 2018 16:04:18 GMT
Content-Length: 0

요청이 완료되면 localhost:8080을 새로고침하고 수신 메시지 목록에서 메시지를 확인할 수 있습니다.

Java

curl 또는 httpie 클라이언트를 사용하여 HTTP POST 요청을 보낼 수 있습니다.

curl -H "Content-Type: application/json" -i --data @sample_message.json "localhost:8080/push-handlers/receive_messages?token=[your-token]"

또는

http POST ":8080/push-handlers/receive_messages?token=[your-token]" < sample_message.json

요청이 완료되면 localhost:8080을 새로고침하고 수신 메시지 목록에서 메시지를 확인할 수 있습니다.

Node.js

curl 또는 httpie 클라이언트를 사용하여 HTTP POST 요청을 보낼 수 있습니다.

curl -H "Content-Type: application/json" -i --data @sample_message.json "localhost:8080/push-handlers/receive_messages?token=[your-token]"

또는

http POST ":8080/push-handlers/receive_messages?token=[your-token]" < sample_message.json

응답:

HTTP/1.1 200 OK
Connection: keep-alive
Date: Mon, 31 Aug 2015 22:19:50 GMT
Transfer-Encoding: chunked
X-Powered-By: Express

요청이 완료되면 localhost:8080을 새로고침하고 수신 메시지 목록에서 메시지를 확인할 수 있습니다.

PHP

curl 또는 httpie 클라이언트를 사용하여 HTTP POST 요청을 보낼 수 있습니다.

curl -i --data @sample_message.json "localhost:4567/push-handlers/receive_messages?token=[your-token]"

또는

http POST ":4567/push-handlers/receive_messages?token=[your-token]" < sample_message.json

요청이 완료되면 localhost:8080을 새로고침하고 수신 메시지 목록에서 메시지를 확인할 수 있습니다.

Python

curl 또는 httpie 클라이언트를 사용하여 HTTP POST 요청을 보낼 수 있습니다.

curl -H "Content-Type: application/json" -i --data @sample_message.json "localhost:8080/pubsub/push?token=[your-token]"

또는

http POST ":8080/pubsub/push?token=[your-token]" < sample_message.json

응답:

HTTP/1.0 200 OK
Content-Length: 2
Content-Type: text/html; charset=utf-8
Date: Mon, 10 Aug 2015 17:52:03 GMT
Server: Werkzeug/0.10.4 Python/2.7.10

OK

요청이 완료되면 localhost:8080을 새로고침하고 수신 메시지 목록에서 메시지를 확인할 수 있습니다.

Ruby

curl 또는 httpie 클라이언트를 사용하여 HTTP POST 요청을 보낼 수 있습니다.

curl -i --data @sample_message.json "localhost:4567/push-handlers/receive_messages?token=[your-token]"

또는

http POST ":4567/push-handlers/receive_messages?token=[your-token]" < sample_message.json

응답:

HTTP/1.1 200 OK
Content-Type: text/html;charset=utf-8
Content-Length: 13
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Server: WEBrick/1.3.1 (Ruby/2.3.0/2015-12-25)
Date: Wed, 20 Apr 2016 20:56:23 GMT
Connection: Keep-Alive

Hello, World!

요청이 완료되면 localhost:8080을 새로고침하고 수신 메시지 목록에서 메시지를 확인할 수 있습니다.

App Engine에서 실행

gcloud 명령줄 도구를 사용하여 App Engine에 데모 앱을 배포하려면 다음 안내를 따르세요.

Go

app.yaml 파일이 있는 디렉터리에서 다음 명령어를 실행합니다.

gcloud app deploy

Java

app.yaml 파일이 있는 디렉터리에서 gcloud 명령어를 실행합니다.

gcloud app deploy

Maven을 사용하여 앱을 배포하려면 다음을 실행합니다.

mvn package appengine:deploy -Dapp.deploy.projectId=PROJECT_ID

PROJECT_ID를 Google Cloud 프로젝트의 ID로 바꿉니다. pom.xml 파일에 이미 프로젝트 ID가 지정된 경우 실행할 명령어에 -Dapp.deploy.projectId 속성을 포함하지 않아도 됩니다.

Node.js

app.yaml 파일이 있는 디렉터리에서 다음 명령어를 실행합니다.

gcloud app deploy

PHP

app.yaml 파일이 있는 디렉터리에서 다음 명령어를 실행합니다.

gcloud app deploy

Python

app.yaml 파일이 있는 디렉터리에서 다음 명령어를 실행합니다.

gcloud app deploy

Ruby

app.yaml 파일이 있는 디렉터리에서 다음 명령어를 실행합니다.

gcloud app deploy app.standard.yaml

이제 https://PROJECT_ID.REGION_ID.r.appspot.com에서 애플리케이션에 액세스할 수 있습니다. 양식을 사용하여 메시지를 제출할 수 있지만 애플리케이션에서 어떤 인스턴스가 알림을 수신하는지 보장하지는 못합니다. 여러 메시지를 전송하고 페이지를 새로 고쳐 수신된 메시지를 확인할 수 있습니다.