How to notify Api Web user with SignalR?

9

When I receive a post in an application Web Api I need to notify a certain client that a new record has been entered. This user will see this notification in a separate client application ( javascript ) of the Web Api project and will be logged using Identity .

How do I send a notification with SignalR to that particular client (based on login Identity )?

    
asked by anonymous 29.09.2014 / 05:04

4 answers

2

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

At the time I used DWR but it is an implementation for java in the case of ASP I saw that there is Pokein but it looks like it's not free, you have a option. edition " but I did not find it for donwload.

But I think there's some free implementation out there, I do not think we're the only ones who have ever needed to use this feature before.

I hope I have helped.

    
10.10.2014 / 16:01
1

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

When a user (via javascript) connects to his Hub, you write the ConnectionId and UserId of his Identity to a database (for example).

When POST happens, you can take an instance of your Hub and send the message to whatever client you want.

To obtain an instance of the Hub, do:

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

and to send a message, you do as if you were inside the Hub, for example:

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

At the time of POST you check which ConnectionId of the recipient client via its UserId and sends the message, remembering that a same UserId can have more than one ConnectionId (multiple browsers / devices) - so you probably want to notify everyone.

/ p>     

20.11.2014 / 09:06
0

A server can not call a client because Internet is based on a small mesangem system: connection, transmission, disconnection, etc. So the client is going to call the server.

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

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

    
07.10.2014 / 16:37
0

It seems that you will have a client (A) that will connect for example at 14H00. Another client (B) will send a message on the server for example at 3:00 p.m. What do you want to do and send an information in client A, to inform you that you have a new message.

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

1) between the client connection A at 14H00 and the moment you are going to send a 15h00 message, 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 IP of the client, but the IP of the router. You can do the following test: put on your site a page that shows the IP, connects with a computer on your network and then another one on the same network and you will see that from the server's point of view the two have the even IP.

When we create a CHAT for example, it has the same problem: show client A, client messages 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 that you think it's the server that's going to call client A. Not possible (unfortunately)

Seriously with SignalR, you will determine the time of the call. And the A client that will (for example, every 5 seconds) call the server. Then the server will know who "calls" and will give the right answer.

You can take a look here: link

You can also take a look here: link In the Scripts folder you have jquery.signalR-2.2.0-pre-140709-b104.js (lines 694 and following to understand the beginning).

    
21.11.2014 / 00:39