Simple Text Oriented Messaging Protocol
간단한 메시지를 전송하기 위한 프로토콜
HTTP에서 모델링되는 Frame 기반 프로토콜
메시지 브로커와 publisher(발행자)/subscriber(구독자) 방식을 사용해,
메시지를 보내는 사람과 받는 사람이 구분되어 있다.
메시지 브로커 : 발행자가 보낸 메시지를 구독자에게 전달해주는 역할
TCP, WebSocket 같은 양방향 네트워크 프로토콜 기반으로 동작하며, 메시지 교환을 단순화, 표준화한다.
Command - Header - Body로 이루어져 있다.
COMMAND
header1:value1
header2:value2
Body^@
subscriber(수신자/구독자) : /topic(= /sub) 경로 구독
publisher(송신자/발행자) : /app(= /pub) 또는 /topic으로 메시지 전송
publisher -> Request Channel -> /topic -> StompBrokerRelay(MessageHandler) -> ResponseChannel -> subscriber
publisher -> Request Channel -> /app -> SimAnnotationMethod(MessageHandler) -> Broker Channel -> StompBrokerRelay(MessageHandler) -> Response Channel -> subscriber
Spring framework의 통합된 Messaging 어플리케이션을 위한 지원을 한다.
채팅방 생성 : topic을 생성한다.
채팅방 입장 : topic을 구독한다.
//채팅방 1에 대한 구독
SUBSCRIBE
destination: /topic/chat/room/1
id: sub-1
^@
채팅방에서 메시지 송수신 : 해당 topic으로 메시지를 송신(pub), 수신(sub)한다.
//ClientA에서 채팅 메시지를 송신할 때
SEND
destination: /app/chat
content-type: application/json
{"chatRoomId":1, "type":"MESSAGE", "sender":"clientA"}^@
STOMP 서버가 모든 구독에서 메시지를 Boradcasting하기 위해 MESSAGE COMMAND를 사용한다.
서버는 메시지를 전송할 Borker에 전달한다. 이때 서버는 불분명한 메시지를 전송할 수 없기 때문에 서버의 모든 메시지는 특정 클라이언트 구독에 응답해야한다. 또, 서버 메시지의 subscription-id 헤더는 클라이언트 구독의 id 헤더와 일치해야한다.
//서버가 모든 구독자에게 Broadcasting
MESSAGE
destination: /topic/chat/room/1
message-id: {message id}
subscription: sub-1
{"chatRoomId":1, "type":"MESSAGE", "sender":"clientA"}^@
다음 포스팅에서는 STOMP를 어떻게 구현했는지, Spring에서의 동작 과정에 대해 설명할 것이다. 🍀