1. Introduction

Websocket is a protocol for full duplex communication over a single TCP connection provided by HTML5. WebSocket makes it easier to exchange data between the client and the server, allowing the server to actively push data to the client, and of course, supporting the client to send data to the server. Usually used for social chat, barrage, multi-player games, collaborative editing, real-time stock fund quotation, automatic update of information and other scenarios, so today we will simply talk about the selection of Websocket technology in Java development.

The selection of technology is the most suitable technical solution combined with their own business, there is no praise or criticism.

2. Common Websocket technologies

2.1 Java Websocket specification

This is the specification provided by JavaEE. Under the package Javax. webSocket, it contains both the client API and the server API. The server API is completely dependent on the client API, but some functions are added on top of it, so you only need to import the server dependency. The concrete implementation needs to be provided by a Web container, JavaEE server or framework. Common Web containers such as Tomcat and Undertow are supported.

Advantages: Easy to integrate, native Java support.

Disadvantages: High coupling with the shared container such as the Web server. You need to control broadcasting and multicast by yourself. Low concurrency, cumbersome tuning, compatibility problems.

2.2 SockJS

SockJS is a browser JavaScript library that abstracts Websocket. SockJS provides you with a consistent, cross-browser Javascript API that 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 that fails, it will try various other browser-specific transport protocols such as XHR-Streaming, Server Sent Events, and long polling. It is also often combined with STOMP (Message-Oriented Simple Text Protocol) to simplify its use. This protocol is used in Spring’s Websocket component.

Advantages: Active community, mature technology, rich protocol stack, full set of Spring solutions, strong compatibility, and can combine publish and subscribe mode.

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

2.3 Socket. IO

Socket.IO is a real-time application framework based on Node.js, which is widely used in instant messaging, notification and message push, real-time analysis and other scenarios. However, it provides netty-based server and client implementations, and supports Websocket and long polling. In addition to the common Websocket scenarios, we can use this component to implement android and IOS message push.

Advantages: Good performance, supporting broadcast, multicast, disconnection reconnection, heartbeat detection, binary. Supports Android and IOS platforms. The community is active.

Disadvantages: Need to self-encapsulate the integration with Spring, the server is not community maintenance, high resource consumption.

2.4 ReactiveStream

Several reaction flow specifications and frameworks also implement Websocket. Spring Webflux and RSocket are examples of this, and some official demos have been released.

Advantages: High throughput and high performance.

Disadvantages: relatively new technology, less learning materials.

3. Summary

Try the first option if your business is very light and urgent. The argument between SockJS and socket. IO is that the latter is better in terms of performance, of course, it consumes more resources, and supports push functionality on mobile terminals better. SockJS has the edge in Spring integration and overall solutions. If the pursuit of high performance, high throughput of Websocket, then undoubtedly the reaction formula is more appropriate, but the learning cost is relatively high. Other niche technology is not evaluated here, if you have a better solution can leave a message to discuss.

Bonus: Performance benchmarking

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

The full English version of the report can be accessed through the public account Nongxiaopangge reply ws.

Follow our public id: Felordcn for more information

Personal blog: https://felord.cn