# Sending Message To Specific Anonymous User On Spring WebSocket


In my current, I had the opportunity to develop a new application based on [Vue.js](https://vuejs.org/) in the frontend and [Spring Boot](https://spring.io/projects/spring-boot) in the backend. The backend should send updates to the frontend via a WebSocket connection so that users do not need to refresh the website to see the latest information.

The view should show a list of transfers where the user can modify the results by using filters and pagination. As a requirement, each user should receive specific results based on his filters and pagination without broadcasting this to all other connected users. The user does not need to authenticate itself at the backend.

Most tutorials cover either the case of broadcasting messages to all connected users or to send messages to certain authenticated users. In this article, I will demonstrate how to send messages to anonymous users without broadcasting the messages.

## The Demo Project

I have a created a [demo project](https://github.com/Mokkapps/spring-boot-websocket-anonymous-messages-demo) to be able to demonstrate the functionality. It is a simple monorepo which contains a backend and a frontend folder.

## Backend

The Spring Boot backend was bootstrapped using [Spring Initializr](https://start.spring.io/) where I chose `WebSocket` as the only dependency:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1601387053021/PulhhByFp.png)

### Configure Websocket

The next step is to configure the application to use a WebSocket connection. To configure the Spring Boot application I followed [this tutorial](https://spring.io/guides/gs/messaging-stomp-websocket/) without the frontend part.

After this tutorial we have a working WebSocket controller that receives and sends messages via a WebSocket connection:

The `greeting()` method is called if a message is sent to the `/hello` destination. This is ensured by using the `@MessageMapping` annotation. The received message is then sent to `/ topic/greetings`. I have added a simulated delay to simulate any asynchronous operation that could be executed on the server-side in between receiving and sending messages.

In this implementation, all messages are broadcasted to all connected users by using the `@SendTo` annotation.

`Greeting.java` and `HelloMessage.java` are simple Java classes which represent the transferred data objects:

The WebSocket is configured in `WebSocketConfig.java`:

In my project, we send updates to the clients via a scheduled interval so I also added this functionality to this demo project. The first step is to enable scheduling in the Spring Boot application using the `@EnableScheduling` annotation:

Next, a `Scheduler.java` class handles the scheduled tasks and triggers `GreetingService` to send a new message each second:

`GreetingsService.java` injects `SimpMessagingTemplate` which provides methods to programmatically send WebSocket messages:

`convertAndSend` is equivalent to the `@SendTo` annotation which was used in the controller to broadcast messages.

## Frontend

The frontend was bootstrapped using the [Vue CLI](https://cli.vuejs.org):

```
npm install -g @vue/cli vue create frontend
```


Next step is to add [STOMP.js](https://github.com/stomp-js/stompjs) as npm library which allows us to connect to our backend STOMP broker over WebSocket:

I have created a `websocket-service.ts` as Singleton which handles the interaction with this library:

In the constructor, the client configuration is done. If we run the backend locally the WebSocket connection is available at `localhost:8080/ws` that's why `ws://localhost:8080/ws` is used as broker URL in Vue development mode.

The service provides this public API:

Inside the `mounted()` method in `App.vue` the service is instantiated:

Received messages are rendered in the template:

At this point we have a running application that can send & broadcast messages via a WebSocket connection:

![Broadcasting message demo](https://cdn.hashnode.com/res/hashnode/image/upload/v1601387054834/pr8zi4O4I.gif)*Broadcasting message demo*

## Prevent Message Broadcasting

As you can see in the video above, each connected user receives the same broadcasted message as we cannot identify certain users. In this chapter, I want to demonstrate how to prevent broadcasting messages to all users without a need for authentication.

The idea is to use UUIDs for each connected client and instead of broadcasting to all users messages are only sent to specific UUIDs.

These steps need to be performed:

1. Generate a Spring Security `Principal` name by UUID for each newly connected client by using `DefaultHandshakeHandler`

1. Store the UUID if a new message is received

1. Use `@SendToUser` instead of `@SendTo` annotation in the WebSocket controller

1. Change endpoint in frontend to have the `user`, so `/user/topic/greetings` instead of `/topic/greetings`;

Let’s start by creating a `CustomHandshakeHandler.java`

which needs to be registered in `WebSocketConfig.java`:

Now `GreetingController.java` has to be adopted:

`GreetingService` needs to be adjusted to be able to store the UUIDs in a list and we change `convertAndSend` to `convertAndSendToUser` where we iterate over all user names and send message to them:

Finally, the topic endpoint in `App.vue` in the frontend code needs to be changed:

```
private readonly webSocketGreetingsSubscribeEndpoint = '/user/topic/greetings' ;
```


Let’s see this in action:

![Anonymous message demo](https://cdn.hashnode.com/res/hashnode/image/upload/v1601387057324/CvCL3Asdg.gif)*Anonymous message demo*

## Conclusion

Sending WebSocket messages to specific anonymous users is not hard using Spring. You can also extend this mechanism by adding another destination for broadcasted messages. This way, you can send certain messages to specific users and also broadcast messages to every connected user.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1601387059255/0-5D-CPH5.jpeg)

*Originally published at [https://www.mokkapps.de](https://www.mokkapps.de/blog/sending-message-to-specific-anonymous-user-on-spring-websocket/).*
