EWS: Closing StreamingSubscriptionConnection after it get expired and reopened raises an exception - exchangewebservices

I have created an object of StreamingSubscriptionConnection that has a lifetime of 30 minutes , which is the max, and reopens automatically as suggested in Auto Reconnect When Using StreamingSubscriptionConnection in EWS.
Till this point everything works fine. however, after the first auto reconnect, if I would like to close the connection to add a new subscription, exceptions start to raise when I open the connection again saying "there is another connection open against the subscription"
Does anyone have any idea about how this happen, and how to solve it

Funny that your question came in today when I am knee-deep in this exact same area in my app! My understanding of this, after some back and forth with the folks in Exchange support, is that you can only add new subscriptions (or remove ones you don't want any more) in the OnDisconnect event handler. I have tried steps similar to yours and get the same kind of errors if I do it outside the handler. As I need for my app to add new subscriptions relatively promptly, I am tending toward a shorter timeout on my streaming connection (e.g. 3 mins), which I realize will add overhead for the more frequent TCP session establishments. In the long run, I'm not sure how bad this is, as under the covers streaming notifications are just doing long polling, so if you're getting lots of notifications, I think you have lots of HTTP Req/Rsp sequences anyway.
In concept Streaming Notifications are great, but based on the doc so far. i.e. the various blogs and forums like this, and the almost useless MSDN references, I have this sinking feeling that I am off on a rather unpleasant adventure I was not expecting.

... Old question ...
I found this documantation ...
First unsubscribe, THAN close connection. It works, but it calls both event handlers. First ErrorHandler (Exception: subscription was unsubscribed?!?) and than DisconnectHandler. So one have to handle tweak handlers with "outside" logic to differ this event from regular error/disconnect event...
Very unintuitive ... I have the same negative feelings about whole EWS )-:

Related

Why are my PushSubscriptions expiring so quickly?

I followed this tutorial to implement push notifications in my web app, and while they work, they stop working very quickly. It seems to handle the first couple of notifications well, but after that, sometimes when less than five minutes has gone by, the subscription expires, according to my back-end. I get an error code of 410 from the push service which the tutorial says is meant to indicate that the subscription expired.
This is crazy, how can a subscription expire in less than five minutes? I searched up my issue and it seems no one else has had this problem, some even saying that subscriptions can last years!
I tried implementing an event listener in my service worker for the "pushsubscriptionchange" event, but according to Serginho's answer, Chrome did not implement that event as of 2019, and I don't think that's changed since then.
Oh, and while Chrome can handle the subscription (but only once/twice as explained above), Firefox doesn't even do anything. I feel like I'm going crazy. If I test the push subscription feature itself using this site, however, it works in Firefox! and Chrome! What are they doing that I'm not? They show the exact same code as the tutorial I linked above.
What even can I do at this point? I've considered perhaps creating an interval with setInterval() and just resubscribing the user every second or so, but I don't think that'll work.
Any help would be appreciated.
You absolutely need to handle the pushsubscriptionchange in your service worker. Otherwise when a subscription expires and is replaced with a new one you will lose it.
Chrome and most browsers actually trigger that event (I don't know where you read something different). I am sure that it is triggered because on our push service we receive thousands of hits per hour from that event.
Take a look at our service worker if you need inspiration on how to implement that event. Then on your server you simply replace the old subscription with the new one.

Google realtime object pool

This question is a little "meta" for SO, but there doesn't seem to be a better place to ask it...
According to Google, realtime collaborative objects are never deleted from the model. So it makes sense to pool objects where possible, rather than not-really-delete them and subsequently create new ones, thus preventing an unnecessary increase in file-size and overhead.
And here's the problem: in an "undo" scenario, this would mean pulling a deleted object out of the trash pool. But "undo" only applies to operations by the local user, and I can't see how the realtime engine could cope if that "deleted" object had already been claimed by a different user.
My question is, am I missing something or wrong-thinking, and/or is there an alternative to a per-user pool?
(It also occurs to me that as a feature, the API could handle pooling deleted objects, automatically minimizing file-bloat.)
I think you have to be very careful about reusing objects in the way you describe. Its really hard to get right. Are you actually running into size issues? In general as long as you don't constantly create and throw out objects, it shouldn't be a big deal.
You can delete the contents of the collab object when its not being used to free up space. That should generally be enough.
(Note, yes, the API could theoretically handle this object cleanup automatically. It turns out to be a really tricky problem to get right, do to features like undo. It might show up as a future feature if it becomes a real issue for people.)
Adding to Cheryl's answer, the one thing that I see as particularly challenging (actually, impossible) is the pulling-an-object-from-the-pool stuff:
Let's say you have a pool of objects, which (currently) contains a single object O1.
When a client needs a new object it will first check the pool. if the pool is not empty it will pull an object from there (the O1 object) and use it, right?
Now, consider the scenario where two clients (a.k.a, editors/collaborators) need a new object at the same time. Each of these clients will run the logic described in the previous paragraph. That is: both clients will check whether the pool is empty and both clients will pull O1 off of the pull.
So, the loosing client will "think" for some time that it succeeded. it will grab an object from the pool and will do some things with it. later on it will receive an event (E) that tells it that the object was actually pulled by another client. At this point the "loosing" client will need to create another object and re-apply whatever changes it did to the first object to this second object.
Given that you do not know if/when the (E) event is going to fire it actually means that every client needs to be prepared to replace every collaborative object it uses with a new one. This seems quite difficult. Making it more difficult is the fact that you cannot do model changes from event handlers (as this will trump the redo/undo stack). So the actual reaction to the (E) event need to be carried out outside of the (E) event handler. Thus, in the time between the receiving of the (E) event and the fix to the model, your UI layer will not be able to use the model.

FreePBX Queues Issue

I'm experiencing some issues with FreePBX queueus.
The longest calls waiting don't always seem to have priority, in various cases we've experienced instances when a call was on hold for 10 ten minutes and another call came in and the new call was sent to the next available agent before it.
Anyone have any experience with this?
According to what I've been told by some developers, this seems to be a bug in Asterisk's queue application. The queue app doesn't seem to share the longest wait times across the queues, thus, if a member is part of multiple queues, there could be some problems like what we've experienced.
I have come to accept that and moved on to a commercial grade Call Center solution.

Failures in eventual consistent system and user experience [duplicate]

When using distributed and scalable architecture, eventual consistency is often a requirement.
Graphically, how to deal with this eventual consistency?
Users are used to click save, and see the result instantaneously... with eventual consistency it's not possible.
How to deal with the GUI for such scenarios?
Please note the question applies both for desktop applications and web applications.
PS: I'm working with the Microsoft platform, but I imagine the question applies to any technology...
A Task Based UI fits this model great. You create and execute tasks from the UI. You can also have something like a task status monitor to show the user when a task has executed.
Another option is to use some kind of pooling from the client. You send the command, and pool from the client until the command completed and the new data is available. You will have a delay in some cases from when the user presses save to when he will see the new record, but in most cases it should be almost synchronous.
Another (good?) option is to assume/design commands that don't fail. This is not trivial but you can have a cache on the client and add the data from the command to that cache and display it to the user even before the command has been executed. If the command fails for some unexpected situation, well then just design a good "we are sorry" message for misleading the user for a few seconds.
You can also combine the methods above.
Usually eventual consistency is more of a business/domain problem, and you should have your domain experts handle it.
I think that other answers mix together CQRS in general and eventual consistency in particular. Task-based UI is very suitable for CQRS but it does not resolve the issue with eventually consistent read model.
First, I would like to challenge your statement:
Users are used to click save, and see the result instantaneously... with eventual consistency it's not possible.
What do you by this? Why is it not possible to see the result immediately? I think the issue here is your definition of result.
The result of any action is that that action has been performed. There are numerous of ways to show this! It depends on what kind of action do you want to complete. Examples:
Send an email: if user has entered a correct email address, it is almost guaranteed that the action will complete successfully. To prevent unexpected failures one might use durable queues since this kind of actions do not need to be done synchronously. So you just say "email sent". Typically you see this kind of response when you ask to reset your password.
Update some information in a user profile: after you have validated the new data on the client, most probably the command will succeed too since the only thing that could happen is the database error (if you use database). Again, even this can be mitigated by using durable queues. In this case you just show the updated field in the same form. The good practice for SPA is to have a comprehensive data store on the client side, like Redux does. In this case you can safely update the server by sending a command and also updating the client-side store, which will result in UI to shows the latest data. Disclaimer: some answers refer to this technique as "tricking the user", but I disagree with this definition.
If you have commands that are prone to error, you can use techniques that are already described in other answers like Websockets or Server-side events to communicate errors back. This requires quite a lot of additional work. You can also send a command and wait for reply or execute commands synchronously. Some would say "this is not CQRS" but this would be just another dogma to be challenged. Ensuring the command has completed the execution in combination with the previous point (client-side data store) will be a good solution.
I am not sure if there is any 100% bullet proof technique that allows you to always show non-stale data from the read model. I think it goes against the principles of CQRS. Even with real-time events you will only get events that indicate that you write model has been updated. Still, your projections could have failed and reacting on this is a whole other story.
However, I would not concentrate that much on this issue. The fact is that well-tested projections and almost-guaranteed commands will work very well. For error handling in 90% of situations it is enough to have some manual or half-manual process to recover from those errors. For the last 10% you can combine generic "error" messages pushed from the server saying "sorry, your action XXX has failed to execute" and the top priority actions could have some creative process behind them but in reality those situations would be very very rare.
There are 2 ways:
To trick a user (just to show that things has happened then they
really hasn't happened yet)
Show that system is processing request
and use polling in background (not good) or just timer with value of
your SLA.
I prefer the 1st option.
As someone has already mentioned, task based UI's fit well for this, and what I would do is employ a technique that 'buys you time' for the command to propagate.
For example, imagine we are on a list screen, where the user can perform various actions, one of which being to add a new item to the list. After choosing to add an item you could display a "What would you like to do next?" which could have 'Add another item', 'Do this task', 'Do some other task', 'Go back to list'.
By the time they have clicked on an option, the data would have hopefully been refreshed.
Also, if you're using a task based UI, you can analyse the patterns of task execution and use these "what would you like to do next" screens to streamline the UI. Similar to amazon's "other people also bought these items".
As previously stated, it is fine to tell the user that the request (command) has been acknowledged (successfully issued). In case of some failure, the system should communicate this to the requester, by means of:
email;
SMS;
custom inbox (e.g. like the SO inbox);
whatever.
E.g., mail client / service:
I am sending a mail to a wrong address;
the mail service says: "email sent successfully :)";
after few minutes, I receive a mail from the service: "email could not be delivered".
I believe a great way to inform the user about a recent failure is to present him an error panel while he's navigating through the application. A user gesture might be required in order to dismiss that alert etc.
For example:
I wouldn't go with tricking the user or blocking him from committing some other actions. I would rather go for streaming data toward UI after they are being acknowledged by a read side. Let's consider these two cases:
Users saves data and expects result. Connection is established toward server. After they are being acknowledged by a read side, they are streamed toward UI and UI is being updated.
User saves data and refreshes web page. Upon reload, data are being fetched from data store and connection for streaming is established. If read side didn't update the data store in the meantime, there's still an opened stream and UI should be updated after data reaches the read side.
Why streaming from read side and not directly from write side? Simply, that would be a confirmation that read side has been reached.
From technical aspect, Server-Sent Events could be used.
Disadvantage:
Results will still not be reflected immediately by a read side. But at least, in most cases, user will be able to continue with his work without being blocked by a UI.
There are several ways to handle eventual consistency. All of them are really to occupy the time from the User's action until the backend refresh.
User Reads A given user can only read from the same database node that they write to. Other users read from the replicated nodes. PROS: UI is quick enough, and application stays in sync. CONS: Your service architecture has to track and route Users to specific database nodes.
Disable the UI until the action has completed, and refresh it. Java Server Faces has a classic example of this. One could create a modal with a loading spinner to cover the UI until the refresh was completed. PROS: UI stays in sync with application state. CONS: Most every action creates a blocked UI. Users get very frustrated by the restricted UI, and will complain of application slowness.
Confirmation Immediately thank the user for their submission. Then let them know later (email, SMS, in-app notification) whether or not the action was completed. PROS: It's fast up front. CONS: UI lags behind system until refresh. Even with a notice, the User may get confused that they don't see the updates. It also requires integration of various communication channels. Users won't see their changes right away. If the action fails, they may not know until it's too late.
Fake it Optimistically assume that the action will complete. Show the User the resulting UI (upvote, comment, credit card confirmation, etc) and allow them to continue as if it succeeded. If there were failures, immediately show them as contextual errors: alerts next to the undone upvotes, in-app alert on the post with the failed comment, email for the declined credit card. PROS: UI feels much faster. CONS: UI is temporarily out of sync with application state, and you must resolve that. One case: you might fake creation of content with temp IDs. But after content is created, then the temp IDs will be wrong until the refresh. Second case, you might need to store all state changes on the UI after the action until the refresh. Then you need some Resolver to apply all the local state changes since the action was issued. This is resolution is non-trivial.
Web Sockets Subscribe the UI to an event stream so that when the action is completed on the backend, it is pushed to the front end. Is it one-way or two-way streaming? PROS: UI feels fast, and it's in sync with the application state. CONS: Consistent browser support, need a backend source of streaming events, and socket server scalability.

Handle multiple emits in socket.io with long polling

Is there any good solution to support long polling clients in scenarios like this:
sockets.in("room1").volatile.emit(message);
sockets.in("room2").volatile.emit(message);
The client is in both rooms and is only received the message in one of the rooms. I suppose it has to do with long polling. When I remove "volatile" from the latter emit, then it works - but is there another way?
Side question: Are there any side-effects by not using VOLATILE? Like the server throwing exceptions etc. I can imagine that it comes with abit of overhead. And how many resends does the server perform?
Best regards,
Mattias
Actually volatile is more unstable. If polling have not reached server after last data sent, it's gone. You don't need to use volatile. If you do not use it, it will queue up all emit in case of connection has not reached yet and when it reach, it get executed in order. I cannot see any advantage from using volatile from my experience.
Text from Socket.io first page about volatile:
"Sometimes certain messages can be dropped. Let's say you have an app that shows realtime tweets for the keyword bieber.
If a certain client is not ready to receive messages (because of network slowness or other issues, or because he's connected through long polling and is in the middle of a request-response cycle), if he doesn't receive ALL the tweets related to bieber your application won't suffer.
In that case, you might want to send those messages as volatile messages."