简体   繁体   中英

Angular 10 JWT asp.net core display user online status realtime

Im using angular 10 as my frontend and asp.net core as my backend. I use jwt as my user authentication method. User posts its username and password to the server side and server generates a token sending it back to the user. And I save the token in user's browser local storage. I have written an http interceptor which adds authorization bearer to the header section of user's api requests. This is the way server knows who is who. Now I have a page in which I should display online and offline users with green and red dots next to their names. I can know when a user signs in because it has to login and I can do whateverI want when user signs in but I dont know how to let my client know when users close their browser and leave the site. I am looking for any library, logic or anything to know my user's online/offline status realtime. I should display which users are online and which are not and it should be realtime. I use microsoft identity context to handle membership. Any help is appreciated. Thank you. I need to add that I want a solution rather than code. I just want to know how to do it. I can handle the code work myself. I just want to know how?

Using a web application to know live information about other users sessions will require the server to publish the other user's data in some way.

One approach would be to use a framework to handle server initiate communication via websockets . This would require something like SignalR , for your ASP.NET Core application, for your server to push the user changes. Using websockets is not hard, but it comes with a steep learning curve and more server requirements to attend to.

As Second approach would be to periodically pull the information about the other users (someone called it "hearbeat request" on the question comments). In this approach you create a normal endpoint on your app, and have your front-end page periodically call it to update the user's info. This is much simpler then websockets to implement, and does not add requirements to your current infrastructure.

The difference on the approaches is how close you get to real-time, and the effort to implement it.

  • With websockets the server would push the user session changes as they occur, there cannot be more real-time then this.
  • Without websockets the client would have to keep pulling the user changes, so in the time between pull's you loose some "real-time".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM