SVG

How To Send Data From Your PHP Codebase to Google Pub/Sub

This tutorial will explore how to send data from your PHP codebase to Google Pub/Sub. Google Pub/Sub is a messaging service that allows you to send and receive messages between independent applications. It provides a reliable and scalable solution for data transmission, making it a popular choice among developers.

Understanding Google Pub/Sub and its benefits

Before diving into the integration steps, let’s first understand what Google PubSub is and why you may want to use it.

What is Google Pub/Sub?

Google Pub/Sub is a messaging service that follows the publish-subscribe pattern. It consists of two primary components: publishers and subscribers. Publishers are responsible for sending messages to topics, while subscribers receive and process those messages.

Topics act as channels through which messages are sent. You can think of them as virtual mailboxes, where each subscriber is assigned a separate mailbox. When a publisher sends a message to a topic, Google Pub/Sub delivers it to all the subscribers of that topic. This allows for asynchronous communication between different applications.

Using the publish-subscribe pattern, Google Pub/Sub ensures that messages are delivered to all interested parties without direct communication between them. This decoupling of components promotes loose coupling and improves your system's overall scalability and flexibility.

Google Pub/Sub is a part of Google Cloud Platform (GCP), so it is easier to integrate and manage other Google Cloud Platform services, including BigQuery, Kubernetes Engine, Cloud Storage, Cloud SQL, Machine Learning Engine, etc. Such integrations can enable multiple use cases, which may take a lot of time otherwise.

Why use Google Pub/Sub?

Google PubSub is suitable for various use cases, including real-time messaging, notification systems, data stream analytics, workflow automation, etc. There are several reasons to consider using Google Pub/Sub in your PHP projects.

First, it provides reliable message delivery. With Google's infrastructure handling the message transmission, you don't have to worry about lost messages or delivery failures. This is especially important in scenarios where data integrity is crucial, such as financial transactions or real-time analytics.

Second, Google Pub/Sub enables you to scale your applications quickly. As your application grows, you can increase the number of subscribers without worrying about the infrastructure's limitations. This makes Google Pub/Sub an excellent choice for high-traffic applications where scalability is a priority. Whether you have a few subscribers or thousands, Google Pub/Sub can handle the load and ensure messages are delivered efficiently.

Finally, Google Pub/Sub helps decouple your applications. By using a messaging service like Google Pub/Sub, you can separate the components of your system and make them more modular. This makes it easier to maintain and update your codebase without affecting other parts of the system. For example, if you need to update the logic of a specific subscriber, you can do so without impacting the publishers or other subscribers. This flexibility allows for better code organization and reduces the risk of introducing bugs during updates.

Setting up Google Cloud PubSub

In order to use Google PubSub, you need to enable the PubSub API and create a service account.

Enabling the Google Cloud PubSub API

Create a Google Cloud project and enable the Pub/Sub API. You can do this in the Google Cloud console.

Configuring Google Pub/Sub credentials

To authenticate your connection to Google Pub/Sub, you can either use the `gcloud auth` command of Google Cloud CLI or use service account credentials. We will use the latter approach here. We will create a Service Account Key in the Google Cloud Console. The Service Account Key contains the necessary authentication information, including the project ID, client email, and private key.

Once you have the Service Account Key, you can use it to authenticate your PHP code. The authentication process typically involves loading the key file and using it to create a new instance of the Google Cloud Pub\Sub library. It involves the following steps:

  1. Create a service account in the Google Cloud Console and download its JSON key.
  2. Set an environment variable pointing to the key:
SH

export GOOGLE_APPLICATION_CREDENTIALS="/path/to/your/service-account.json"

Alternatively, you can set up the `.env` file, and define the 'GOOGLE_APPLICATION_CREDENTIALS' variable and specify the path to the service account key file. This ensures your PHP application can locate and use the credentials when interacting with Google Pub/Sub.

Your PHP script can now use this service account for authentication.

Remember, always ensure the security of your service account keys. Never expose them in your public code or repositories.

Sending data from PHP to Google Pub/Sub

Google Cloud Platform provides Google PubSub REST APIs to interact with Google PubSub. It also provides SDKs in popular languages/frameworks (C++, Python, PHP, Java, Node.js, etc.). We will use the Google Cloud PHP SDK to ensure we hit the correct Pub/Sub API endpoint and send the data in a valid format.

Installing Google Cloud Pub/Sub PHP client library

To install the 'google/cloud-pubsub' package, you can use Composer. Execute the following command in your PHP project:

SH

composer require google/cloud-pubsub

Alternatively, you may define this dependency in the 'composer.json' file in your project's root directory. In this file, you specify the required package dependency, which is `google/cloud-pubsub.` Run the command `composer install` to download and install the package. Composer will automatically fetch the required package and all its dependencies.

Creating a new Google Cloud Pub/Sub Topic

To create a topic in your project, you can either use the Google Cloud Console or GCloud CLI. Follow these steps to create a new topic via Google Cloud Console:

  1. Navigate to the Google Cloud Console.
  2. Select "Pub/Sub" > "Topics".
  3. Create a new topic with the choice of your topic name, e.g., “my-topic.”

Publishing a message to a topic

To send a message to the topic you created, you will need to use `PubSubClient`’s `publish` method. Check out following code:

PHP

<?php

require 'vendor/autoload.php';

use Google\Cloud\PubSub\PubSubClient;

// Create a Pub/Sub client.

$pubSub = new PubSubClient();

$topic =$pubSub->topic('my-topic');

// Create a message payload

$message = [

'data' => 'Check out RudderStack Data Learning Center',

'attributes' => [

'website' => 'rudderstack.com/learn'

]

]

// Publish the message to the topic.

$topic->publish($message)

echo 'Message published successfully!';

?>

Now you can save this script and run it, and your message will be published to the topic. Google Pub/Sub takes care of delivering the message to all the subscribers. To debug, check published messages. To check published messages, you need to either create a subscription or pull the list of all the subscriptions.

This is a simplified example. For production systems, it's crucial to implement error handling. Be sure to wrap your code in appropriate try/catch blocks and handle exceptions thrown by the Google Cloud PHP Client Library.

That’s it. You have successfully sent the data to Google PubSub. Read Google Cloud PHP Client Library, Google PubSub docs, Google PubSub API references for more information.

Conclusion

We demonstrated how to send messages to Google Cloud PubSub from the PHP codebase using Google Cloud PHP SDK. By leveraging the power of Google Pub/Sub, you can decouple your applications, improve scalability, and ensure the integrity of your data.

Don't want to go through the pain of direct integration? RudderStack's PHP SDK makes it easy to send data from your PHP app to Google Pub/Sub.