That's actually all we need server side to send and receive messages believe it or not. We're going to start with a controller that receives a heartbeat from users who are signed into chat: In this case, we're receiving a message that contains information in the header about who the user is, and mark their most recent heartbeat in a class called the instance.We also have a method that will aggregate the user names of all the active users, based on the metric of them checking in within the last 5 seconds.The code below is likely horribly inefficient and could probably be rewritten by a UI developer in about 10 lines.
In return, users can reply when receiving a message, telling the server they're still active, creating a round trip heartbeat.I have a love/hate relationship with frameworks that are automagical. Often times automagical frameworks have rules, usually modeled by conventions. Sometimes it tells you something that you can't accurately discern without digging into the bowels of the code that you're trying to leverage without creating bowels yourself.On one hand, they can save you a lot of time and effort, preventing you from writing tons of boilerplate code, and just less code in general (every line of code is one you have to support, right? If you violate these, the framework will slap you across the face with a stacktrace. In this case, Spring was guilty of said crimes on several occasions. However, in this case the solution involved going roughly shoulder deep into Spring's bowels to find simple, yet not obvious solutions to problems.If a client hasn't checked in for more than 5 seconds (i.e. This gives us near real time resolution of users being available to chat.Users will appear in a box on the left hand side of the screen, clicking on a name will pull up a chat window for them, and names with an envelope next to them have new messages.