Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

316
Vistas
What is MultiServerUserRegistry in spring websocket?

In package org.springframework.messaging.simp.user there is a class MultiServerUserRegistry.

This class looks like it would work on multi-server application, but I could not find any documentation that could help me understand how it works or how should I use it.

What does this class do and how do I use it? How do I use websocket to work on multi-server application?

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

The default publish mechanism use a local sessions repository to resolve socket id for a user.
If you are setting up a cluster, you can't use this strategy. We need to use a remote repository.
The removeRepository already exists in Spring as MultiServerUserRegistry. To active it you just have to configure your MessageBrokerRegistry :

registry.enableStompBrokerRelay("/topic/", "/queue/", "/exchange/")
                .setUserDestinationBroadcast("/topic/unresolved-user")
                .setUserRegistryBroadcast("/topic/user-registry")

It works fine for me. I hope that it will help.

about 4 years ago · Santiago Trujillo Denunciar

0

According to the MultiServerUserRegistry's JavaDoc, it's an implementation of SimpUserRegistry, which enable us to look up both local and remote user registries.

SimpUserRegistry that looks up users in a "local" user registry as well as a set of "remote" user registries. The local registry is provided as a constructor argument while remote registries are updated via broadcasts handled by UserRegistryMessageHandler which in turn notifies this registry when updates are received.

Another SimpUserRegistry's implementation is DefaultSimpUserRegistry, which could only get local user.

I read the related source code knowing that when we config setUserRegistryBroadcast in the enableStompBrokerRelay, a MultiServerUserRegistry will be created, and we can get all the users' online state regardless which server they connected in cluster.

Here is the key source code in AbstractMessageBrokerConfiguration:

@Bean
@SuppressWarnings("deprecation")
public SimpUserRegistry userRegistry() {
    SimpUserRegistry registry = createLocalUserRegistry();
    if (registry == null) {
        registry = createLocalUserRegistry(getBrokerRegistry().getUserRegistryOrder());
    }
    boolean broadcast = getBrokerRegistry().getUserRegistryBroadcast() != null;
    return (broadcast ? new MultiServerUserRegistry(registry) : registry);
}

As this function annotated with @Bean, we can get the SimpUserRegistry instance by @Autowired:

@Autowired
private SimpUserRegistry simpUserRegistry;

Using this interface, we have the ability to:

/**
 * Get the user for the given name.
 * @param userName the name of the user to look up
 * @return the user, or {@code null} if not connected
 */
@Nullable
SimpUser getUser(String userName);

/**
 * Return a snapshot of all connected users.
 * <p>The returned set is a copy and will not reflect further changes.
 * @return the connected users, or an empty set if none
 */
Set<SimpUser> getUsers();

/**
 * Return the count of all connected users.
 * @return the number of connected users
 * @since 4.3.5
 */
int getUserCount();

/**
 * Find subscriptions with the given matcher.
 * @param matcher the matcher to use
 * @return a set of matching subscriptions, or an empty set if none
 */
Set<SimpSubscription> findSubscriptions(SimpSubscriptionMatcher matcher);

We can use it to get the count of all the online users (by getUserCount) in our whole cluster, and detect whether a specific user is online or not (by getUser).

As my practice, the following message broker configure is suggested in multi-server scenario:

@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
    registry.setApplicationDestinationPrefixes("/app");
    registry.enableStompBrokerRelay("/topic", "/queue")
            .setUserDestinationBroadcast("/topic/log-unresolved-user")
            .setUserRegistryBroadcast("/topic/log-user-registry")
            .setRelayHost(brokerConfig.getHost())
            .setRelayPort(brokerConfig.getPort())
            .setVirtualHost(brokerConfig.getVirtualHost())
            .setClientLogin(brokerConfig.getUsername())
            .setClientPasscode(brokerConfig.getPassword())
            .setSystemLogin(systemUsername)
            .setSystemPasscode(systemPassword);
}
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda