Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Websockets and load balancing

Spring and Java EE have nice support for websockets. For example in Spring you can have:

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(new MyHandler(), "/myHandler")
            .addInterceptors(new HttpSessionHandshakeInterceptor());
    }

}

And with MyHandler class you can send and listen for messages to HTML5 websocket.

var webSocket = 
      new WebSocket('ws://localhost:8080/myHandler');
 webSocket.onmessage = function(event) {
      onMessage(event)
    };

The problem is if you run multiple servers behind a load balancer. The clients of server A will not be notified for events on server B.

This problem is solved in Spring by using message broker with Stomp protocol (http://assets.spring.io/wp/WebSocketBlogPost.html)

Since using the handler and "native" html5 websockets looks easier to me then the Stomp way, my questions are:

  1. Is it posible to use the message broker without the Stomp protocol?
  2. Are there any other options to overcome the load balancing issue?
like image 561
Evgeni Dimitrov Avatar asked Apr 20 '14 18:04

Evgeni Dimitrov


People also ask

How are WebSockets load balanced?

WebSocket load balancing is mutual problem of client and server. Without a hint from client, it is hardly possible to get 6D-tuple knowledge on the server side. A reasonable solution is to lift load balancing to the application level. It means that client application logic will be aware of server-side scaling.

Why WebSockets are not scalable?

But why are WebSockets hard to scale? The main challenge is that connections to your WebSocket server need to be persistent. And even once you've scaled out your server nodes both vertically and horizontally, you also need to provide a solution for sharing data between the nodes.

Does AWS ELB support WebSocket?

WebSockets on Amazon ALBAmazon ALB Listeners only offer HTTP or HTTPS protocol, but the good news is that WebSocket initially contacts the server with HTTP if you use ws:// or HTTPS if you use wss://. Your server will then reply with 101 Switching Protocols, telling the client to upgrade to a WebSocket connection.

Can I use WebSockets FOR REST API?

REST is the most commonly used architecture for developing APIs in recent years. It supports a half-duplex data transmission between the client and server. In the case of full-duplex data transmission, WebSockets are used.


1 Answers

The message brokers can be used without STOMP, take for example the RabbitMQ broker, it can be used with several other protocols; HTTP, AMQP, MQTT, AMQP. You can use them as a JMS implementation.

As there are several instances of the application, the best alternative is really having a central messaging broker for handling messages that need to be published to the clients of all applications.

Any alternative would imply doing something similar by hand, the servers in the backend need to communicate and notify each other of those events in some way. An alternative would be to write to the database certain events and have each server poll some table, but that is the type of design we try to avoid.

Also concerning load balancing and web sockets, the load balancer needs to be configured to allow forwarding of the HTTP Upgrade headers, which is usually off by default. For example nginx needs some configuration to allow forwarding of the these headers, otherwise Websockets won't work.

In that case SockJS will automatically use some of the fallback options that kick in when web sockets are not possible.

like image 121
Angular University Avatar answered Sep 25 '22 00:09

Angular University