Using push notifications service along with WCF for windows store application - wcf

I'd like to build an app based on WCF service and Win8 as presentation layer. I will have messaging capabilities in the app so any user must see updates on the tile (when new messages arrive). As I understand I need to use "Push notifications service" mechanism somehow to update all client s tiles with new arrived messages? How would app work? It should call directly to WCF service and then WCF service should call to push notifications service, or firstly Win8 calls to notification service which calls to WCF? Can somebody clarify me how all this stuff can communicate with each other? Perhaps I should think about WCF Duplex approach? Is it possible to make WCF service to be Push notification service as well?

You can use the
The Windows Push Notification Services (WNS). Your app requests a notification channel. Then it will call the WCF service to send it that channel. The WCF service will send a POST request to the notification channel (which is a URI) in order to notify the app. The data of the request is in XML format (you can read about it in the link).
The Windows Push Notification Services (WNS) enables third-party
developers to send toast, tile, badge, and raw updates from their own
cloud service. This provides a mechanism to deliver new updates to
your users in a power-efficient and dependable way.
It involves these steps:
- Your app sends a request for a push notification channel to the Notification
Client Platform.
- The Notification Client Platform asks WNS to create a notification channel.
This channel is returned to the calling device in the form of a Uniform
Resource Identifier (URI).
- The notification channel URI is returned by Windows to your app.
- Your app sends the URI to your own cloud service. This callback mechanism is
an interface between your own app and your own service. It is your
responsibility to implement this callback with safe and secure web standards.
- When your cloud service has an update to send, it notifies WNS using the
channel URI. This is done by issuing an HTTP POST request, including the
notification payload, over Secure Sockets Layer (SSL). This step requires
authentication.
- WNS receives the request and routes the notification to the appropriate
device.

Related

How to receive an Inbound SMS using Twilio in VB.NET WinForms?

I have a WinForms application and I want to receive inbound SMS using Twilio. I am using VB.NET. The code that I find on the Twilio website are using web application and MVC. Can anyone help me how to use it in WinForms?
Did not get enough to try out anything.
Twilio uses a standard way of notifying your service, called webhooks. When an SMS, phone call, or something else happens, and you configure a webhook for that, Twilio will send HTTP requests to the URL configured as a webhook.
This does mean that you have to have a publicly running web server that can accept those HTTP requests with the details of the SMS, phone call, etc. That's why the samples will use ASP.NET, as this is only possible with web technology.
Winforms runs on your computer and doesn't expose any public web endpoints to receive the webhook HTTP requests, so you can't receive it directly. However, depending on your use-case, you have options.
If you don't need real-time updates, you can read the message history using the Twilio C# .NET SDK.
In your winforms app, you could add a button to refresh the messages on click. Alternatively, you could query the message X amount of seconds to give it a more real-time feel, even tho it's not really real-time.
Warning: to use the Twilio API to get the messages, you'll need to embed the Twilio credentials into your Winform app. Anyone that has access to your app will be able to read those credentials. Keep that security risk in mind!
The second option is to use ASP.NET to receive the webhook HTTP requests, and then use a SignalR or websockets to notify any connected clients, of which your winform app would be one.
For example, when Twilio receives an SMS, the ASP.NET application receives the webhook HTTP request, the ASP.NET app then send the SMS details to all clients connected to your SignalR hub, and then your Winforms app receives the SMS payload which you can use to render your app.
The second option is a lot more work and requires more infrastructure since the ASP.NET app needs to be hosted somewhere. We don't have a tutorial for that, but I'd be happy to forward more links to docs etc. if you have questions.

REST API with active push notifications from server to client

Problem description
i am working on a Xamarin application that consumes a REST API written in Python flask.
The Xamarin application offers virtual shopping lists where user can collaborate on buying stuff they have on a shared list.
To improve the user experience, i want to be able to actively notify the user about finished items on the list.
Possible solutions:
Synchronous API polling from client side
Notifications are stored by the API in a relational database and have a flag indicating if the user received the notification already.
The API has an endpoint GET /users/:user_id/notifications/ that queries the database for notifications and returns a JSON response with those.
Advantages
fairly simple to implement
Problems
synchronous polling creates a huge amount of http requests
API service remains stateless, making a horizontal scaling with a loadbalancer easier
Websocket endpoint on the API
The API has an endpoint POST /users/:user_id/notifications/register which creates a websocket connection between client and API.
The connection is stored to a global array in which each entry maps a client id to a websocket connection.
When a new notification is created, the endpoint makes a lookup in the connection dictionary by comparing the owner id of the notification with the dictionary entries. The notification is sent to appropriate user through the websocket.
Notifications are stored in the database like in the first approach.
When a user calls the endpoint, a new websocket connection will be established first and upon success the API sends all unseen notifications from the database to the user.
Advantages
API can push notifications to clients asynchronously
Problems
When a user terminates the websocket connection his dictionary entry will persis
Retaining one websocket connection per user permanently adds additional overhead to the API
Horizontal scalability of the API is more difficult because the service is not stateless anymore (Websocket connection information saved in
RabbitMQ
The API uses a RabbitMQ service to send notifications to the client. Every client uses subscribes to his own notification queue to prevent the broadcasting of messages.
Advantages
API remains stateless
Problems
Notifications needs to be resend to the exchange when a user is offline
Amount of queues grows drastically
Additional costs for RabbitMQ service
High temporary load on the RabbitMQ service when many users come online in the same time
Final words
It would be interesting to hear the opinion of others.
I believe the active distribution of notifications from backen services to clients i a very common use case.
best,
D
I would use RabbitMQ and consume events forwarding them as push notifications. This will work while the user is not actively connected to the website and enhance the engagement with each user experience that will return to the website when notified for more information see How to setup basic web push notification functionality using a Flask backend or How to send push notifications to a browser in ASP.NET Core or Sending Notifications with Spring Boot, Angular, and Firebase Cloud Messaging this way the RabbitMQ will not wait until the user is back online. If the user is online you can forward the notification directly to the Xamarin application via WebSockets and a load balancer like NGINX that can handle many WebSockets in an optimized way.
Synchronous API polling from the client-side is the less preferred way since it overloads the webserver with requests while nothing was changed.
I don't think the scalability of WebSocket is a problem. You can scale up easily with pub/sub. The hotspot of long connections is a kind of serious problem.
For one-way communication, I would suggest Server sent event. In the end, it usually depends on what your team is confident with.
I can recommend on a different approach for API that provides JSON which is called GraphQL
It supports subscriptions capabilities that are pushed by the GraphQL API Server (using web sockets)
GraphQL is considered today to be better than RESTful API since its very flexible and you can get exactly the data you need with one query.

WCF Service Callback Integration with Web Push API for Web Notifications

I need notifications (which comes from a WCF service callback) in my web application even when the browser is not opened.
The notification source / event I will get from a WCF service callback from WCF service hosted in the network.
I came across the Web Push API and Service Workers concepts which gives me the intended behavior (notify when even browser is closed).
Push API can send the notification to service worker and then it can process.
Only service worker can get activated even when browser is not opened for notifications.
I doubt whether I can host WCF service for the service callback even when browser is closed in contrast to service workers.
How to achieve this scenario? notifications which I get at WCF service callback in my webapp -> pass to push api -> service worker -> Notification Display.

WCF duplex channel for inter-process communication

I have system like this:
Windows service (WCF, data/events) <-> Web app <-> Web client
I need simultaneous response for clients requests. I have some events from service for clients too. So duplex channel is the way to go. But I need high throughput, because clients calls simultaneously.
Request/reply approach
In order not to serialize channel requests I need more channels for parallel calls, right? But how to handle callback channel then? Ho to keep it still open for receiving events, even on channel errors?
OneWay approach
On channel should be enough (no waiting for data preparation), but how to link data sent to callback with original request, to be able to compose response for client?
What is the way to go? Thank you.
In a simple case, when a web client sends a request to the web app, and web app (possibly) sends a request(s) to WCF service, there's no need in duplex binding at all.
As for events, raised by the service to be fired in Web client, I'd suggest to use a message broker which supports WebSockets - for example RabbitMQ. It has a plugin compatible with WebSockets and WCF binding.
Putting things together, one can create a RabbitMQ server, which accepts messages from WCF service and sends it to Web client, which subscribes to the event feed from Javascript.

SignalR - Sending a Message from a WCF Project

I've followed the instructions from https://github.com/SignalR/SignalR/wiki/Hubs
entitled "Broadcasting over a Hub from outside of a Hub".
I got this method working from within an MVC Action in the same project. Requesting the Action sends the update to connected clients.
My problem is that I need to be able to send updates from another project, in particular a WCF Web Services project. My app has an API and a web component and when API users make calls that change things, these updates need to be pushed out to the Web clients via SignalR. And calling a web service with the same code as my Test Action doesn't work.
I also tried the same code inside an nunit unit test that didn't work either.
What do I need to do to make this same method described on the Wiki work for a WCF Project?
The easiest solution is probably to provide an API on your Web Application (use MVC or the new WebAPI) that broadcasts to all connected clients. Any other application (an NT Service, an NUnit test, ...) can call that API if it wants to send a message to the clients.
You can't expect SignalR to do anything if you aren't hosting a Hub either in a Web Application running under IIS, or another application hosting it directly.
If you need two-way communication from your separate application to your clients then simply make your application into a SignalR client too and have it communicate via the Web Application hosted SignalR to the clients and have it listen to messages from them too.
For example, here's how I have configured a complex Service + WebSite + Clients solution (ignore the purple for now):
The Live Web Server allows NT Services to connect and create SignalR Groups. NT Services send to those groups. Web browsers connect to a group and receive messages send to that group. In effect the middle box becomes a pubsubhub.
I cannot get exactly what you aim. But if I understood correctly you're trying to send some kind of notifications raised inside WCF services to SignalR clients.
If that's the case; I can suggest you my approach:
I have some WCF services and a SignalR hub in the same application server. IMHO, the best way to communicate WCF with SignalR hub is by using MSMQ.
When a notification occurs inside a WCF service, it puts the notification payload into MSMQ.
On the other end, SignalR hub listens the same queue. When a message put into the queue, it gets the content and broadcasts to the hub clients. Very easy and straightforward. No extra service/hub call at the server side.
SignalR hub can listen for new queue items by using System.Messaging.MessageQueue#ReceiveCompleted method. When this event raised, SignalR hub gets the queue item and broadcasts to its clients.