STOMP(Simple Text Oriented Messaging Protocol)๋?
์น์์ผ ์์์ ๋์ํ๋ ์๋ธ ํ๋กํ ์ฝ๋ก, ํด๋ผ์ด์ธํธ์ ์๋ฒ๊ฐ ์๋ก ํต์ ํ๋๋ฐ ์์ด ๋ฉ์์ง์ ํ์, ์ ํ, ๋ด์ฉ ๋ฑ์ ์ ์ํด์ฃผ๋ ํ๋กํ ์ฝ์ด๋ค.
์น ์์ผ ํ๋กํ ์ฝ์ Text ๋๋ binary ๋๊ฐ์ง ์ ํ์ ๋ฉ์์ง ํ์ ์ ์ ์ํ์ง๋ง ๋ฉ์์ง์ ๋ด์ฉ์ ๋ํด์๋ ์ ์ํ์ง ์๋๋ค. ์ฆ, ์น ์์ผ๋ง ์ฌ์ฉํด์ ๊ตฌํํ๊ฒ ๋๋ฉด ํด๋น ๋ฉ์์ง๊ฐ ์ด๋ค ์์ฒญ์ธ์ง, ์ด๋ค ํฌ๋งท์ผ๋ก ์ค๋์ง ์ ํด์ ธ์์ง ์์ ์ผ์ผ์ด ๊ตฌํํด์ผํ๋ค.
์ด๋ STOMP๋ผ๋ ํ๋กํ ์ฝ์ ์๋ธ ํ๋กํ ์ฝ๋ก ์ฌ์ฉํ์ฌ, ๋จ์ํ binary, text๊ฐ ์๋๋ผ ๊ท๊ฒฉ์ ๊ฐ์ถ ๋ฉ์์ง๋ฅผ ๋ณด๋ผ ์ ์๋ค.
๋ํ ๊ธฐ๋ณธ์ ์ผ๋ก pub/sub ๊ตฌ์กฐ๋ก ๋์ด์์ด ๋ฉ์์ง๋ฅผ ์ ์กํ๊ณ ๋ฉ์์ง๋ฅผ ๋ฐ์ ์ฒ๋ฆฌํ๋ ๋ถ๋ถ์ด ํ์คํ ์ ํด์ ธ ์๊ธฐ ๋๋ฌธ์ ๊ฐ๋ฐ์ ์ ์ฅ์์ ๋ช ํํ๊ฒ ์ธ์งํ๊ณ ๊ฐ๋ฐํ ์ ์๋ ์ด์ ์ด ์๋ค.
STOMP๋ฅผ ์ด์ฉํ๋ฉด ๋ฉ์ธ์ง์ ํค๋์ ๊ฐ์ ์ค ์ ์์ด ํค๋ ๊ฐ์ ๊ธฐ๋ฐ์ผ๋ก ํต์ ์ ์ธ์ฆ ์ฒ๋ฆฌ๋ฅผ ๊ตฌํํ๋ ๊ฒ๋ ๊ฐ๋ฅํ๋ฉฐ, STMOP ์คํ์ ์ ์ํ ๊ท์น๋ง ์ ์งํค๋ฉด ์ฌ๋ฌ ์ธ์ด ๋ฐ ํ๋ซํผ๊ฐ ๋ฉ์์ง๋ฅผ ์ํธ ์ด์ํ ์ ์๋ค.
์คํ๋ง์ spring-websocket ๋ชจ๋์ ํตํด์ STOMP๋ฅผ ์ ๊ณตํ๊ณ ์๋ค.
ํ์
ํ์
STOMP๋ HTTP์์ ๋ชจ๋ธ๋ง๋๋ Frame ๊ธฐ๋ฐ ํ๋กํ ์ฝ์ด๋ค. Frame์ ๋ช ๊ฐ์ Test line์ผ๋ก ์ง์ ๋ ๊ตฌ์กฐ์ธ๋ฐ ์ฒซ๋ฒ์งธ ๋ผ์ธ์ Text์ด๊ณ , ์ดํ Key:Value ํํ๋ก Header์ ์ ๋ณด๋ฅผ ํฌํจํ๋ค. ๊ทธ ๋ค์ ๋น ๋ผ์ธ์ ์ถ๊ฐํ๊ณ Payload๊ฐ ์กด์ฌํ๋ค.
์๋ ํ์์ ๋ณด๋ฉด HTTP์ ์ ์ฌํ๋ค๋ ๊ฒ์ ์ ์ ์๋ค.
COMMAND
header1:value1
header2:value2
Body^@
-
COMMAND
- ํด๋ผ์ด์ธํธ๋ ๋ฉ์์ง๋ฅผ ์ ์กํ๊ธฐ ์ํด COMMAND๋ก SEND ๋๋ SUBSCRIBE ๋ช ๋ น์ ์ฌ์ฉํ๋ค
- MESSAGE COMMAND๋ ๋ชจ๋ ๊ตฌ๋ ์์๊ฒ ๋ฉ์ธ์ง๋ฅผ ๋ธ๋ก๋์บ์คํ ํ ๋ ์ฌ์ฉํ๋ค.
- ๊ทธ์ธ ๋ค์๊ณผ ๊ฐ์ COMMAND๊ฐ ์๋ค
- UNSUBSCRIBE, BEGIN, COMMIT, ABORT, ACK, NACK, DISCONNECT
-
header, value
-
๋ฉ์์ง์ ์์ ๋์๊ณผ ๋ฉ์์ง์ ๋ํ ์ ๋ณด๋ฅผ ์ค๋ช ํ๋ค.
-
๊ธฐ์กด ์น์์ผ๋ง์ผ๋ก๋ ํํํ ์ ์๋ ํ์์ด๋ค.
-
destination : ํค๋ ์ค ํ๋๋ก, ์ด ํค๋๋ก ์ง์ ํ ์ฃผ์๋ก ๋ฉ์ธ์ง๋ฅผ ๋ณด๋ด๊ฑฐ๋ ๊ตฌ๋ ํ ์ ์๋ค.
-
destination์ ์๋์ ์ผ๋ก ์ ๋ณด๋ฅผ ๋ถ๋ถ๋ช ํ๊ฒ ์ ์ํ์๋๋ฐ, ์ด๋ STOMP ๊ตฌํ์ฒด์์ ๋ฌธ์์ด ๊ตฌ๋ฌธ์ ๋ฐ๋ผ ์ง์ ์๋ฏธ๋ฅผ ๋ถ์ฌํ๋๋ก ํ๊ธฐ ์ํจ์ด๋ค.
-
์ผ๋ฐ์ ์ผ๋ก ๋ค์์ ํ์์ ๋ฐ๋ฅธ๋ค.
"/topic/..." : publish - subscribe(1:N) "/queue/..." : point-to-point (1:1)
-
-
๋ฉ์์ง ๋ธ๋ก์ปค
publisher๋ก๋ถํฐ ์ ๋ฌ ๋ฐ์ ๋ฉ์์ง๋ฅผ subscriber๋ก ์ ๋ฌํด์ฃผ๋ ์ค๊ฐ์ญํ ์ ์ํํ๋ค. ๋์๋ฐฉ์์ ๋ค์๊ณผ ๊ฐ๋ค.
-
๊ฐ๊ฐ A, B, C๋ผ๋ ์ ์ ๊ฐ ์ฐจ๋ก๋ก 5๋ฒ๋ฐฉ์ ์ ์ฅํ๋ค.
SUBSCRIBE destination:/subscribe/chat/room/5 ^@- ์ ์ ๊ฐ ์ ์ฅํ๋ฉด์ 5๋ฒ๋ฐฉ์ ๋ํ ๊ตฌ๋ ์ ํ๋ค.
- ๋ฉ์์ง ๋ธ๋ก์ปค๋ ํด๋ผ์ด์ธํธ์ SUBSCRIBE ์ ๋ณด๋ฅผ ์์ฒด์ ์ผ๋ก ๋ฉ๋ชจ๋ฆฌ์ ์ ์งํ๋ค.
-
A๊ฐ 5๋ฒ๋ฐฉ์์ ์ฑํ ์ ์ ์กํ๋ค.
SEND content-type:application/json destination:/publish/chat {"chatRoomId":5,"type":"MESSAGE","writer":"clientB"}^@ -
5๋ฒ๋ฐฉ ๋ฉ์์ง ๋ธ๋ก์ปค๊ฐ ๋ฉ์์ง๋ฅผ ๋ฐ๋๋ค.
-
5๋ฒ๋ฐฉ ๋ฉ์์ง ๋ธ๋ก์ปค๊ฐ 5๋ฒ๋ฐฉ ๊ตฌ๋ ์(A, B, C)์๊ฒ ๋ฉ์์ง๋ฅผ ์ ์กํ๋ค.
Spring์์ STOMP๋ก ๊ตฌํ
์น ์์ผ์ STOMP, ๋ฉ์์ง ๋ธ๋ก์ปค ์ฐ๋
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
registry.enableSimpleBroker("/subscribe");
registry.setApplicationDestinationPrefixes("/publish");
}
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
registry.addEndpoint("/ws-connection")
.setAllowedOrigins("*")
.withSockJS();
}
}- @EnableWebSocketMessageBroker
- STOMP๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด ์ ์ธํ๋ ์ ๋ ธํ ์ด์
- WebSocketMessageBrokerConfigurer
- ๋ฉ์์ง ๋ธ๋ก์ปค๊ฐ ์ง์ํ๋ ์น์์ผ ๋ฉ์์ง ์ฒ๋ฆฌ๋ฅผ ํ์ฑํํ๋ค.
- configureMessageBroker()
- ์ธ ๋ฉ๋ชจ๋ฆฌ ๊ธฐ๋ฐ์ Simple Message Broker๋ฅผ ํ์ฑํํ๋ค.
- RabbitMQ, ActiveMQ ๊ฐ์ ์ธ๋ถ ๋ฉ์ธ์ง ์์คํ ์ ์ฌ์ฉํ ์ ์๋ค.
- ๋ฉ์์ง ๋ธ๋ก์ปค๋
enableSimpleBroker()๋ก ์ค์ ํ ๊ฐ์ผ๋ก ์์ํ๋ ์ฃผ์์ subscriber๋ค์๊ฒ ๋ฉ์์ง๋ฅผ ์ ๋ฌํ๋ ์ญํ ์ ํ๋ค. ์์์์๋/subscribe๋ก ์ค์ ํ์ฌ/subscribe๋ก ๋ค์ด์จ ์์ฒญ์ ๊ตฌ๋ ์ผ๋ก ์ ํด์ง๋ค. - ๋ง์ฐฌ๊ฐ์ง๋ก
setApplicationDestinationPrefixes()๋ก ์ค์ ํ ๊ฐ์ publish๋ฅผ ์ํด ์ฌ์ฉ๋๋ค,
- ์ธ ๋ฉ๋ชจ๋ฆฌ ๊ธฐ๋ฐ์ Simple Message Broker๋ฅผ ํ์ฑํํ๋ค.
- registerStompEndpionts()
- HandShake์ ํต์ ์ ๋ด๋นํ EndPoint๋ฅผ ์ง์ ํ๋ค.
- ์์์์๋ ํด๋ผ์ด์ธํธ์์ ์๋ฒ๋ก WebSocket ์ฐ๊ฒฐ์ ํ๊ณ ์ถ์ ๋,
/ws-connection์ผ๋ก ์์ฒญ์ ๋ณด๋ด๋๋ก ์ค์ ํ์๋ค.
ChatRequest DTO
public class ChatRequest {
private Long senderId;
private Long receiverId;
private Long roomId;
private String message;
}Controller
@RestController
@RequiredArgsConstructor
public class ChattingController {
private final ChattingService chattingService;
private final SimpMessagingTemplate simpMessagingTemplate;
public ChattingController(ChattingService chattingService, SimpMessagingTemplate simpMessagingTemplate) {
this.chattingService = chattingService;
this.simpMessagingTemplate = simpMessagingTemplate;
}
@MessageMapping("/messages")
public void chat(@Valid ChatRequest chatRequest) {
chattingService.save(chatRequest);
simpMessagingTemplate.convertAndSend("/subscribe/rooms/" + chatRequest.getRoomId(), chatRequest.getMessage());
}
}-
SimpMessagingTemplate
@EnableWebSocketMessageBroker์ ํตํด์ ๋ฑ๋ก๋๋ ๋น์ด๋ค. ๋ธ๋ก์ปค๋ก ๋ฉ์์ง๋ฅผ ์ ๋ฌํ๋ค.
-
@MessageMapping
-
ํด๋ผ์ด์ธํธ๊ฐ SEND ํ ์ ์๋ ๊ฒฝ๋ก.
-
WebSocketConfig ์์ ๋ฑ๋กํ
setApplicationDestinationPrefixes()์ฃผ์์ ํฉ์ณ์ง๋ค/publish/messages
-
ํด๋ผ์ด์ธํธ ์ฐ๋
function connect() {
var socket = new WebSocket('/ws-connection');
stompClient = Stomp.over(socket);
stompClient.connect({}, function () {
setConnected(true);
stompClient.subscribe('/subscribe/rooms/5', function (greeting) {
console.log(greeting.body);
});
});
}
function sendMessage() {
stompClient.send("/publish/messages", {}, JSON.stringify({
'message': $("#name").val(),
'senderId': 7,
'receiverId': 14,
'roomId': 5
}));
}์ถ์ฒ
https://tecoble.techcourse.co.kr/post/2021-09-05-web-socket-practice/