Open kajoseph opened 1 week 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
?
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:
websocket
or maybe ws
)
In server.js:
socket.io
orhttp
.app.listen
instead ofserver.listen
. You may want to use the http.createServer() if you're wanting to add on some certificate stuff, but obviously you're not doing that here./api
and then serve those routes using express instead of socket.io and then from the front end just usefetch
to call the endpoints.index.js
orindex.html
.server.js
is a good file name for the backend, but for the frontend it's not very apparent which html page I should start with.