When to use TCP and HTTP in node.js?

2020-07-18 02:10发布

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?

标签: http tcp node.js
2条回答
戒情不戒烟
2楼-- · 2020-07-18 02:56

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.

查看更多
聊天终结者
3楼-- · 2020-07-18 03:10

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.

查看更多
登录 后发表回答