MicahMaphet / LocalChat

0 stars 1 forks source link

Suggestions #3

Open kajoseph opened 1 week ago

kajoseph commented 1 week ago

In server.js:

MicahMaphet commented 4 days ago

That makes since for sending data from the client to the server. But it appears that using app has a loss of functionality. The advantage socket.io has is it can send data to all of the clients at once without refreshing or creating some kind of loop for listening to requests. The client does not have app.listen (unless I send a node module...) so it can get the data whenever the server gives it. I need to get a message from client to server and send it back to all of the clients, which requires some kind of arbitrary data receiving (such as app.listen or socket.on) on the client side. The only way I know to get data on client side without socket.on is fetch which only can get the data once, unless I do some kind of loop... How do I do that without socket.io?

kajoseph commented 3 days ago

ah ok, I was thinking you were just wanting to post messages to the server. If you're wanting to have 2 clients that are messaging each other through the server (or otherwise expecting to receive messages from the server), there are main ways of doing it:

  1. Websockets (I believe the npm package for it is just called websocket or maybe ws)
  2. As you alluded to, having a loop that polls for new messages. In fact, this is the way facebook does their notifications (or at least used to). They do/did what's called a long poll where there's a forever loop that calls an API and the API just doesn't respond until there's a notification. The request times out after 30s or so and the loop continues.