You haven't shown anything in your code that relates to chatbot functionality. Let's assume that you know that you need both a client and a server in this scenario, then you need to think about how you want to handle messaging. The first thing that you need to consider that each user in the system must have a unique identifier of one form or another. The reason that this is important is because you are going to want to send messages to someone who is uniquely identifiable. Let's imagine that our site has 5 users identifed somewhat originally as A, B, C, D, and E, and these users are all online at the same time.
Now user A wants to send a message to user B. From the client, he sends something to indicate that he wants to send this chunk of text to user B. The server, on receiving this, forwards this to user B's connection. B receives the message and replies back to A; the same client/server process happens in reverse.
A knows B, D, and E, but doesn't know C. A wants to send a message to all the people they know. Theoretically, the same process as above could happen, just sending the server message on to multiple people. Alternatively, these could all be inside a group and A sends the message to the group. Internally, when the server receives the request, it will forward the messages on to individual connections but, for convenience sake, it will do this hidden under the covers as a message to a group.
What you see here is that the important part of the process is the interaction between the client and the server, and the connections it keeps open to know who is online at any point. Theoretically, you could manage all of this yourself so, when a user logs in, they register a connection at the server which is kept open for the duration of their client session. This is easy enough to do conceptually but making this a robust approach takes a lot of hard work.
If I were doing this entirely in JavaScript, I would use
socket.io[
^] and use that to manage the end to end communications.