Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use TCP and HTTP in node.js?

Tags:

http

node.js

tcp

Stupid question, but just making sure here:

When should I use TCP over HTTP? Are there any examples where one is better than the other?

like image 854
resopollution Avatar asked Dec 23 '22 03:12

resopollution


2 Answers

TCP is full-duplex 2-way communication. HTTP uses request/response model. Let's see if you are writing a chat or messaging application. TCP will work much better because you can notify the client immediately. While with HTTP, you have to do some tricks like long-polling.

However, TCP is just byte stream. You have to find another protocol over it to define your messages. You can use Google's ProtoBuffer for that.

like image 77
ZZ Coder Avatar answered Jan 14 '23 15:01

ZZ Coder


Use HTTP if you need the services it provides -- e.g., message framing, caching, redirection, content metadata, partial responses, content negotiation -- as well as a large number of well-understood tools, implementations, documentation, etc.

Use TCP if you can't work within those constraints. However, if you use TCP you'll be creating a new application protocol, which has a number of pitfalls.

like image 43
Mark Nottingham Avatar answered Jan 14 '23 13:01

Mark Nottingham