0

we have a project with 1 server and 2 clients

  1. server (webapi c#-mvc) : that is run on https:domain.com:2020.
  2. client1 : android app
  3. client2 : raspberry device (as an IoT device)

these two clients use WebApis to connect to the server and get and post commands and change status in database with api calls.

problem : clients have to do polling (real-time) to the server to find if there is a change in database to update their status. what can i use for this polling ? i thinks socket is the best but i don't know how should i use socket in my webapi, i saw a lot of examples but all of them are running on console app.

i found this example but i think it's not complete code and i don't know how should i use it in my webapi controllers. should i inherit of it or not?

thanks in advance :)

1 Answers1

1

There is a library for this use case - SignalR , easy to use and it works for 4 transport protocol: websocket, forever frames, server sent and long polling. Another approach would be using native websocket support ,but in this case you would need to maintain the state of websocket ( same with any socket if you prefer).

SignalR needs client Library (or you can write one after reading the specifications) and officially available for c# and JavaScript, you can find libraries in some programming languages such as java and python , created by community.

mbshambharkar
  • 386
  • 4
  • 13
  • thanks but i think signalR use websocket as long polling protocol (that is not our prefer)... if i'm wrong, let me know if i can use signalR for sending status to just one specific user not broadcast to all clients. i can't find a good example – Bahar_Engineer Feb 21 '21 at 08:06
  • 1
    Example you have mentioned is also websocket sample, being said that SignalR will fall back to other transport protocol if websocket is not available also you can disable it in the configuration. You can send information to specific client based on client Id or if you map it with Identity you can use user id to push data to specific user. – mbshambharkar Feb 21 '21 at 08:15
  • 1
    Hope this link would give some insights: https://stackoverflow.com/q/19522103/3576971 – mbshambharkar Feb 21 '21 at 08:18
  • 1
    Just a point - websocket and long polling are two different transport protocols with respect to their behaviour and performance. You can read more at https://github.com/aspnet/SignalR/blob/master/specs/TransportProtocols.md – mbshambharkar Feb 21 '21 at 08:27
  • great, i will try to do your suggestions and i will send the result here :) thanks – Bahar_Engineer Feb 21 '21 at 08:40