Can't do GET request for pull subscription from local fake PubSub Server - get

TL;DR: What is the correct way to send a GET Request to do a pull subscription from a Pub/Sub server. What is the correct URL to use?
I am running a local Google Pub/Sub fake using gcloud beta emulators pubsub start, I have been able to successfully publish to it using a ruby script I wrote, however I have been unable to do a pull subscription.
I am aiming to accomplish this just using a GET request, not a script. One of the issues I've found is there's tons of documentation on doing pull subscriptions with a client or gcloud, but very little on how to access the server by URL. Perhaps I am misunderstanding what is possible - but I want to publish a message to pub/sub using a ruby client, and then use Postman to do a GET request to the pub/sub server to retrieve the message.
I am fairly certain the issue is with how I am making a get request, but I have reproduced everything else below for context
Ruby Publishing Code
require "google/cloud/pubsub"
require 'json'
class Publisher
def publish(event)
puts "1==============>>>>>>>> publishing..."
pubsub = Google::Cloud::PubSub.new(
project_id: "grpc-demo-proj",
emulator_host: "localhost:8085"
)
topic_id = "event_topic"
topic = pubsub.topic topic_id
begin
topic.publish_async "receive_event#event",
event: JSON.generate(event) do |result|
raise "Failed to publish the message." unless result.succeeded?
puts "2==============>>>>>>>> Message published asynchronously."
end
# Stop the async_publisher to send all queued messages immediately.
topic.async_publisher.stop.wait!
rescue StandardError => e
puts "3==============>>>>>>>> Received error while publishing: #{e.message}"
end
end
end
This seems to work, as I get
1==============>>>>>>>> publishing...
DEBUG GRPC : calling localhost:8085:/google.pubsub.v1.Publisher/GetTopic
DEBUG GRPC : calling localhost:8085:/google.pubsub.v1.Publisher/Publish
2==============>>>>>>>> Message published asynchronously.
In my terminal.
I also have the Pub/Sub server running using the following shell scripts.
#!/bin/bash
# Kill the existing process if it's already running
if [ "$(lsof -i:8085)" ]; then
kill $(lsof -t -i:8085)
fi
# Kick off the new process
gcloud beta emulators pubsub start --project=grpc-demo-proj
# Connect to environment variables
$(gcloud beta emulators pubsub env-init)
PubSub Setup Script
#!/bin/bash
# Wait for the pubsub emulator to boot up
sleep 7
while [[ ! "$(lsof -i:8085)" ]]
do
echo '#===> PUBSUB EMULATOR SETUP: Waiting for PubSub Emulator to start...'
sleep 3
done
# Create topics
curl --header "Content-Type: application/json" \
--request PUT \
http://localhost:8085/v1/projects/grpc-demo-proj/topics/event_topic
# Create test subscriptions for each topic
curl --header "Content-Type: application/json" \
--request PUT \
--data '{"topic": "projects/grpc-demo-proj/topics/event_topic"}' \
http://localhost:8085/v1/projects/grpc-demo-proj/subscriptions/event_topic.test_sub1
Again. These seem to work well.
Where I have trouble...
is doing a pull subscription from the pub/sub server using a GET request (Either from PostMan, or just in browser's URL bar)
http://localhost:8085/v1/projects/grpc-demo-proj/subscriptions/event_topic.test_sub1:pull
Returns
{
"error": {
"code": 400,
"message": "Invalid [subscriptions] name: (name=projects/grpc-demo-proj/subscriptions/event_topic.test_sub1:pull)",
"status": "INVALID_ARGUMENT"
}
}
But the subscription name is valid, as
http://localhost:8085/v1/projects/grpc-demo-proj/subscriptions/event_topic.test_sub1
returns
{
"name": "projects/grpc-demo-proj/subscriptions/event_topic.test_sub1",
"topic": "projects/grpc-demo-proj/topics/event_topic",
"pushConfig": {},
"ackDeadlineSeconds": 10,
"messageRetentionDuration": "604800s"
}
Which would seem to confirm the server is working, and the topics and subscriptions have been successfully created.
Although -NOT- the solution I am looking for, I tried using gcloud in command line:
bgc#jadzia:~$ gcloud beta pubsub subscriptions pull test_sub1
ERROR: (gcloud.beta.pubsub.subscriptions.pull) NOT_FOUND: Resource not found (resource=test_sub1).
Even though other sources seem to confirm this subscription does exist.
While it could possibly be an issue with Ruby incorrectly saying it successfully published the message, or something is wrong with the server. I suspect I'm just not doing the GET request correctly. I've tried several variations on the above GET request but won't list them all here.
So, without using a script - how can I get a message back from the pub/sub server? Ideally a URL for a GET request I can plug into PostMan, but command-line based solutions may also work here.

I did reproduce your local fake Pub/sub server using the all the scripts you posted. As you commented I used POST instead of GET and got a response. Pub/Sub subscriptions pull reference
POST https://pubsub.googleapis.com/v1/{subscription}:pull
POST request for subscriptions pull:
curl --header "Content-Type: application/json" \
--request POST \
--data "{
"maxMessages": "1"
}" \
http://localhost:8085/v1/projects/my-project/subscriptions/event_topic.test_sub1:pull
Output of subscriptions pull:
Pubsub Message is encoded in base 64. Take note that I ran everything (creation of pubsub server, topics, subscriber, publish of message, pulling of message) in the Google cloud shell.
EDIT 1:
As Brian mentioned, this is the request that is working for him. This works on my testing as well!
curl --header "Content-Type: application/json" \
--request POST \
localhost:8085/v1/projects/my-prject/subscriptions/event_topic.test_sub1:pull?maxMessages=5
Output:

Related

Getting {"error": "unathorized"} every time, when trying to access any route in API on back4app

I have managed to deploy the appication with back4app as well as got it web-hosted there. However, I can't access the routes of my API (BookReadingApp). Every time I try to access any route I get an error: {"error": "unathorized"}
403 error persists no matter what type of request I'm trying to send to the server (get, post, put, patch, delete). The same thing applies to every error I get when trying to send the wrong request to the routes that don't exist (for testing purposes only).
Console logs show that "Database was connected successfully" and the server was launched with no errors (as I pointed out in my app.js file to know that api works as expected).
Please, help me to resolve this issue - any help is highly appreciated.
Kind Regards
Vladyslav
I tried testing API (with POSTMAN and in Chrome) intentionally sending the wrong requests - the error does not change: it's always 'unathorized' (403). The console of the application shows that the server started successfully and that DB was also connected.
unauthorized always said when something internally failed or when you use the cloud functions and didn't specify the keys in the header:
curl -X POST \
-H "X-Parse-Application-Id: key_here" \
-H "X-Parse-REST-API-Key: key_here" \
-H "Content-Type: application/json" \
-d "{}" \
https://parseapi.back4app.com/functions/hello

Schedule completed event is not supported by stripe

I am using stripe schedules to handle the downgrade of the subscriptions. I need to test it, however, the events subscription_schedule.completed is not supported by stripe CLI(Even I upgraded to latest verison).
Theres no way on stripe dashboard to compelte the schedule before time.I need to do handle stuff on my backend after this event is triggered, what should I do?
subscription_schedule.completed events are sent when a Subscription Schedule transitions to having a status of completed. This happens when the Schedule has end_behavior: cancel and was allowed to end naturally by completing its last phase (not through an API call to cancel it).
You can trigger subscription_schedule.completed events yourself by updating or creating a new Subscription Schedule that ends a few seconds in the future and has end_behavior: cancel. Here's an example of how you would do this using curl (replacing FUTURE_TIMESTAMP with one that is a few seconds in the future, and using your own keys and object IDs):
curl https://api.stripe.com/v1/subscription_schedules \
-u sk_test_XXX: \
-d customer=cus_XXX \
-d start_date=now \
-d end_behavior=cancel \
-d "phases[0][items][0][price]"=price_XXX \
-d "phases[0][items][0][quantity]"=1 \
-d "phases[0][end_date]"={{FUTURE_TIMESTAMP}}

Send MQTT message from the android client to all the clients through MQTT broker

I need MQTT broker to publish the received MQTT message from the Android client to all other clients so added mosquitto pub command in the body of the message.
publish(client,"mosquitto_pub -h 192.34.63.138 -t fromApp -m "Turn" -d ");
It is giving error that "Can't resolve symbol "Turn" and ; or ) expected" .
Update
I understood it correctly later. I actually needed an MQTT message from the android client to be sent to all other clients so I thought to include publish keyword in the message body which was quite wrong. The MQTT itself sends the received messages to all the clients provided if the clients have subscribed to that topic.Hopefully, it will help other readers.
There are a number of problems with your approach.
First, the compile time error is because you have nested " within a String (which is bound by ". To do this you need to escape the "'s with the \ as follows:
"mosquitto_pub -h 192.34.63.138 -t fromApp -m \"Turn\" -d "
The second problem is the more important one. MQTT doesn't work the way you seem to be expecting it to.
You do not send commands to the broker for it to execute, you publish a message from one client to a topic, and the broker then delivers that message to all the clients that have subscribed to that topic. So in this case you would just publish a message with the payload Turn to the topic fromApp. Which is going to look something like:
MqttMessage message = new MqttMessage("Turn".getBytes());
sampleClient.publish("fromApp", message);

How to send a single message to multi connections using UE?

I have tried to send messages to various connections via unification engine and it works fine. It's mentioned here (https://developer.unificationengine.com/) that its possible to send only one message to unification engine mentioning various connections and it sends it to respective social media apps. However, I am not able to find a way to get it to work. Please advise if anyone has tried.
Appreciate your help!
Thanks & Best Regards,
Anand Patil
It is possible to send same message to multiple connections via unification engine.
The following command will send the same messsage to two facebook connections and a twitter connection
curl -XPOST https://apiv2.unificationengine.com/v2/message/send --data "{ \"message\": {
\"receivers\": [
{\"name\": \"Me\", \"address\": \"\" , \"Connector\": \"UNIQUE_CONNECTION_IDENTIFIER_FACEBOOK_1\"},
{\"name\": \"Me\", \"address\": \"\" , \"Connector\": \"UNIQUE_CONNECTION_IDENTIFIER_FACEBOOK_2\"},
{\"name\": \"name\", \"address\": \"TWITTER_HANDLE\" , \"Connector\": \"UNIQUE_CONNECTION_IDENTIFIER_TWITTER_1\"}
],
\"parts\":
[{\"id\": \"1\",\"contentType\": \"text/plain\", \"data\":\"MESSAGE_BODY\" ,\"size\": MESSAGE_BODY_SIZE,\"type\": \"body\",\"sort\":0}]
}}" -u USER_ACCESSKEY:USER_ACCESSSECRET -k

How to send a push notification in unificationengine

How to send a push notification to a channel in unificationengine?
If a user is already created the channel via UE.
Example for pushing messages to UE
curl -XPOST https://apiv2.unificationengine.com/v2/push/notification -u CONNECTOR_KEY:CONNECTOR_SECRET –data “{ \"channel\”: \“6d2d2111-d3b6-4ac9-8e67-55980a141fdd_sms\”, \“data\”: \“test message\”}“ -k
The documentation for push messages is given in Connector section in https://developer.unificationengine.com.

Resources