I have been trying to grasp fully the concept of WebSocket and i stumbled
messagingTemplate.convertAndSendToUser();
and
messagingTemplate.convertAndSend()
please what is this class and how can it be used ?
I have been trying to grasp fully the concept of WebSocket and i stumbled
messagingTemplate.convertAndSendToUser();
and
messagingTemplate.convertAndSend()
please what is this class and how can it be used ?
From wiki, https://en.wikipedia.org/wiki/WebSocket:
WebSocket is a computer communications protocol, providing full-duplex communication channels over a single TCP connection. The WebSocket protocol was standardized by the IETF as RFC 6455 in 2011, and the WebSocket API in Web IDL is being standardized by the W3C. WebSocket is a different TCP protocol from HTTP.
And from http://blog.teamtreehouse.com/an-introduction-to-websockets:
WebSockets provide a persistent connection between a client and server that both parties can use to start sending data at any time. The client establishes a WebSocket connection through a process known as the WebSocket handshake. This process starts with the client sending a regular HTTP request to the server.
In a typical spring based web app, on the HTML (javascript) side, EventSource constructor takes argument for a REST api endpoint. This REST API endpoint returns a SSEemitter and application keeps a handle to that SSEmitter. Then onwards, EventSource's onMessage is called whenever send method on SSEmitter is called.
There would be similar ways of communication across languages and frameworks.
Another way within spring framework to use webSocket is like a HTTP based message broker by using MessageSendingOperations on which convertAndSend method is exposed. With this method again, message is converted with the help of the passed converter and is then sent to the websocket.
@Autowired
private final MessageSendingOperations<String> messagingTemplate;
....
this.messagingTemplate.convertAndSend(
"/data", new Random().nextInt(100));
On the consuming end, subscriber consumes the message.
To endwith, websocket is just a communication protocol. It doesn’t define things like - How to send a message only to users who are subscribed to a particular topic, or how to send a message to a particular user. There are other protocols for that, for example: STOMP. You could refer to a useful blog at: http://kimrudolph.de/blog/spring-4-websockets-tutorial.