Status Update
Comments
ya...@google.com <ya...@google.com> #2
gy...@gmail.com <gy...@gmail.com> #3
[Deleted User] <[Deleted User]> #4
mi...@mend.com <mi...@mend.com> #5
li...@google.com <li...@google.com> #6
To achieve the equivalent behavior though, you can send gcloud pubsub requests to the running emulator by modifying the pubsub api_endpoint by running:
gcloud config set api_endpoint_overrides/pubsub
Note:
- This hostname is currently the default value of running `gcloud beta emulators pubsub start`
- You must use http as https doesn't work with the emulator.
As I mentioned earlier, the pubsub emulator doesn't match up with the actual pubsub service. One thing is that create requests don't work through gcloud when using the emulator. To get around this, either:
1. Use the client libraries
2. `curl -H "Content-Type: application/json" -X PUT -d '{}'
mb...@gmail.com <mb...@gmail.com> #7
Also currently the pubsub emulator doesn't match up with the real API 100% either (e.g. Using gcloud to send topics create request to the emulator fails) which is a more concrete reason to block this.
Thanks for the helpful detail and reasonable explanation for blocking this PUBSUB_EMULATOR_HOST env var feature request—makes sense.
As I mentioned earlier, the pubsub emulator doesn't match up with the actual pubsub service. One thing is that create requests don't work through gcloud when using the emulator. To get around this, either:
- [...]
curl -H "Content-Type: application/json" -X PUT -d '{}' http://localhost:8085/v1/projects/my-project/topics/my-topic
I can't get your curl example working and I have a hunch it's because my message schema is wrong. I tried a couple things:
-
Create message schema based on
https://cloud.google.com/pubsub/docs/reference/rest/v1/projects.topics/publish reference—doesn't work.% curl --header 'Content-Type: application/json' --request PUT --data '{"messag es":[{"data":"hello world"}]}' http://localhost:8085/v1/projects/myproject/topic s/mytopic {"error":{"code":400,"message":"Payload isn't valid for request.","status":"INVALID_ARGUMENT"}}
-
Another try: Encode data field based on
https://cloud.google.com/pubsub/docs/reference/rest/v1/PubsubMessage#:~:text=A%20base64%2Dencoded%20string . example—also doesn't work.% echo hello world | base64 aGVsbG8gd29ybGQK % curl --header 'Content-Type: application/json' --request PUT --data '{"messag es":[{"data":"aGVsbG8gd29ybGQK"}]}' http://localhost:8085/v1/projects/myproject/ topics/mytopic {"error":{"code":400,"message":"Payload isn't valid for request.","status":"INVALID_ARGUMENT"}}
Notice that the payload isn't valid for both requests.
Please expand your curl example to include a sample message, thank you!
Aside: For reference, this is my local Pub/Sub emulator setup:
-
Run Pub/Sub emulator in Docker container.
docker run --rm --name=pubsub --interactive --tty --publish=8085:8085 us.gcr.io/google.com/cloudsdktool/google-cloud-cli gcloud beta emulators pubsub start --host-port=0.0.0.0:8085 --project=myproject
-
Create topic and subscription and wait for messages.
git clone git@github.com:googleapis/python-pubsub.git cd python-pubsub/samples/snippets python3 -m venv venv source venv/bin/activate pip install -r requirements.txt PUBSUB_EMULATOR_HOST=localhost:8085 python publisher.py myproject create mytopic PUBSUB_EMULATOR_HOST=localhost:8085 python subscriber.py myproject create mytopic mysubscription PUBSUB_EMULATOR_HOST=localhost:8085 python publisher.py myproject publish mytopic PUBSUB_EMULATOR_HOST=localhost:8085 python subscriber.py myproject receive mysubscription # read earlier messages, then wait for more messages (TODO: Post a message with a curl request)
sh...@herobullion.com <sh...@herobullion.com> #8
Responding to last commenter about the failure to be able to publish a message using the rest api (curl), after poking around a bit it seems like you two things wrong
- The URL/path should have ":publish" at the end like this:
http://localhost:8085/v1/projects/myproject/topics/mytopic:publish
- The HTTP method should be POST instead of PUT
curl --header 'Content-Type: application/json' --request POST --data '{"messages":[{"data":"aGVsbG8gd29ybGQK"}]}' http://localhost:8085/v1/projects/my-project/topics/my-topic:publish
{
"messageIds": ["6"]
}
The rest api docs are thorough but some things don't seem to be obvious.
Responding to the google employee that suggested using the gcloud config set api_endpoint_overrides/pubsub http://localhost:8085/
command, I'm still not able to get that to work for me in 2025. When I run gcloud pubsub topics list
after doing so I get no topics listed, even when I can see there are topics using curl --header 'Content-Type: application/json' --request GET --data '{}' http://localhost:8085/v1/projects/my-project/topics
. I also saw some CLOUDSDK_API_ENDPOINT_OVERRIDES_PUBSUB
env var
Bummer because it would be convenient if even some of the gcloud CLI functionality worked with the pubsub emulator since the docs push using the gcloud CLI so hard. I'm very thankful you pointed out the underlying REST api usage in your comment though, so thanks. Double bummer the docs don't even give a whiff of the underlying REST api, not even on the page that documents that emulator.
Last thing, should this ticket be closed?
Description
I tried to use the following command to test my code locally through the pubsub emulator :
gcloud beta pubsub topics publish "COMMAND.SELLER.CREATE" "Hello world"
But gcloud doesn't seem to send the message to the emulator but instead to the real pubsub server on google cloud.
You can see the use case here :
hope this feature request will be accepted.