10

I use strophe.js library to send and receive XMPP messages in browser. It works fine, but only for users I already have in my contact list - roster.

I need to add someone (whose address I know) to my roster. How can I achieve this using strophe.js? This is important for me since gmail refuses sending messages to people I don't have in my roster. I'd like to get subscription: both, to be able to receive and send messages.

Pavel S.
  • 11,892
  • 18
  • 75
  • 113

1 Answers1

12

Send <presence to="friend@example.com" type="subscribe"/>:

conn.send($pres({ to: "friend@example.com", type: "subscribe" }));

When your friend accepts, they should send a subscribe to you also, which you can handle by setting a Strophe handler for incoming presence with type "subscribe":

function on_subscription_request(stanza)
{
    if(stanza.getAttribute("type") == "subscribe" && is_friend(stanza.getAttribute("from")))
    {
        // Send a 'subscribed' notification back to accept the incoming
        // subscription request
        conn.send($pres({ to: "friend@example.com", type: "subscribed" }));
    }
    return true;
}
conn.addHandler(on_subscription_request, null, "presence", "subscribe");
MattJ
  • 7,924
  • 1
  • 28
  • 33
  • @MattJ and whit is_friend method, can you update code with it? – pregmatch Nov 21 '13 at 13:15
  • 1
    @pregmatch It depends on your application. It receives a JID and should true if the user wants to accept their request, or false if they do not. – MattJ Nov 21 '13 at 23:18
  • @MattJ, I understand what it does. But do you check it against roster? My question was more like how is_friend(jid) function looks like. – pregmatch Nov 22 '13 at 10:08