Websocket API to replace REST API?

后端 未结 10 1678
温柔的废话
温柔的废话 2020-12-02 03:43

I have an application whose primary function works in real time, through websockets or long polling.

However, most of the site is written in a RESTful fashion, which

10条回答
  •  醉酒成梦
    2020-12-02 04:25

    HTTP REST and WebSockets are very different. HTTP is stateless, so the web server doesn't need to know anything, and you get caching in the web browser and in proxies. If you use WebSockets, your server is becoming stateful and you need to have a connection to the client on the server.

    Request-Reply communication vs Push

    Use WebSockets only if you need to PUSH data from the server to the client, that communication pattern is not included in HTTP (only by workarounds). PUSH is helpful if events created by other clients needs to be available to other connected clients e.g. in games where users should act on other clients behaviour. Or if your website is monitoring something, where the server pushes data to the client all the time e.g. stock markets (live).

    If you don't need to PUSH data from the server, it's usually easier to use a stateless HTTP REST server. HTTP uses a simple Request-Reply communication pattern.

提交回复
热议问题