List of commonly used Websocket technologies

1 Introduction

Websocket is  a protocol that  HTML5  began to provide for full-duplex communication on a single  TCP connection. WebSocket  makes the data exchange between the client and the server easier, allowing the server to actively push data to the client, and of course it also supports the client to send data to the server. Usually used for social chat, barrage, multi-player games, collaborative editing, stock fund real-time quotes, automatic information updates and other scenarios, so today I will briefly talk about the technical selection of Websocket in Java development .

Technical selection is to choose the most suitable technical solution based on your own business, and there is no praise or criticism.

2. Commonly used Websocket technology

2.1 Java Websocket specification

This is the specification provided by JavaEE . Under the package javax.websocket, it contains the client API and the server API. The server API is completely dependent on the client API, but some functions are added to it, so you only need to import the server dependencies. Specific implementation needs to be provided by a Web container, JavaEE server or framework. Our commonly used web containers such as Tomcat and Undertow are supported.

Advantages: simple integration, native Java support.

Disadvantages: High coupling with shared containers such as web servers, broadcast and multicast need to be controlled by themselves. Concurrency is low, tuning is troublesome, and compatibility problems exist.

2.2 SockJS

SockJS is a browser JavaScript library that abstracts Websocket . SockJS provides you with a consistent, cross-browser Javascript API , which creates a low-latency, full-duplex, cross-domain communication channel between the browser and the web server. SockJS tries to use native WebSockets first . If it fails, it will try various other browser-specific transmission protocols, such as xhr-streaming , Server sent events,  and long polling. Usually also cooperate with STOMP (Simple Text Protocol for Message) to simplify its use. In fact , this protocol is used in Spring  's Websocket component.

Advantages: active community, mature technology, rich protocol stack, a full set of Spring solutions, strong compatibility, and can be combined with publish and subscribe mode.

Disadvantages: Need to learn SockJS and STOMP, disconnection reconnection, heartbeat detection, binary support is not good.

2.3 Socket.IO

Socket.IO  is a real-time application framework based on Node.js. It is widely used in instant messaging, notification and message push, real-time analysis and other scenarios, but it provides Netty- based server-side implementation and client-side implementation. Support Websocket and long polling. In addition to the common scenarios of Websocket , we can implement Android and IOS message push through this component .

Advantages: good performance, support for broadcast, multicast, disconnection reconnection, heartbeat detection, binary. Support Android and IOS platforms. The community is active.

Disadvantages: Need to encapsulate the integration with Spring by itself, the server is not maintained by the community and consumes a lot of resources.

2.4 ReactiveStream

Some reactive flow specifications and frameworks also implement Websocket . Spring Webflux and RSocket are the representatives, and some related demos have been released officially.

Advantages: high throughput, high performance.

Disadvantages: relatively new technology, less learning materials.

3. Summary

If the business volume is very small and very urgent, you can try the first one. The point of contention between SockJS and Socket.IO is that the latter is better in performance. Of course, it consumes more resources and supports better mobile push functions. SockJS has more advantages in Spring integration and a full set of solutions . If the pursuit of high-performance, high-throughput Websocket is undoubtedly more appropriate, the learning cost is relatively high. Other niche technologies are not evaluated here, if you have a better solution, you can leave a message to discuss.

Attachment: performance benchmark test

The following are some key indicators of the performance test of native Websocket , SockJS , Socket.IO in 2020 by a foreign paper .

As the number of clients increases, it takes time to create connections

The average time to receive messages as client connections increase

The number of connections consumed to receive a message and the number of reassembled TCP segments

Server memory usage trend

 Original link: https://www.cnblogs.com/felordcn/p/13688366.html

If you think this article is helpful to you, you can like it and follow it to support it, or you can follow my public account, there are more technical dry goods articles and related information sharing, everyone can learn and progress together!

 

Guess you like

Origin blog.csdn.net/weixin_50205273/article/details/108656859