0

I am making a chat app. I have operations like sendMessage like this:

sending_client_socket -> server_socket -> receiving_client_socket

This is obviously very fast, but what about when I need to persist the message data? Should I instead use an http post initially?

http_post -> http_server -> emitting_server_socket -> receiving_client_socket

I am worried this will be slow, due to the headers and so forth.

My other option seems to be:

sending_client_socket -> server_socket -> receiving_client_socket -> database operations) -> receiving_client_socket

But I have security concerns about that approach, as I won't be able to verify who is sending the message (since I am using jwts).

What is the common approach?

Dylan Harness
  • 719
  • 1
  • 9
  • 20

1 Answers1

1

Which operations should be sockets and which should be http?

There is no specific preference. A requirement to persist the data can be met just fine with a webSocket or socket.io connection. It simply does not matter how the data was sent (via webSocket or via http post). Your server can implement code to do whatever it wants with the data when it arrives either way.

So, select the transport that makes the most sense for other reasons and then just put the code behind that transport to persist the data.

If you already have an authenticated socket.io connection for other reasons, then it makes perfectly fine sense to just send the data over that connection that you want to persist.

If you don't have a trusted means of authenticating the other endpoint for your socket.io and it's easier, simpler or more practical to use an HTTP post where you do already have the appropriate authentication code, then you can go that way just fine too. Since all socket.io connections start with an HTTP connection and there's an authentication scheme built into socket.io, you could also move your auth scheme over to socket.io too. Which ever is more practical to implement will be a fine choice.

What is the common approach?

Certainly HTTP requests are much more common than messages sent over socket.io, but HTTP is more common for other reasons which don't really have to do with your particular situation. For a generic discussion of supporting a given request on socket.io or via Ajax, you can see this other answer: Ajax vs Socket.io

Community
  • 1
  • 1
jfriend00
  • 683,504
  • 96
  • 985
  • 979