How to notify Web Api user with Signalr?

Asked

Viewed 1,270 times

9

Upon receiving a post in an application Web Api need to notify a particular customer that a new record has been entered. This user will see this notification in a client application(javascript) separate from the project Web Api and will be logged using Identity.

How to send a notification with SignalR for that particular customer(based on login Identity)?

  • You have already set up a Hub to communicate the user?

  • Man, know Pusher? is a service that, let’s say, outsources it to you, I have a post about him. The cool thing is that you make the account in the Pusher service, create a channel and an event, download their api, and on one side trigger the event and on the other, the page, registers via javascript the event, very cool. http://wp.me/p2AWkc-7y

4 answers

2

I think you’re looking for something like "AJAX Reverso", I did not find a definition in English but here is a link to better understand how it works Prokata.com/reverseajax.html.

At the time I used the DWR but it is an implementation for java, in the case of ASP.NET vi that there is the Pokein but it seems that it is not free, has an option of "Free community Edition" but not found for donwload.

But I believe there is some free implementation out there, I think we are not the only ones who have needed to use this feature before.

I hope I’ve helped.

  • DWR is good but you need to have access to the server to install. So you need to know if Carlos can do this?

1

Carlos, considering that your Hub is implemented in the same project as the Webapi:

When a user (via javascript) connects to your Hub, you save his Connectionid and Userid Identity to a database (for example).

When POST happens, you can take an instance from your Hub and send the message to the customer you want.

For an instance of Hub, do:

var meuHub =  GlobalHost.ConnectionManager.GetHubContext<TIPO_DO_MEU_HUB>();

and to send a message, you make as if you are inside the Hub, example:

meuHub.Clients.Client(ConnectionId_DO_CLIENTE).enviaMensagemParaOCliente("woot");

At the time of the POST you check which Connectionid of the recipient client via their Userid and send the message, remembering that the same Userid may have more than one Connectionid (multiple browsers/devices) -then you will probably want to notify all.

0

A server cannot call a client because the Internet is based on a system of small mesangem: connection, transmiçao, cuts connection etc... Then it is the client that will call the server.

What you can do is put a piece of JS on the site, with a timer. For example every minute, the page will call the server to see if you have a new message (see setTimeout() )

2 options: 1) the page calls the served then has a "refresh" page. Much easier in well "aesthetic" because with the no message on the server will have a "refresh" 2) use Xmlhttprequest() which allows a JS code to call another page and receive the information, without doing a "refresh".

  • 1

    Peter, you’re right to say that only one customer can initiate a connection but that has nothing to do with the question. It is perfectly possible and common to send data in real time from the server to the client, without a specific request (see socket.io or signalr, for example)

  • True Leo, but ONLY after the client call. Because to chat with a client the server must have the client IP. A server has a "fixed" IP but not the client. Worse, NOTHING can demonstrate that if the server sends data for 10 minutes, the client will continue to have the same IP for 10 minutes. Today, for example, my internet network has crashed three times, each time for 2 or 3 minutes. And each time, the IP address assigned was different.

  • Peter, as I said, this has nothing to do with the question asked. Disconnection problems should be handled by the programmer if he wants to send server notifications to the client and are quite easy to handle, whether using an abstraction like Signalr or pure websockets. Your suggestions for his need are extremely inefficient and would be very concerned to see a professional site using them nowadays.

  • This has MUCH to do with the question. Because if Carlos and alone in his office, and that throughout his programming time, he has the same IP address, he will be happy because it will work. But when it will connect a second computer what your internet connection will fall, even 5 seconds and that it will receive a new IP, it will not understand why it does not work but.

  • Peter, takes another look at the question, he is not asking anything about how the HTTP protocol works, probably he already knows (since web program), as well as the common fact of occasional IP change. He is just asking how to send a notification to the client, from the server, simple practice and well known today. Your answer is bad for those who fall here because it suggests that what he wants to do is only possible via poolling or refresh, which not only is not true but - I repeat - extremely inefficient and should be discouraged.

0

It seems that you will have a customer (A) that will connect for example to 14H00. Another client (B) will send a message on the server for example at 15H00. What do you want to do and send information on client A, to inform that you have a new message.

It’s impossible to be sure that this will work. Why?

1) Between customer’s Connection A at 14H00 and the moment vc will send a message 15H00, client A can change IP

2) If the client has 2 computers, a router and a modem, when the server will save the IP, it will not save the client’s IP, but the router’s IP. You can do the following test: put on your site a page that shows the IP, connects with a computer of your network and then with another of the same network and you will see that from the point of view of the server the two have the same IP.

When we create a CHAT for example, it has the same problem: show to client A, messages from client B, C etc... The Signalr can be used, but not as you think. You say "How do I send a notification with Signalr to that particular client (based on login Identity)?" which means you think you’re the server that’s gonna call client A. Not possible (unfortunately)

Actually with Signalr, vc will determine the time of the call. And the client A that will have to call the server (for example every 5 seconds). Then the server will know who "calls" and will give the right answer.

You can take a look here: http://www.asp.net/signalr/overview/getting-started/tutorial-high-frequency-realtime-with-signalr

You can also take a look here: https://github.com/JabbR/JabbR In the Scripts folder you have jquery.signalR-2. 2.0-pre-140709-b104. js (lines 694 and following to understand the principle).

  • 1

    Peter you are wrong. At the moment there is the IP change you lose the connection to the server. If you are using Signalr, it tries reconnection and you just handle this reconnection in your Hub. No need to worry about IP, it’s all abstracted for you - you just need to manage Connectionid which is the "address" of the client p/ who you want to send a message to. The "test" you suggest is irrelevant, the internet works perfectly, even with NAT. What Carlos is asking is perfectly possible. And you’re also wrong to say you need to poole on the signalR.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.