Backend Development 8 min read

Which Real‑Time Push Method Fits Your App? Polling, SSE, WebSocket, Netty & MQTT Compared

This article reviews common real‑time message push techniques—including short and long polling, Server‑Sent Events, WebSocket, Netty, and MQTT—explaining their implementation ideas, core code snippets, advantages, and drawbacks so you can choose the most suitable solution for your business scenario.

Lobster Programming
Lobster Programming
Lobster Programming
Which Real‑Time Push Method Fits Your App? Polling, SSE, WebSocket, Netty & MQTT Compared

Real‑time message push is everywhere: sales dashboards, unread notifications, game skill releases, or new comments on video platforms. Different companies adopt different implementations.

1. Short Polling

Implementation idea: the front‑end uses a timer (setInterval) to send periodic requests; the back‑end returns data if available, otherwise an empty response.

Core code (illustrated in the image) shows the request‑response flow.

Problems: many requests may return no data, wasting server resources and causing high load under heavy traffic.

2. Long Polling

Implementation idea: the front‑end sends a request that the back‑end holds (suspends) until new data is available, then responds.

Problems: if no new data arrives, the request may time out; under high concurrency the many held connections can exhaust server threads and increase pressure.

3. Server‑Sent Events (SSE)

Implementation idea: the front‑end initiates a request; the back‑end sets the response content type to text/event-stream to establish a long‑living one‑way channel. When data changes, the server pushes it to the client instantly.

SSE subscription commonly uses the following headers:

<code>Accept: text/event-stream  // indicates client can receive event streams
Cache-Control: no-cache   // disables any event caching
Connection: keep-alive    // keeps the connection persistent</code>

Core code (Spring example):

<code>/**
  * 创建连接sse
  */
public SseEmitter connect() {
    final String clientId = UUID.randomUUID().toString().replace("-", "");
    SseEmitterUTF8 sseEmitter = new SseEmitterUTF8(0L);
    try {
        sseEmitter.send(SseEmitter.event().comment("创建连接成功 !!!"));
    } catch (Exception e) {
        logger.error("创建连接失败 , {} ", e.getMessage());
    }
    sseEmitter.onCompletion(() -> {
        logger.info("connect onCompletion , {} 结束连接 ...", clientId);
        removeClient(clientId);
    });
    sseEmitter.onTimeout(() -> {
        logger.info("connect onTimeout , {} 连接超时 ...", clientId);
        removeClient(clientId);
    });
    sseEmitter.onError((throwable) -> {
        logger.error("connect onError , {} 连接异常 ...", clientId);
        removeClient(clientId);
    });
    sseCache.put(clientId, sseEmitter);
    return sseEmitter;
}</code>

SSE is a one‑way HTTP‑based communication suitable for frequent low‑latency updates such as chat, monitoring dashboards, or news feeds.

4. WebSocket

Implementation principle: the client opens a long‑living request; the server accepts and upgrades it to a bidirectional channel.

Client request example:

<code>#请求建立长链接
new WebSocket("ws://192.168.27.201:8080/ws/connect")</code>

Server implementation (Spring + Java WebSocket API):

<code>#建立连接、接受数据、关闭连接
@ServerEndpoint("/ws/connect") // similar to @RequestMapping
@Component
public class WebsocketServer {
    public static ConcurrentHashMap<String,Session> clintsMap = new ConcurrentHashMap<>();

    @OnOpen
    public void onOpen(Session session, @PathParam("token")String token){
        System.out.println("客户端开始建立连接:"+token);
        clintsMap.put(token, session);
    }

    @OnMessage
    public void onMessage(String msg, @PathParam("token")String token){
        System.out.println("客户端:"+token + "发送信息给服务器:" + msg);
    }

    @OnClose
    public void onClose(@PathParam("token")String token){
        System.out.println("客户端关闭连接:"+token);
        clintsMap.remove(token);
    }

    @OnError
    public void onError(Throwable e){
        System.out.println("客户端连接发生异常");
    }
}

@Configuration
public class WebSocketConfiguration {
    @Bean
    public ServerEndpointExporter serverEndpointExporter() {
        return new ServerEndpointExporter();
    }
}</code>

5. Netty Real‑Time Push

Netty provides an NIO‑based framework that encapsulates WebSocket, making it easier to handle high‑concurrency scenarios with better performance.

6. MQTT

MQTT (Message Queuing Telemetry Transport) is a lightweight publish‑subscribe protocol ideal for constrained devices and unreliable networks. It enables bidirectional communication: devices subscribe to topics on an MQTT broker, and any published message is instantly delivered to all subscribers.

MQTT defines three QoS levels to guarantee delivery reliability:

QoS 0 : at most once, possible loss if client offline.

QoS 1 : at least once, may deliver duplicates.

QoS 2 : exactly once.

Summary : For simple, low‑traffic scenarios short or long polling may suffice. For higher traffic or more interactive apps, WebSocket or Netty is preferable. In IoT contexts, MQTT provides reliable, lightweight push. Choose the method that matches your business requirements and load characteristics.

real-timemessage-pushWebSocketMQTTpollingSSE
Lobster Programming
Written by

Lobster Programming

Sharing insights on technical analysis and exchange, making life better through technology.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.