Created
June 5, 2018 15:58
-
-
Save ssi-anik/590f2af9e80e445d4477d3ecc54b2e65 to your computer and use it in GitHub Desktop.
Google PubSub With Go and PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
go get -u cloud.google.com/go/pubsub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
composer require google/cloud-pubsub |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"golang.org/x/net/context" | |
"cloud.google.com/go/pubsub" | |
"log" | |
"fmt" | |
) | |
func main() { | |
ctx := context.Background() | |
// Sets your Google Cloud Platform project ID. | |
projectID := "project-id" | |
// Creates a client. | |
client, err := pubsub.NewClient(ctx, projectID) | |
if err != nil { | |
log.Fatalf("Failed to create client: %v", err) | |
} | |
// Sets the name for the new topic. | |
topicName := "topic" | |
/* | |
// Creates the new topic. | |
topic, err := client.CreateTopic(ctx, topicName) | |
if err != nil { | |
log.Fatalf("Failed to create topic: %v", err) | |
} | |
fmt.Printf("Topic %v created.\n", topic)*/ | |
t := client.Topic(topicName) | |
msg := "This is the message from go" | |
result := t.Publish(ctx, &pubsub.Message{ | |
Data: []byte(msg), | |
}) | |
id, err := result.Get(ctx) | |
if err != nil { | |
log.Fatalf("Failed to create topic: %v", err) | |
} | |
fmt.Printf("Published a message; msg ID: %v\n", id) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'vendor/autoload.php'; | |
use Google\Cloud\PubSub\PubSubClient; | |
function publish_message ($projectId, $topicName, $message) { | |
$pubsub = new PubSubClient([ | |
'projectId' => $projectId, | |
]); | |
$topic = $pubsub->topic($topicName); | |
$topic->publish([ 'data' => $message ]); | |
print('Message published' . PHP_EOL); | |
} | |
$projectId = 'project-id'; | |
$topic = 'topic'; | |
$message = 'This is the message from php'; | |
publish_message($projectId, $topic, $message); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"sync" | |
"golang.org/x/net/context" | |
"cloud.google.com/go/pubsub" | |
"log" | |
) | |
func main() { | |
var mu sync.Mutex | |
received := 0 | |
projectID := "project-id" | |
ctx := context.Background() | |
client, err := pubsub.NewClient(ctx, projectID) | |
subName := "subscription" | |
if err != nil { | |
log.Fatalf("Failed to create client: %v", err) | |
} | |
sub := client.Subscription(subName) | |
cctx, cancel := context.WithCancel(ctx) | |
err = sub.Receive(cctx, func(ctx context.Context, msg *pubsub.Message) { | |
msg.Ack() | |
fmt.Printf("Got message: %q\n", string(msg.Data)) | |
mu.Lock() | |
defer mu.Unlock() | |
received++ | |
if received == 10 { | |
cancel() | |
} | |
}) | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once 'vendor/autoload.php'; | |
use Google\Cloud\PubSub\PubSubClient; | |
function pull_messages ($projectId, $subscriptionName) { | |
$pubsub = new PubSubClient([ | |
'projectId' => $projectId, | |
]); | |
$subscription = $pubsub->subscription($subscriptionName); | |
foreach ($subscription->pull() as $message) { | |
printf('Message: %s' . PHP_EOL, $message->data()); | |
// Acknowledge the Pub/Sub message has been received, so it will not be pulled multiple times. | |
$subscription->acknowledge($message); | |
} | |
} | |
$projectId = 'project-id'; | |
$subscription = "subscription"; | |
pull_messages($projectId, $subscription); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment