XMPP
XMPP 란?
XMPP 는’Extensible Messaging and Presence Protocol’의 약자이다. 풀어보면 확장 가능한 메시지 및 존재(상태)를 정의한 규약쯤 될 것 같다. 쉽게 말해서 메시징 관련 프로토콜이다. 1999년 오픈소스 커뮤니티인 Jabber에 의해서 만들어진 프로토콜이라고 한다. 현재 Messsaging protocol 표준이며, 대표적으로 Google Talk, Facebook chat등에 이용되고 있다. (하지만, Facebook은 2015/04/30 이후로 deprecate, Google Talk역시 Hangout으로 넘어가면서 XMPP 지원을 하지 않는듯 하다. Slack은 XMPP gateway를 서드파티 메시징 클라이언트를 위해 지원하는 듯.)
XMPP는 XML기반으로 통신이 이루어진다. 이걸 보는 순간 ‘느린거 아닌가?’ 하는 생각이 들었다. Facebook이나 Google에서 deprecate시키는걸 보면, XML기반이라는 이유가 한몫 하지 않을까라고 생각된다. 다음은 Wikipedia에 정리되어 있는 장점 및 단점이다.
Strengths
Decentralization - 마스터 서버가 존재하지 않음.
Open Standards - 자유로이 이용가능 함. 관련 라이브러리도 풍부
History - 1999년 부터 사용되었기 때문에 여러 client, server, component, library 가 구현되어있음.
Security - XMPP 서버는 public XMPP 네트워크로부터 격리 시킬수 있고, SALS과 TLS 지원 함
Flexibility - XML 기반의 프로토콜이라 확장이 용이하며, custom application 구축이 쉬움
Weaknesses
Does not support Quality of Service (Qos) - 메시지가 잘 전송되었는지 따로 구현해야 함.
Text-based communication - text 기반의 통신이다. 순수 바이너리 솔루션에 비해 오버헤드가 크다.
In-band binary data transfer is limited - 바이너리 데이터를 base64인코딩해서 단일 XML에 첨부해야 함-> 비효율적
Does not support end-to-end encryption
특징은 대략 이 정도로 정리가 된다.
Jabber ID
Jabber ID는 Jabber 네트워크상의 모든 유저가 가지고 있는 유일한 ID 이다. (줄여서 JID라고 불린다.) 마스터서버가 없기 때문에 Jabber ID는 마치 이메일 주소와 같은 형태를 가진다.
JID의 형태 : username@domain/resource
ex) kkw0528@nhnent.com/iPhone
resource파트를 가지고 있어서 한 유저가 각각 다른 기기로 접속할 수 있다.
Stream 과 Stanza
Stream
두 피어간의 open-ended XML 요소를 말한다. 반드시 Root요소 이어야 하며, Stream 내부에 다수의 Stanza가 올 수 있다.
Stanza
XML형식에 맞는 완전한 XML요소이다. Stream 내부에 포함되어서 전달된다. 대표적으로 presence, message, iq가 있다. message는 말그대로 메시징에 이용하는 Stanza이고, IQ는 get, set, result, error 등의 타입이 있다. get, set은 요청, result, error는 응답에 해당한다. 마지막으로 presence는 상대방이 메시지를 받을수 있는지 여부를 의미한다.
다음으로 message, iq, presence stanza에 대해 알아보자.
- Message stanza
<message to="romeo@gmail.com" from="juliet@gmail.com" type="chat" xml:lang="en">
<body>Wherefore art thou, Romeo?</body>
</message>
잘 알다시피 to는 수신자, from은 발신자를 의미한다. type은 메시지를 구별하는데 이용하며 chat 이외에도 error, groupchat, headline등의 type이 있다. xml:lang은 language에 대한 정보이다. 실제 메시지 내용은 <body></body> 안에 위치한다.
- IQ stanza
<iq from="juliet@gmail.com" type="get" id="roster_1">
<query xmlns="jabber:iq:roster"/>
</iq>
juliet@gmail.com으로 부터 roster_1정보를 가져오라는 요청을 담은 IQ stanza이다. 이에 대한 응답으로 다음과 같은 roster(친구 정보)를 가져오게 된다.
<iq to="juliet@gmail.com" type="result" id="roster_1">
<query xmlns="jabber:iq:roster">
<item jid="romeo@gmail.com" name="Romeo" subscription="both">
<group>Friends</group>
</item>
<item jid="mercutio@gmail.com" name="Mercutio" subscription="from">
<group>Friends</group>
</item>
<item jid="benvolio@gmail.com" name="Benvolio" subscription="both">
<group>Friends</group>
</item>
</query>
</iq>
- Presence stanza
<presence xml:lang="en">
<show>dnd</show>
<status>Good night, good night! parting is such sweet sorrow, that I shall say good night till it be morrow.</status>
</presence>
클라이언트는 자신의 상태를 presence stanza로 서버에 보내야 한다. show 태그 안에 상태를 표시한다. 상태의 종류는 다음과 같다.
chat - 채팅 가능한 상태.
dnd - Do not disturb.
away - 자리비움(잠시)
xa - 자리비움(오랫동안)
좀더 상세한 상태는
<presence from="romeo@gmail.com" to="juliet@gmail.com" xml:lang="en">
<show>dnd</show>
<status>Good night, good night! parting is such sweet sorrow, that I shall say good night till it be morrow.</status>
</presence>
<presence from="romeo@gmail.com" to="mercutio@gmail.com" xml:lang="en">
<show>dnd</show>
<status>Good night, good night! parting is such sweet sorrow, that I shall say good night till it be morrow.</status>
</presence>
<presence from="romeo@gmail.com" to="benvolio@gmail.com" xml:lang="en">
<show>dnd</show>
<status>Good night, good night! parting is such sweet sorrow, that I shall say good night till it be morrow.</status>
</presence>
마치며
XMPP에 대해 알아보았는데, Google 및 Facebook에서 deprecate 되는것을 보니 잘은 모르지만 지고있는 표준이 아닌가 생각된다. 다음번에는 MQTT 및 message queue에 대한 내용을 알아보는 시간이 되도록 하겠다.
References
- Wikipedia XMPP - http://en.wikipedia.org/wiki/XMPP
- Fun with XMPP and Google Talk, Part 2 - http://www.adarshr.com/fun-with-xmpp-and-google-talk-part-2
- Fun with XMPP and Google Talk - http://www.adarshr.com/fun-with-xmpp-and-google-talk
- XMPP Standards Foundation official site - http://xmpp.org