Pub/Sub 메시지 쓰기 및 응답

리전 ID

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

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

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

이 문서에서는 Cloud 클라이언트 라이브러리 .

기본 요건

샘플 앱 복제

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

Go

v1.18 이상

    git clone https://github.com/GoogleCloudPlatform/golang-samples.git
    cd golang-samples/appengine_flexible/pubsub

v1.15 이하

    git clone https://github.com/GoogleCloudPlatform/golang-samples.git
    cd golang-samples/appengine_flexible/go115_and_earlier/pubsub

Java

버전 11/17

git clone https://github.com/GoogleCloudPlatform/java-docs-samples
cd java-docs-samples/flexible/java-11/pubsub/

버전 8

git clone https://github.com/GoogleCloudPlatform/java-docs-samples
cd java-docs-samples/flexible/java-8/pubsub/

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

v3.8 이상

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

v3.7 및 이전

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

Ruby

버전 3.2

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

버전 3.1 이하

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

.NET

버전 6

git clone  https://github.com/GoogleCloudPlatform/dotnet-docs-samples
cd dotnet-docs-samples/appengine/flexible/Pubsub/Pubsub.Sample

버전 3.1 이하

git clone https://github.com/GoogleCloudPlatform/dotnet-docs-samples
git checkout flex-dotnet3-and-earlier
cd dotnet-docs-samples/appengine/flexible/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서비스 계정 이메일로 바꿉니다.

.NET

# 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 파일을 수정하여 주제 및 확인 토큰의 환경 변수를 설정합니다.

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-token

Java

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>

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: 1234abc

Ruby

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

env_variables:
    PUBSUB_TOPIC: <pubsub-topic-name>
    # This token is used to verify that requests originate from your
    # application. It can be any sufficiently random string.
    PUBSUB_VERIFICATION_TOKEN: <verification-token>

.NET

버전 6

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

runtime: aspnetcore
env: flex

runtime_config:
  operating_system: ubuntu22

env_variables:
  TEST_PROJECT_ID: your-project-id
  TEST_VERIFICATION_TOKEN: your-token
  TEST_TOPIC_ID: your-topic
  TEST_SUBSCRIPTION_ID: your-sub
  TEST_AUTH_SUBSCRIPTION_ID: your-auth-sub
  TEST_SERVICE_ACCOUNT_EMAIL: your-service-account-email

버전 3.1 이하

appsettings.json 파일을 수정하여 프로젝트 ID를 설정합니다.

{
  "Pubsub": {
    "ProjectId": "your-project-id",
    "VerificationToken": "your-secret-token",
    "TopicId": "your-topic",
    "SubscriptionId": "your-subscription",
    "AuthSubscriptionId": "your-auth-subscription",
    "ServiceAccountEmail": "your-service-account-email"
  },

  "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  }
}

코드 검토

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

Go

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

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

messages   []string

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


func pushHandler(w http.ResponseWriter, r *http.Request) {
	// Verify the token.
	if r.URL.Query().Get("token") != token {
		http.Error(w, "Bad token", http.StatusBadRequest)
		return
	}
	msg := &pushRequest{}
	if err := json.NewDecoder(r.Body).Decode(msg); err != nil {
		http.Error(w, fmt.Sprintf("Could not decode body: %v", err), http.StatusBadRequest)
		return
	}

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

publishHandler 함수는 새 메시지를 주제에 게시합니다.


func publishHandler(w http.ResponseWriter, r *http.Request) {
	ctx := context.Background()

	msg := &pubsub.Message{
		Data: []byte(r.FormValue("payload")),
	}

	if _, err := topic.Publish(ctx, msg).Get(ctx); err != nil {
		http.Error(w, fmt.Sprintf("Could not publish message: %v", err), 500)
		return
	}

	fmt.Fprint(w, "Message published.")
}

Java

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

String pubsubVerificationToken = System.getenv("PUBSUB_VERIFICATION_TOKEN");

샘플 앱은 메시지를 저장할 Cloud Datastore 데이터베이스 인스턴스를 유지합니다. PubSubPush 서블릿은 내보내기된 메시지를 수신하여 messageRepository 데이터베이스 인스턴스에 추가합니다.

버전 11/17

@WebServlet(value = "/pubsub/push")
@MultipartConfig()
public class PubSubPush extends HttpServlet {

  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws IOException, ServletException {
    String pubsubVerificationToken = System.getenv("PUBSUB_VERIFICATION_TOKEN");
    // Do not process message if request token does not match pubsubVerificationToken
    if (pubsubVerificationToken == null
        || pubsubVerificationToken.compareTo(req.getParameter("token")) != 0) {
      resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
      return;
    }
    // parse message object from "message" field in the request body json
    // decode message data from base64
    Message message = getMessage(req);
    try {
      messageRepository.save(message);
      // 200, 201, 204, 102 status codes are interpreted as success by the Pub/Sub system
      resp.setStatus(HttpServletResponse.SC_OK);
    } catch (Exception e) {
      System.out.println(e);
      resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
  }

버전 8

@WebServlet(value = "/pubsub/push")
public class PubSubPush extends HttpServlet {

  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws IOException, ServletException {
    String pubsubVerificationToken = System.getenv("PUBSUB_VERIFICATION_TOKEN");
    // Do not process message if request token does not match pubsubVerificationToken
    if (req.getParameter("token").compareTo(pubsubVerificationToken) != 0) {
      resp.setStatus(HttpServletResponse.SC_BAD_REQUEST);
      return;
    }
    // parse message object from "message" field in the request body json
    // decode message data from base64
    Message message = getMessage(req);
    try {
      messageRepository.save(message);
      // 200, 201, 204, 102 status codes are interpreted as success by the Pub/Sub system
      resp.setStatus(HttpServletResponse.SC_OK);
    } catch (Exception e) {
      resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    }
  }

PubSubPublish 서블릿은 App Engine 웹 앱과 상호작용하여 새 메시지를 게시하고 수신된 메시지를 표시합니다.

@WebServlet(name = "Publish with PubSub", value = "/pubsub/publish")
public class PubSubPublish extends HttpServlet {

  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws IOException, ServletException {
    Publisher publisher = this.publisher;
    try {
      String topicId = System.getenv("PUBSUB_TOPIC");
      // create a publisher on the topic
      if (publisher == null) {
        publisher = Publisher.newBuilder(
            ProjectTopicName.of(ServiceOptions.getDefaultProjectId(), topicId))
            .build();
      }
      // construct a pubsub message from the payload
      final String payload = req.getParameter("payload");
      PubsubMessage pubsubMessage =
          PubsubMessage.newBuilder().setData(ByteString.copyFromUtf8(payload)).build();

      publisher.publish(pubsubMessage);
      // redirect to home page
      resp.sendRedirect("/");
    } catch (Exception e) {
      resp.sendError(HttpStatus.SC_INTERNAL_SERVER_ERROR, e.getMessage());
    }
  }

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: php
env: flex

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

$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 = []

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

@app.route("/pubsub/push", methods=["POST"])
def pubsub_push():
    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)

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

    # publisher = pubsub_v1.PublisherClient()
    topic_path = publisher.topic_path(
        current_app.config["PROJECT"], current_app.config["PUBSUB_TOPIC"]
    )

    publisher.publish(topic_path, data=data)

    return "OK", 200

Ruby

샘플 앱은 app.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

.NET

버전 6

[HttpGet]
[HttpPost]
public async Task<IActionResult> IndexAsync(MessageForm messageForm)
{
    var model = new MessageList();
    if (!_options.HasGoodProjectId())
    {
        model.MissingProjectId = true;
        return View(model);
    }
    if (!string.IsNullOrEmpty(messageForm.Message))
    {
        // Publish the message.
        var pubsubMessage = new PubsubMessage()
        {
            Data = ByteString.CopyFromUtf8(messageForm.Message)
        };
        pubsubMessage.Attributes["token"] = _options.VerificationToken;
        await _publisher.PublishAsync(pubsubMessage);
        model.PublishedMessage = messageForm.Message;
    }
    // Render the current list of messages.
    model.Messages = s_receivedMessages.ToArray();
    model.AuthMessages = s_authenticatedMessages.ToArray();
    return View(model);
}

버전 3.1 이하

[HttpGet]
[HttpPost]
public IActionResult Index(MessageForm messageForm)
{
    var model = new MessageList();
    if (!_options.HasGoodProjectId())
    {
        model.MissingProjectId = true;
        return View(model);
    }
    if (!string.IsNullOrEmpty(messageForm.Message))
    {
        // Publish the message.
        var pubsubMessage = new PubsubMessage()
        {
            Data = ByteString.CopyFromUtf8(messageForm.Message)
        };
        pubsubMessage.Attributes["token"] = _options.VerificationToken;
        _publisher.PublishAsync(pubsubMessage);
        model.PublishedMessage = messageForm.Message;
    }
    // Render the current list of messages.
    model.Messages = s_receivedMessages.ToArray();
    model.AuthMessages = s_authenticatedMessages.ToArray();
    return View(model);
}

로컬에서 샘플 실행

로컬에서 실행 시 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

mvn clean package

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

export PUBSUB_VERIFICATION_TOKEN=[your-verification-token]
export PUBSUB_TOPIC=[your-topic]
mvn jetty:run

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

.NET

애플리케이션의 루트 디렉터리에서 다음 명령어를 실행합니다.

    dotnet restore
    dotnet run

웹브라우저에서 http://localhost:5000/을 입력합니다. 웹 서버를 종료하려면 터미널 창에서 Ctrl+C를 누릅니다.

푸시 알림 시뮬레이션

애플리케이션은 로컬에서 메시지를 보낼 수 있지만 로컬에서 푸시 메시지를 받지는 못합니다. 하지만 로컬 푸시 알림 엔드포인트에 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

응답:

HTTP/1.1 200 OK
Date: Wed, 26 Apr 2017 00:03:28 GMT
Content-Length: 0
Server: Jetty(9.3.8.v20160314)

요청이 완료되면 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을 새로고침하고 수신 메시지 목록에서 메시지를 확인할 수 있습니다.

.NET

HTTP POST 요청을 보내려면 다음 안내를 따르세요.

Get-Content -Raw .\sample_message.json | Invoke-WebRequest -Uri
http://localhost:5000/Push?token=your-secret-token -Method POST -ContentType
'text/json' -OutFile out.txt

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

App Engine에서 실행

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

Go

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

gcloud app deploy

Java

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.yaml

.NET

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

gcloud app deploy

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