-3

I want to build a chat application with a stateless server as backend. Users have an online and offline status. When a user connects to the server, the server marks that user as online.

Now imagine a user disconnects because his battery died. The user can not notify the server he is going offline. How would a stateless server make sure the user will be marked offline in that scenario? The server MUST keep some sort of state of the user, right?

Every user is coupled to a user_id UUID primary key in the database in table User which has the status column. I think the server must keep track of the user_id in order to mark the user as offline on disconnect, right?.

I was wondering if it is possible to implement disconnect events in a truly stateless server.

6
  • 3
    What is a stateless server by your definition?
    – Boris B.
    Nov 19, 2021 at 9:39
  • @BorisB. server which does not hold state of users connected to it
    – J. Doe
    Nov 19, 2021 at 10:01
  • 4
    @J.Doe how does your stateless server which does not hold state of users connected to it "mark[s] that user as online" as in the first paragraph of your question? Nov 19, 2021 at 10:12
  • 2
    No, it is not literally your question. You asserted "When a user connects to the server, the server marks that user as online." Your question is "How would a stateless server handle events when the user disconnects?" The answer is "the same way as you marked the user as being online" (just reversed). Nov 19, 2021 at 11:44
  • 2
    What Philip is getting at is that there is a difference between storing data and being a stateless web server. In that context, stateless means that every request is treated independently, not based on some history of prior requests. It says nothing about whether the application stores data. For example: if the first request was "Who is the president of the US?, and the second request is "And how old is he?", this cannot be answered by a stateless server because it requires remembering what the first request was to understand who "he" refers to.
    – Flater
    Nov 19, 2021 at 12:41

1 Answer 1

4

I am struggling to vision how can you possibly build a chat service in an stateless fashion. For this kind of bi-directional communication systems you need to keep track of the available clients which is state by definition.

Anyway, with respect to your question, what you would typically do is request a periodical heartbeat signal from clients and mark them as offline on absence of it - and based on whatever other criteria suits your requirement -.

2
  • The second paragraph answers your confusion in the first paragraph. While not the most common approach, a chat application can poll the backend, just like how you'd implement that heartbeat. Not saying it is the best approach, but it answers "how you could possibly build a chat service in a stateless fashion".
    – Flater
    Nov 19, 2021 at 12:43
  • HTTP is a stateless protocol. Whether you store a 'session' on the server or on a e.g. DB seems to be minor detail when a client drops off the network. The second paragraph is the most straightforward solution, I think.
    – JimmyJames
    Nov 19, 2021 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.