Search code examples
javascriptangularjswebsocketspring-bootspring-websocket

Notify registered clients using Websockets with AngularJS (angular-websocket-service) and Spring Boot


I am new in AngularJS as well as FullStack development. The architecture of my current app is already set up and should not change preferably (for security reasons). So far, I can emit messages to the server using angular-websocket-service. Here is the code snippet of the service from the front-end:

proxiMiamApp.service('WebSocketService', function ($websocket) {
var wsEndpoint = {};

this.openWsEndpoint = function () {
    wsEndpoint = $websocket.connect("ws://localhost:9000/proximiamHandler");
    console.log(wsEndpoint);
    return wsEndpoint;
}

this.sendMessage = function(){
    if($.isEmptyObject(this.wsEndpoint)){
        this.openWsEndpoint();
    }

    eventUser = {

        idEvent : '1',
        idUser : '49'
    };

    wsEndpoint.register('/eventUser', function(){

        console.log('Register OK!');
    });
    console.log('Ready!');
    wsEndpoint.emit('/eventUser',eventUser);


}});

As for the back-end, I am using an implementation of the WebSocketHandler interface:

@Controller
public class ProximiamHandler implements WebSocketHandler {

@Override
public void afterConnectionEstablished(WebSocketSession webSocketSession) throws Exception {
    System.out.println("afterConntectionEstablished called");
}

@Override
public void handleMessage(WebSocketSession webSocketSession, WebSocketMessage<?> webSocketMessage) throws Exception {

    System.out.println("handleMessage called");
    // My code here...

}

@Override
public void handleTransportError(WebSocketSession webSocketSession, Throwable throwable) throws Exception {
    System.out.println("handleTransportError called");
}

@Override
public void afterConnectionClosed(WebSocketSession webSocketSession, CloseStatus closeStatus) throws Exception {
    System.out.println("afterConnectionClosed called");
}

@Override
public boolean supportsPartialMessages() {
    return true;
}}

The Implementation of the WebSocketHandler is called via Spring WebSocketConfigurer

@Configuration
@EnableWebSocket
@Controller
public class WebSocketConfig implements WebSocketConfigurer {

@Override
public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
    registry.addHandler(myHandler(), "/proximiamHandler").setAllowedOrigins("*");
}

@Bean
public WebSocketHandler myHandler() {
    return new ProximiamHandler();
}}

My questions are:

  1. Can I notify subscribed clients using this architecture?
  2. If yes, how can I do it?
  3. Is there a way to return something to subscribed clients from the server? (an Object or a String for instance)

Thanks in advance for your help


Solution

  • Can I notify subscribed clients using this architecture? => Yes.

    If yes, how can I do it? => Based on the Spring web socket APIs, you have to retain the ' WebSocketSession' passed to you via "afterConnectionEstablished" callback. Use the sendMessage() API of Web socket session to send notifications to client.

    Is there a way to return something to subscribed clients from the server? (an Object or a String for instance) => You can format your data in either JSON or XML & wrap it using "WebSocketMessage" and pass it to client.

    I never worked on spring, however, I am answering this based on my knowledge on web socket. See if it helps.