The documentation and examples for Process One’s just released EXMPP library is very lacking. Not to fault them, it was just released a couple days ago. So here I’ll describe how I used EXMPP for working with a multi-user chat system. Obviously you need to have your Jabber server setup for muc. Uncomment the mod_muc stuff in your ejabberd config if you are using that. I have mine set to use conference.localhost.
First, you can look in echo_client.erl that is provided with the source to see how to setup a connection to your Jabber server. The key pieces that are a pain to figure out are the records for connecting to a room and for creating a message.
Below is a function for creating the record to send that will move your user to the specified room:
-define (MUC, <<"http://jabber.org/protocol/muc">>).
-define (JABBER_HOST, "localhost").
-define (JABBER_MUC_HOST, "conference.localhost").
-define (JABBER_PORT, 5222).
create_move_room (Room, Username) ->
#xmlel {name=presence, attrs=[#xmlattr{name=to,
value=list_to_binary(Room++"@"++?JABBER_MUC_HOST++"/"++Username)}],
children=[#xmlel{name=x, attrs=[#xmlattr{name=xmlns, value=?MUC}]}]}.
exmpp_session:send_packet(Session, create_move_room (Room, Username)),
Now that we are in a room we can start sending messages to the people there:
create_message (Room, Message) ->
#xmlel {name=message, attrs=[#xmlattr{name=to,
value=list_to_binary(Room++"@"++?JABBER_MUC_HOST)},
#xmlattr{name=type, value=list_to_binary("groupchat")}],
children=[#xmlel{name=body,
children=[#xmlcdata{cdata=list_to_binary(Message)}]}]}.
exmpp_session:send_packet(Session, create_message(To, Msg)),
Not much too it. I was just annoyed trying to figure out their records for XML but they turned out be very easy as well. Its great to have a complete and well done Erlang Jabber library at last.
hi! i would enjoy your code a little more if it were formatted larger. the xml api does look a little goofy, maybe i’m just not used to it or maybe a little wrapper api would help
-(***o_soothing_o***)-
Hi, I wrote some examples for using exmpp here: http://obvioushints.blogspot.com/2009/10/writing-erlang-pubsub-client-with-exmpp.html
thanks for script xml, good luck admin
Hello,
Thank you for post.
Where i must call exmpp_session:send_packet(Session, create_move_room (Room, Username)), ???
I get error:
** {badarg,[{erlang,binary_to_list,[to]},
{exmpp_xml,xmlnsattributes_to_xmlattributes2,3},
{exmpp_xml,unresolve_xmlel_nss,3},
{exmpp_xml,node_to_iolist2,4},
{exmpp_xml,node_to_binary,3},
{exmpp_socket,send,2},
{exmpp_session,send_packet,3},
{exmpp_session,logged_in,3}]}
Thank you.