Broadcast message using Openfire

Hi everybody,

I’m wondering how can we send message to a chat room (contain many users in there) by using PHP language?

In PHP, I used XMPP library and can send message to individual user successfully but when I tried to send to a chat room, not success.

Please let me know if my question not so clear.

Thank you in advance.

Did you first join the chatroom by sending presence to the room?

daryl

Hi Daryl,

Thank for your reply, but I still didn’t make it work. Let me explain my situation.

I have 1 chat room (room01@conference.my_server_name) with some people in there. They log on by using Spark client app. I want to use my PHP code to send message to this chat room, so I did as follow:

$conn = new XMPPHP_XMPP(‘my_server_name’, 5222, ‘my_account’, ‘my_password’, ‘resource’);

$conn->useEncryption(false);

$conn->connect();

$conn->processUntil(‘session_start’);

$conn->presence(‘Hello’, TRUE, ‘room01@conference.my_server_name’, ‘’);

$conn->message(‘room01@conference.my_server_name’, ‘This is a test message!’);

$conn->disconnect();

Do you have any idea, Daryl?

Thank you in advance.

Deline,

I think you’d want to send an initial online presence and then I am not sure your presence() function is called correctly…

$conn->presence();

$conn->presence(NULL, "available", "testchatroom@127.0.0.1/myuser");

daryl

Hi Daryl,

I modified my code as you told but I still can not send a message to a chat room. This is my code after modified:

$conn = new XMPPHP_XMPP(‘my_server_name’, 5222, ‘username’, ‘password’, ‘xmpphp’, ‘my_server_name’, $printlog = true, $loglevel = XMPPHP_Log::LEVEL_INFO);

$conn->connect();

$conn->processUntil(‘session_start’);

$conn->presence(NULL, “available”, “testchatroom@conference.my_server_name/username”);

$conn->message(‘testchatroom@conference.my_server_name’, ‘Test message!’, ‘groupchat’);

$conn->presence(NULL, “unavailable”, “testchatroom@conference.my_server_name/username”);

$conn->disconnect();

testchatroom@conference.my_server_name have some people here.

I also saw the link you gave me. But in that link, Trillomedia S.r.l. also have a problem like me. He and I also send a message to a specific user successfully but when send to a chat room, it did not work.

Do we need to configure something of chat room in Openfire Admin Console? Please help me about this if you have any idea. Currently, I have no clue to continue.

Thank you Daryl for your reply.

I wonder if your code is executing faster than the server is processing the stanzas, so the client exits before anything actually happens. Are you able to put some wait or sleep functions into the code to give it some time for processing to occur?

Hi Daryl,

Thank you for your answer, I’m also so sorry for my late response. I put sleep function into my code but I still can’t send the message to chat room.

Anyway, thank you for your help!!!

Hi Daryl, now I can send the message to the chat room. I don’t know why, I just go to Admin Console and change Topic Description for chat room, and then I used this code to send the message & it was sent.

This is my code I used to send the message to chat room. I hope this will help someone face with this problem like me.

$conn = new XMPPHP_XMPP(‘my_server_name’, 5222, ‘my_account’, ‘my_password’, ‘xmpphp’, ‘my_server_name’, $printlog = true, $loglevel = XMPPHP_Log::LEVEL_INFO);

$conn -> connect();

$conn -> processUntil(‘session_start’);

$conn -> presence(NULL, “available”, “room01@conference.my_server_name/my_account”);

$conn -> message(‘room01@conference.my_server_name’, ‘Test message!’, ‘groupchat’);

sleep(60);

$conn -> presence(NULL, “unavailable”, “room01@conference.my_server_name/my_account”);

$conn -> disconnect();

Thank you so much Daryl, you helped me a lot!!!

1 Like

Does the room exist prior to you joining it? Perhaps what is happening is that the server is sending the client the room configuration options when you are joining it, thus it is locked prior to entry?

Hello sir , I want send push notification to multiple user using xmpp server , now i have send only one user at a time.

This is cide I use to send message to a user.

$conn = new XMPPHP_XMPP(‘server name’, 5222, ‘username’, ‘password’, ‘xmpphp’, ‘server name’, $printlog=false, $loglevel=XMPPHP_Log::LEVEL_INFO);

try {

$conn->connect();

$conn->processUntil(‘session_start’);

$conn->presence();

$conn->message(user@sever domain, $msg , ‘chat’);

$conn->disconnect();

} catch(XMPPHP_Exception $e) {

die($e->getMessage());

}

Hi Ashish,

As my code above, I did as following:

1. I created a group chat room in Openfire Admin Console page.

2. Then, I sent a presence of my acount to that room (join that chat room):

$conn -> processUntil(‘session_start’);

$conn -> presence(NULL, “available”, “room01@conference.my_server_name/my_account”);

3. After that, I sent a message to that room and wait for 60 seconds. I sent the message by ‘groupchat’ mode, not by ‘chat’ mode.

$conn -> message(‘room01@conference.my_server_name’, ‘Test message!’, ‘groupchat’);

sleep(60);



4. Finally, I’m out of that chat room before disconnect the server.

$conn -> presence(NULL, “unavailable”, “room01@conference.my_server_name/my_account”);

$conn -> disconnect();

I noticed in your code that you just send a message by ‘chat’ mode, not ‘groupchat’ mode:

$conn->presence();

$conn->message(user@sever domain, $msg , ‘chat’);

Hope this helps

Hi deline,

The above code is not working for me nor I am able to send messages to individual users. What I want is to send and receive messages from individual users using xmpp server. Please help.

Hi Dary/ ashish l, now I can send the message to the single user .but i am unable to send message to group,

conn = new XMPPHP_XMPP('my_server, ‘username’, ‘password’, ‘xmpphp’, ‘myserver’, $printlog=true, $loglevel=XMPPHP_Log::LEVEL_INFO);

var_dump($conn);

$conn->connect();

$conn->processUntil(‘session_start’);

$conn->message('jeeva@myserver, ‘This is a momday single chat message!’,‘chat’);

$conn->disconnect();

This above cod working successfully for single chat.

$conn = new XMPPHP_XMPP('my_server, 5222, ‘username’, ‘passwors’, ‘xmpphp’, 'my_server, , $printlog = true, $loglevel = XMPPHP_Log::LEVEL_INFO);

var_dump($conn);

$conn -> connect();

$conn -> processUntil(‘session_start’);

$conn -> presence(NULL, “available”, “hobbies@conference.my_server, /admin”);

$conn -> message(‘hobbies@conference.my_server’, ‘Tis is group chat!’, ‘groupchat’);

sleep(60);

$conn -> presence(NULL, “unavailable”, “hobbies@my_server, .tenet.res.in/admin”);

$conn -> disconnect();

Here i am using above code for group chat,but its not sending any message ,

Kindly do the need full.

Hi denile , i am new one in openfire , now I can send the message to the single user .but i am unable to send message to group,

conn = new XMPPHP_XMPP('my_server, ‘username’, ‘password’, ‘xmpphp’, ‘myserver’, $printlog=true, $loglevel=XMPPHP_Log::LEVEL_INFO);

var_dump($conn);

$conn->connect();

$conn->processUntil(‘session_start’);

$conn->message('jeeva@myserver, ‘This is a momday single chat message!’,‘chat’);

$conn->disconnect();

This above cod working successfully for single chat.

$conn = new XMPPHP_XMPP('my_server, 5222, ‘username’, ‘passwors’, ‘xmpphp’, 'my_server, , $printlog = true, $loglevel = XMPPHP_Log::LEVEL_INFO);

var_dump($conn);

$conn -> connect();

$conn -> processUntil(‘session_start’);

$conn -> presence(NULL, “available”, “hobbies@conference.my_server, /admin”);

$conn -> message(‘hobbies@conference.my_server’, ‘Tis is group chat!’, ‘groupchat’);

sleep(60);

$conn -> presence(NULL, “unavailable”, “hobbies@my_server, .tenet.res.in/admin”);

$conn -> disconnect();

Here i am using above code for group chat,but its not sending any message ,

Kindly do the need full.

$conn = new XMPPHP_XMPP('my_server, 5222, ‘username’, ‘passwors’, ‘xmpphp’, 'my_server, , $printlog = true, $loglevel = XMPPHP_Log::LEVEL_INFO);

var_dump($conn);

$conn -> connect();

$conn -> processUntil(‘session_start’);

$conn -> presence(NULL, “available”, “hobbies@conference.my_server, /admin”);

$conn -> message(‘hobbies@conference.my_server’, ‘Tis is group chat!’, ‘groupchat’);

sleep(60);

$conn -> presence(NULL, “unavailable”, “hobbies@my_server, .tenet.res.in/admin”);

$conn -> disconnect();

Here i am using above code for group chat,but its not sending any message ,

Kindly do the need full.

Thanks for all support sir…

my new question is . can i create new conference from php ? ans is yes so how?

yes sir thanks for your more valuable reply now i want to create conference room from php and add user in this conference room.

include ‘…/XMPP.php’;

$conn = new XMPPHP_XMPP(‘xxx.xxx.xxx.xxx’, 5222, ‘username’, ‘password’, ‘xmpphp’,

xxx.xxx.xxx.xxx’, $printlog = true, $loglevel = XMPPHP_Log::LEVEL_INFO);

$conn -> connect();

$conn -> processUntil(‘session_start’);

$conn -> presence(NULL, “available”, "room1@conf.xxx.xxx.xxx.xxx/username");

$conn -> message(‘room1@conf.xxx.xxx.xxx.xxx’, ‘Test message!’, ‘groupchat’);

sleep(60);

$conn -> presence(NULL, “unavailable”, "room1@conf.xxx.xxx.xxx.xxx/username");

$conn -> disconnect();

// sir i able to send individual message but can’t send group message for conferance room…

and also sir please i want to register user and create group from php . Please denote a way to work it proper.