AppFabric Topic Subscription - appfabric

I am trying to assemble a simple AppFabric Topic whereby messages are sent and received using the SessionId. The code does not abort, but brokeredMessage is always null. Here is the code:
// BTW, the topic already exists
var messagingFactory = MessagingFactory.Create(uri, credentials);
var topicClient = messagingFactory.CreateTopicClient(topicName);
var sender = topicClient.CreateSender();
var message = BrokeredMessage.CreateMessage("Top of the day!");
message.SessionId = "1";
sender.Send(message);
var subscription = topic.AddSubscription("1", new SubscriptionDescription { RequiresSession = true});
var mikeSubscriptionClient = messagingFactory.CreateSubscriptionClient(subscription);
var receiver = mikeSubscriptionClient.AcceptSessionReceiver("1");
BrokeredMessage brokeredMessage;
receiver.TryReceive(TimeSpan.FromMinutes(1), out brokeredMessage); // brokeredMessage always null

You have two problems in your code:
You create a subscription AFTER you send the message. You need to create a subscription before sending, because a subscription tells the topic to, in a sense, copy, the message to several different "buckets".
You are using TryReceive but are not checking for its result. It returns true, if a message was received, and false if not (e.g. Timeout has occured).
I am writing my sample application and will post it on our blog today. I will post the link here as well. But until then, move the subscription logic to before sending the message, and the receiver after it and you will start seeing results.
Update:
As promised, here is the link to my blog post on getting started with AppFabric Queues, Topics, Subscriptions.

Related

AWS FIFO SQS queue message is disappearing when I repost the same message even after successfully deleting it

I am facing a strange issue in SQS. Let me simplify my use-case, I have 7 messages in the FIFO queue and my standalone app should keep-on polling the messages in the same sequence for my business case infinitely. For instance, my app read message1 and after some business processing, the app will delete it and repost the same message into the same queue(tail of the queue), and these steps will be continued for the next set of messages endlessly. Here, my expectation is my app will be polling the message continuously and doing the operations based on the messages in the queue in the same sequence, but that's where the problem arises. When the message is read from the queue for the very first time, delete it, and repost the same message into the same queue, even after the successful sendMessageResult, the reposted message is not present in the queue.
I have included the below code to simulate the issue, briefly, Test_Queue.fifo queue with Test_Queue_DLQ.fifo configured as reDrivePolicy is created. At the very first time after creating the queue, the message is posted -> "Test_Message" into Test_Queue.fifo queue(Getting the MessageId in response ) and long-polling the queue to read the message, and after iterating the ReceiveMessageResult#getMessages, deleting the message(Getting MessageId in response). Again, after the successful deletion of the message, the same message is reposted into the tail of the same queue(Getting the MessageId in response). But, the reposted message is not present in the queue. When, I checked the AWS admin console the message count is 0 in the Messages available and Messages in flight sections and the reposted message is not even present in Test_Queue_DLQ.fifo queue. As per the SQS docs, if we delete the message, even if it is present in flight mode should be removed, so reposting the same message should not be an issue. I suspect on SQS side, where they are performing some equals comparison and skipping the same message during in visibleTimeOut interval to avoid deduplication of the same message in the distributed environment, but couldn't get any clear picture.
Code snippet to simulate the above issue
public class SQSIssue {
#Test
void sqsMessageAbsenceIssueTest() {
AmazonSQS amazonSQS = AmazonSQSClientBuilder.standard().withEndpointConfiguration(new AwsClientBuilder
.EndpointConfiguration("https://sqs.us-east-2.amazonaws.com", "us-east-2"))
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(
"<accessKey>", "<secretKey>"))).build();
//create queue
String queueUrl = createQueues(amazonSQS);
String message = "Test_Message";
String groupId = "Group1";
//Sending message -> "Test_Message"
sendMessage(amazonSQS, queueUrl, message, groupId);
//Reading the message and deleting using message.getReceiptHandle()
readAndDeleteMessage(amazonSQS, queueUrl);
//Reposting the same message into the queue -> "Test_Message"
sendMessage(amazonSQS, queueUrl, message, groupId);
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest()
.withQueueUrl(queueUrl)
.withWaitTimeSeconds(5)
.withMessageAttributeNames("All")
.withVisibilityTimeout(30)
.withMaxNumberOfMessages(10);
ReceiveMessageResult receiveMessageResult = amazonSQS.receiveMessage(receiveMessageRequest);
//Here I am expecting the message presence in the queue as I recently reposted the same message into the same queue after the message deletion
Assertions.assertFalse(receiveMessageResult.getMessages().isEmpty());
}
private void readAndDeleteMessage(AmazonSQS amazonSQS, String queueUrl) {
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest()
.withQueueUrl(queueUrl)
.withWaitTimeSeconds(5)
.withMessageAttributeNames("All")
.withVisibilityTimeout(30)
.withMaxNumberOfMessages(10);
ReceiveMessageResult receiveMessageResult = amazonSQS.receiveMessage(receiveMessageRequest);
receiveMessageResult.getMessages().forEach(message -> amazonSQS.deleteMessage(queueUrl, message.getReceiptHandle()));
}
private String createQueues(AmazonSQS amazonSQS) {
String queueName = "Test_Queue.fifo";
String deadLetterQueueName = "Test_Queue_DLQ.fifo";
//Creating DeadLetterQueue
CreateQueueRequest createDeadLetterQueueRequest = new CreateQueueRequest()
.addAttributesEntry("FifoQueue", "true")
.addAttributesEntry("ContentBasedDeduplication", "true")
.addAttributesEntry("VisibilityTimeout", "600")
.addAttributesEntry("MessageRetentionPeriod", "262144");
createDeadLetterQueueRequest.withQueueName(deadLetterQueueName);
CreateQueueResult createDeadLetterQueueResult = amazonSQS.createQueue(createDeadLetterQueueRequest);
GetQueueAttributesResult getQueueAttributesResult = amazonSQS.getQueueAttributes(
new GetQueueAttributesRequest(createDeadLetterQueueResult.getQueueUrl())
.withAttributeNames("QueueArn"));
String deadLetterQueueArn = getQueueAttributesResult.getAttributes().get("QueueArn");
//Creating Actual Queue with DeadLetterQueue configured
CreateQueueRequest createQueueRequest = new CreateQueueRequest()
.addAttributesEntry("FifoQueue", "true")
.addAttributesEntry("ContentBasedDeduplication", "true")
.addAttributesEntry("VisibilityTimeout", "600")
.addAttributesEntry("MessageRetentionPeriod", "262144");
createQueueRequest.withQueueName(queueName);
String reDrivePolicy = "{\"maxReceiveCount\":\"5\", \"deadLetterTargetArn\":\""
+ deadLetterQueueArn + "\"}";
createQueueRequest.addAttributesEntry("RedrivePolicy", reDrivePolicy);
CreateQueueResult createQueueResult = amazonSQS.createQueue(createQueueRequest);
return createQueueResult.getQueueUrl();
}
private void sendMessage(AmazonSQS amazonSQS, String queueUrl, String message, String groupId) {
SendMessageRequest sendMessageRequest = new SendMessageRequest()
.withQueueUrl(queueUrl)
.withMessageBody(message)
.withMessageGroupId(groupId);
SendMessageResult sendMessageResult = amazonSQS.sendMessage(sendMessageRequest);
Assertions.assertNotNull(sendMessageResult.getMessageId());
}
}
From Using the Amazon SQS message deduplication ID:
The message deduplication ID is the token used for deduplication of sent messages. If a message with a particular message deduplication ID is sent successfully, any messages sent with the same message deduplication ID are accepted successfully but aren't delivered during the 5-minute deduplication interval.
Therefore, you should supply a different Deduplication ID each time the message is placed back onto the queue.
https://stackoverflow.com/a/65844632/3303074 is fitting, I should have added the SendMessageRequest#withMessageDeduplicationId, but I would like to add few more points to the answer, The technical reason behind the message disappearance is because I have enabled the ContentBasedDeduplication for the queue. Amazon SQS uses an SHA-256 hash to generate the MessageDeduplicationId using the body of the message (but not the attributes of the message) if the MessageDeduplicationId has not been mentioned explicitly when sending the message. When ContentBasedDeduplication is in effect, messages with identical content sent within the deduplication interval are treated as duplicates and only one copy of the message is delivered. So even we add different attributes for the same message repost into the same queue will not work as expected. Adding MessageDeduplicationId helps to solve the issue because even the queue has ContentBasedDeduplication set, the explicit MessageDeduplicationId overrides the generated one.
Code Snippet
SendMessageRequest sendMessageRequest = new SendMessageRequest()
.withQueueUrl(queueUrl)
.withMessageBody(message)
.withMessageGroupId(groupId)
// Adding explicit MessageDeduplicationId
.withMessageDeduplicationId(UUID.randomUUID().toString());
SendMessageResult sendMessageResult = amazonSQS.sendMessage(sendMessageRequest);

Reply a message after receiving data on subscriber in Google PubSub Pull

Is it possible to reply a message once you received a data from Publisher.
It must be a direct reply, once the Publisher published a message.
I'm using Google PubSub Service.
https://cloud.google.com/pubsub/docs/pull
Publisher/Sender (PHP):
$sendToOps =[];
$sendToOps['MESSAGE'] = "my message";
$topicName = env('GOOGLE_CLOUD_TO_OPS_TOPIC');
$pubSub = new PubSubClient();
$topic = $pubSub->topic($topicName);
$ret = $topic->publish([
'attributes'=>$sendToOps
]);
//**********The word "Apple" must output here**********
echo $ret;
//*****************************************************
Subscriber/Receiver (Javascript):
'use strict';
//Get .env File Data
require('dotenv').config({path: '/usr/share/nginx/html/myProject/.env'});
var request = require('request');
var port = process.env.PORT_GATEWAY;
var host = process.env.IP_PUSH;
var test = process.env.TEST_FLAG;
var pubsubSubscription = process.env.GOOGLE_CLOUD_TO_OPS_SUBSCRIPTION;
const keyFilePath= 'public/key/key.json';
// Imports the Google Cloud client library
const {PubSub} = require('#google-cloud/pubsub');
// Creates a client; cache this for further use
const pubSubClient = new PubSub({
keyFilename: keyFilePath
});
function listenForMessages() {
// References an existing subscription
const subscription = pubSubClient.subscription(pubsubSubscription);
// Create an event handler to handle messages
const messageHandler = message => {
console.log(message.attributes);
//*****************************************************
//I want to reply to Sender with the word "Apple" here
//*****************************************************
message.ack()
};
subscription.on('message', messageHandler);
}
listenForMessages();
Is it possible to reply a message once you received a data from
Publisher.
Depends on what you mean by "reply". The publisher of a message posts a message on a Pub/Sub Topic. Subscribers receive messages from a Pub/Sub Subscription. There is no two-way communications channel here. There is no Pub/Sub reply back method.
A subscriber could publish a message to a different topic that the publisher reads as a subscriber. Both sides would be publisher and a subscriber but on different topics.
Once a message is received, a subscriber could directly call an API on the publisher.
However, the intent of Publish/Subscribe is to decouple senders from receivers and not to lock them together.

Facebook Webhooks. How to create an event that will work on like and unlike?

How to create an event that will work on like and unlike?,
when I create a webhook
object = page callback_url = ”example.com" fields = feed verify_token =
hub_verify_token
verification callback_url passes and the answer is tapped
{ “success”: true }
But after like or unlike on callback_url nothing is sent.
The events that I have used before :
edge.create and edge.remove.
http://i.imgur.com/xK5C4IK.png
Help please!
There is a solution to the problem:
Details here: https://developers.facebook.com/docs/graph-api/reference/page/subscribed_apps/
Code example in the JavaScript SDK:
/* make the API call */
FB.api(
"/{page-id}/subscribed_apps",
'POST',
{
"object":"page",
"callback_url":"https://example.com/callback",
"fields":"likes",
"verify_token":"my_token_code",
"access_token" : "you_access_token"
}
function (response) {
if (response && !response.error) {
/* handle the result */
}
}
);
but unlike for the page does not work (((
what do you think about it?
There is a list of all webhook fields in the docs: https://developers.facebook.com/docs/graph-api/webhooks/reference/page/
There is no specific webhook about likes/unlikes. According to this article, it should be possible though: https://developers.facebook.com/blog/post/2017/11/07/changes-developer-offerings/
The feed hook may only deliver likes to posts:
Describes nearly all changes to a page's feed, such as posts, shares,
likes, etc. The values received depend on the types of changes made to
the page's feed.

Identity Server 3 Facebook Login Get Email

Identity server is implemented and working well. Google login is working and is returning several claims including email.
Facebook login is working, and my app is live and requests email permissions when a new user logs in.
The problem is that I can't get the email back from the oauth endpoint and I can't seem to find the access_token to manually request user information. All I have is a "code" returned from the facebook login endpoint.
Here's the IdentityServer setup.
var fb = new FacebookAuthenticationOptions
{
AuthenticationType = "Facebook",
SignInAsAuthenticationType = signInAsType,
AppId = ConfigurationManager.AppSettings["Facebook:AppId"],
AppSecret = ConfigurationManager.AppSettings["Facebook:AppSecret"]
};
fb.Scope.Add("email");
app.UseFacebookAuthentication(fb);
Then of course I've customized the AuthenticateLocalAsync method, but the claims I'm receiving only include name. No email claim.
Digging through the source code for identity server, I realized that there are some claims things happening to transform facebook claims, so I extended that class to debug into it and see if it was stripping out any claims, which it's not.
I also watched the http calls with fiddler, and I only see the following (apologies as code formatting doesn't work very good on urls. I tried to format the querystring params one their own lines but it didn't take)
(facebook.com)
/dialog/oauth
?response_type=code
&client_id=xxx
&redirect_uri=https%3A%2F%2Fidentity.[site].com%2Fid%2Fsignin-facebook
&scope=email
&state=xxx
(facebook.com)
/login.php
?skip_api_login=1
&api_key=xxx
&signed_next=1
&next=https%3A%2F%2Fwww.facebook.com%2Fv2.7%2Fdialog%2Foauth%3Fredirect_uri%3Dhttps%253A%252F%252Fidentity.[site].com%252Fid%252Fsignin-facebook%26state%3Dxxx%26scope%3Demail%26response_type%3Dcode%26client_id%3Dxxx%26ret%3Dlogin%26logger_id%3Dxxx&cancel_url=https%3A%2F%2Fidentity.[site].com%2Fid%2Fsignin-facebook%3Ferror%3Daccess_denied%26error_code%3D200%26error_description%3DPermissions%2Berror%26error_reason%3Duser_denied%26state%3Dxxx%23_%3D_
&display=page
&locale=en_US
&logger_id=xxx
(facebook.com)
POST /cookie/consent/?pv=1&dpr=1 HTTP/1.1
(facebook.com)
/login.php
?login_attempt=1
&next=https%3A%2F%2Fwww.facebook.com%2Fv2.7%2Fdialog%2Foauth%3Fredirect_uri%3Dhttps%253A%252F%252Fidentity.[site].com%252Fid%252Fsignin-facebook%26state%3Dxxx%26scope%3Demail%26response_type%3Dcode%26client_id%3Dxxx%26ret%3Dlogin%26logger_id%3Dxxx
&lwv=100
(facebook.com)
/v2.7/dialog/oauth
?redirect_uri=https%3A%2F%2Fidentity.[site].com%2Fid%2Fsignin-facebook
&state=xxx
&scope=email
&response_type=code
&client_id=xxx
&ret=login
&logger_id=xxx
&hash=xxx
(identity server)
/id/signin-facebook
?code=xxx
&state=xxx
I saw the code parameter on that last call and thought that maybe I could use the code there to get the access_token from the facebook API https://developers.facebook.com/docs/facebook-login/manually-build-a-login-flow
However when I tried that I get a message from the API telling me the code has already been used.
I also tried to change the UserInformationEndpoint to the FacebookAuthenticationOptions to force it to ask for the email by appending ?fields=email to the end of the default endpoint location, but that causes identity server to spit out the error "There was an error logging into the external provider. The error message is: access_denied".
I might be able to fix this all if I can change the middleware to send the request with response_type=id_token but I can't figure out how to do that or how to extract that access token when it gets returned in the first place to be able to use the Facebook C# sdk.
So I guess any help or direction at all would be awesome. I've spent countless hours researching and trying to solve the problem. All I need to do is get the email address of the logged-in user via IdentityServer3. Doesn't sound so hard and yet I'm stuck.
I finally figured this out. The answer has something to do with Mitra's comments although neither of those answers quite seemed to fit the bill, so I'm putting another one here. First, you need to request the access_token, not code (authorization code) from Facebook's Authentication endpoint. To do that, set it up like this
var fb = new FacebookAuthenticationOptions
{
AuthenticationType = "Facebook",
SignInAsAuthenticationType = signInAsType,
AppId = ConfigurationManager.AppSettings["Facebook:AppId"],
AppSecret = ConfigurationManager.AppSettings["Facebook:AppSecret"],
Provider = new FacebookAuthenticationProvider()
{
OnAuthenticated = (context) =>
{
context.Identity.AddClaim(new System.Security.Claims.Claim("urn:facebook:access_token", context.AccessToken, ClaimValueTypes.String, "Facebook"));
return Task.FromResult(0);
}
}
};
fb.Scope.Add("email");
app.UseFacebookAuthentication(fb);
Then, you need to catch the response once it's logged in. I'm using the following file from the IdentityServer3 Samples Repository, which overrides (read, provides functionality) for the methods necessary to log a user in from external sites. From this response, I'm using the C# Facebook SDK with the newly returned access_token claim in the ExternalAuthenticationContext to request the fields I need and add them to the list of claims. Then I can use that information to create/log in the user.
public override async Task AuthenticateExternalAsync(ExternalAuthenticationContext ctx)
{
var externalUser = ctx.ExternalIdentity;
var claimsList = ctx.ExternalIdentity.Claims.ToList();
if (externalUser.Provider == "Facebook")
{
var extraClaims = GetAdditionalFacebookClaims(externalUser.Claims.First(claim => claim.Type == "urn:facebook:access_token"));
claimsList.Add(new Claim("email", extraClaims.First(k => k.Key == "email").Value.ToString()));
claimsList.Add(new Claim("given_name", extraClaims.First(k => k.Key == "first_name").Value.ToString()));
claimsList.Add(new Claim("family_name", extraClaims.First(k => k.Key == "last_name").Value.ToString()));
}
if (externalUser == null)
{
throw new ArgumentNullException("externalUser");
}
var user = await userManager.FindAsync(new Microsoft.AspNet.Identity.UserLoginInfo(externalUser.Provider, externalUser.ProviderId));
if (user == null)
{
ctx.AuthenticateResult = await ProcessNewExternalAccountAsync(externalUser.Provider, externalUser.ProviderId, claimsList);
}
else
{
ctx.AuthenticateResult = await ProcessExistingExternalAccountAsync(user.Id, externalUser.Provider, externalUser.ProviderId, claimsList);
}
}
And that's it! If you have any suggestions for simplifying this process, please let me know. I was going to modify this code to do perform the call to the API from FacebookAuthenticationOptions, but the Events property no longer exists apparently.
Edit: the GetAdditionalFacebookClaims method is simply a method that creates a new FacebookClient given the access token that was pulled out and queries the Facebook API for the other user claims you need. For example, my method looks like this:
protected static JsonObject GetAdditionalFacebookClaims(Claim accessToken)
{
var fb = new FacebookClient(accessToken.Value);
return fb.Get("me", new {fields = new[] {"email", "first_name", "last_name"}}) as JsonObject;
}

Sitecore EXM 3.2(ECM) Assign goal to the triggered message

I need to do a simple newsletter form. This form should work like this:
User inputs an email and presses to the submit button
User recieves message on email with confirm link
After user clicks on the link his email is added to Recipient list
This form should be work with help EXM
I've created Triggered message in the EXM with link for subscription.
And I wrote this code for the Submit button for trigger the Newsletter Goal
[HttpPost]
public ActionResult NewsletterSubscribe(NewsletterViewBag model)
{
var goal = Context.Database.GetItem(newsletterGoal);
if (goal == null)
{
continue;
}
var registerGoal = new Sitecore.Analytics.Data.Items.PageEventItem(goal);
var eventData = Tracker.Current.CurrentPage.Register(registerGoal);
eventData.Data = goal[DateTime.Now.ToString(CultureInfo.InvariantCulture)];
Tracker.Submit();
}
How I can assign my triggered message to the newsletterGoal?
Also I try manually send message this way:
MessageItem message = Sitecore.Modules.EmailCampaign.Factory.GetMessage(new ID(messageId));
Sitecore.Modules.EmailCampaign.AsyncSendingManager manager = new AsyncSendingManager(message);
var contactId = ClientApi.GetAnonymousIdFromEmail(email);
var recipientId = (RecipientId) new XdbContactId(contactId);
manager.SendStandardMessage(recipientId);
And I see error in the log: The recipient 'xdb:857bbea1-1f18-4621-a798-178399cd0b54' does not exist. But Triggered Message haven't any recipient list
Goals are not assigned directly to messages. You can, however, assign engagement plans and campaigns. Each message has its own engagement plan to handle tracking the contacts actions with the message. If you create a campaign that triggers a goal, you can assign that to the message and it will be associated with the contact when they receive the message. You can also leverage the message engagement plan to trigger events as the contact proceeds through those states.
Also, you're missing some details while recording the contact data.
Look at the newsletter signup control that is included in the EXM module. The important part in there is this:
protected virtual RecipientId RecipientId
{
get
{
RecipientId recipientId = null;
var contactId = ContactId;
if (contactId != (ID)null)
{
recipientId = new XdbContactId(contactId);
}
return recipientId;
}
}
protected virtual ID ContactId
{
get
{
if (!Email.Visible || string.IsNullOrEmpty(Email.Text))
{
return new ID(Tracker.Current.Contact.ContactId);
}
var anonymousId = ClientApi.GetAnonymousIdFromEmail(Email.Text);
return anonymousId.HasValue ? new ID(anonymousId.Value) : new ID(Tracker.Current.Contact.ContactId);
}
}
protected virtual void UpdateEmailInXdb()
{
_recipientRepository.UpdateRecipientEmail(RecipientId, Email.Text);
}
It will write the email address directly to Mongo, rather than waiting for the session to end. Include this and the related RecipientId and ContactId properties in your signup code.
Once they are signed up you can register the goal programmatically or send them to a Thank You page where the goal can be registered (Advanced - Tracking), or send the message and let that register the goal. Or create an engagement plan with states for each step of the process (this is the best way).
You'll also want to add the recipient to a list that the newsletter message can use later. Actually, it looks to me like the example Subscription Form does everything you need.