xmppo / xmpp-php

PHP client library for XMPP (Jabber) protocol
https://github.com/xmppo/xmpp-php
MIT License
45 stars 22 forks source link

Help with simple example using Prosody xmpp server #18

Closed guybrushbr closed 3 years ago

guybrushbr commented 3 years ago

Hi! Sorry, I am very new in this XMPP world and english is not my native language.

I am trying to send/receive messages using a Prosody xmpp server, following your basic example:

$options = new Options();
$options
    ->setHost('xmppserver.com')
    ->setPort(2700)
    ->setUsername('admin01')
    ->setPassword('password')
    ->setResource('Resource01');
$client = new XmppClient($options);
$client->connect();
$client->message->send('ping', 'user01@xmppserver.com');
$response = $client->getResponse();
echo '<br>'.$response;

admin01 can connect to the xmppserver.com and send the message without issues.

user01 is configured to auto-reply with a "pong" every time it receives a "ping" message.

When user01 is offline (disconnected) I receive the following response:

SCRAM-SHA-1-PLUSPLAINSCRAM-SHA-1admin01@xmppserver.com/Resource01

Here is the log:

2021.05.17 18:36:22 60a2e1d495412 REQUEST::Norgul\Xmpp\Socket::send::53 <message to='user01@xmppserver.com' type='normal'><body>ping</body></message>
2021.05.17 18:36:23 60a2e1d495412 RESPONSE::Norgul\Xmpp\Socket::receive::75 <message to='admin01@xmppserver.com/Resource01' from='user01@xmppserver.com' type='error'><error type='cancel'><service-unavailable xmlns='urn:ietf:params:xml:ns:xmpp-stanzas'/></error></message>

When user01 is online (connected) I receive the following response:

SCRAM-SHA-1-PLUSPLAINSCRAM-SHA-1admin01@xmppserver.com/Resource01pong  // Response "pong" is in the end

Here is the log:

2021.05.17 18:38:42 60a0118ad9ae4 REQUEST::Norgul\Xmpp\Socket::send::53 <message to='user01@xmppserver.com' type='normal'><body>ping</body></message>
2021.05.17 18:38:43 60a0118ad9ae4 RESPONSE::Norgul\Xmpp\Socket::receive::75 <message to='admin01@xmppserver.com' from='user01@xmppserver.com/Resource01' type='normal' id='XMPPMessage00000007'><body>pong</body></message>

Please, I'd like to know how to check for user presence before send the message and also how to parse the message to only receive what is in the body.

Thank you very much!

Norgul commented 3 years ago

Hello @pardinijr

it's been a while since I did any work on this library as I am engaged on other fronts. I wanted to refactor everything, but I have no time currently.

$client->getResponse() fetches the raw response. For message, try $client->message->receive() as specified here.

Regarding checking user presence, you should check that part in XMPP protocol documentation. I never had the need to do it so I can't really tell. There is a $client->iq->ping() method, but I doubt it is what you want. On the other hand, I'm not sure how auto-replies work. Only way this makes sense is if auto-reply is on XMPP server, so that it knows to return that response when user is disconnected. In your case I see a service-unavailable so I'm not sure if everything is configured correctly.

guybrushbr commented 3 years ago

Hello @Norgul

Thank you so much for your reply!

You are right, I was following the instructions inside Example.php and didn't notice the method $client->message->receive() in README.

I will research XMPP protocol documentation on how to get user presence and try to implement it. If I have sucess I will send you the code.

Best regards!