Push Notification Service: How to Manage Offline Push Notification in Ejabberd XMPP Server?

Push Notification Service: How to Manage Offline Push Notification in Ejabberd XMPP Server?
4 (80%) 1 vote

live chat integration

Did you know that WhatsApp, Facebook, and Google use Ejabberd XMPP server as their primary chat protocol?

Guess what?

They do.

In fact, there are many other mobile apps for same chat feature like you can use Adium, PSI+, Empathy, Pidgin, apart which are multi-account chatting application. For Android, there is OneTeam, Beem, or Xabber for the same chat functionality.

However, in some cases, a sysadmin requires an internal chat solution and an Ejabberd cluster is an ideal option to adopt. An Ejabberd cluster generally has 4 Debian 7 VM’s (2 GB RAM each) spread over 3 sites and 1 datacenter, which serves nearly 12,000 users, most of the time serving 6000 users simultaneously.

Ejabberd stands for Erlang Jabber Daemon which is an XMPP application service, written in the Erlang Programming Language, that has an extensible protocol. It can run under many Unix-like operating systems such as Mac OS X, FreeBSD, Linux, NetBSD, OpenBSD, OpenSolaris, and Microsoft windows.


Is it Compulsory to Own XMPP server?

There are many reasons to set up your own XMPP server. Do you know the example of push notification in iPhone and Android? You might use Hangouts and you know Google’s service has changed and it might drop XMPP compatibility. If you have contacts, who don’t use Gmail, you can keep chatting to them. And, still use an open protocol, which is supported, not being locked into google specific software and hardware.

Or, you might want more control over the logging of your data. It would be recommended to turn off ejabberd logging and use ‘Off The Record’ that offers full privacy.

As Ejabberd is a widely used XMPP server to integrate chatting functionality into an app, we have integrated the ejabberd(XMPP) into our iOS application. One of our clients wanted to send a Push Notification to the offline users and every time, the offline users receive a message. We showed him few push notification examples and explained him two solutions.

Solution -1 (NOT Recommended):

We have decided to implement this from the iOS app side. That means, senders have to check every time, before sending the Message, whether the receivers are online or not. We will do this thing by using ejabberd SDK and call the Push Notification API if a user was offline.

This solution is the most tempting and fastest, but this idea is NOT an industry best practice.

Solution 2 (Recommended):

The best practice is the server should be notifying the OFFLINE users rather than the device notifies the server for offline users. The server should have no dependency on the client for checking the OFFLINE/ONLINE status of the users.

Note: Below are steps assuming that the reader has already setup the Ejabberd Server (2.1.11) on an Ubuntu Linux 14.04

Step 1

Connect to Ejabberd using Putty

Step 2
Install Erlang using the following command

>> sudo apt-get install erlang

How to Manage Offline Push Notification in Ejabberd XMPP Server?

Step 3
Create a New Directory “ejabberd_offline_push”

>> mkdir ejabberd_offline_push
>> cd ejabberd_offline_push

Step 4
Create a New .erl file with the following content and name it mod_ejabberd_offline_push.erl



-export([start/2, stop/1, create_message/3]).



start(_Host, _Opt) ->
?INFO_MSG("Module Started", []),
ejabberd_hooks:add(offline_message_hook, _Host, ?MODULE, create_push_message, 50).

stop (_Host) ->
?INFO_MSG("Stop Method of the Module Called", []),
ejabberd_hooks:delete(offline_message_hook, _Host, ?MODULE, create_push_message, 50).

create_push_message(_From, _To, Packet) ->
Type = xml:get_tag_attr_s("type", Packet),
Sender = xml:get_tag_attr_s("from", Packet),
Receiver = xml:get_tag_attr_s("to", Packet),
Body = xml:get_path_s(Packet, [{elem, "body"}, cdata]),
if (Type == "chat") ->
post_push_message(Sender, Receiver, Body)

post_push_message(Sender, Receiver, Body) ->
httpc:request(post, {"YourCustomUrlt",[], "application/x-www-form-urlencoded",lists:concat(["From=", Sender,"&To=", Receiver,"&Body=", Body])}, [], []),
?INFO_MSG("Push Sent.").

Step 5
Go to the Path, where you stored the module (.erl) and copy the following command

erlc -I /usr/lib/ejabberd/include/ mod_ejabberd_offline_push.erl

Step 6
This will create the .beam file in the same directory, the name of the file will be


Step 7

Copy the .beam file generated in the Step 6 to the /usr/lib/ejabberd/ebin/ using the following command

sudo mv mod_ejabberd_offline_push.beam /usr/lib/ejabberd/ebin/

Step 8
Stop the Jabber Server using

>> Service ejabberd stop

Step 9
Open the .cfg file of ejabberd (/etc/ejabberd/ejabberd.cfg) and paste the following Line


Step 10
Start the Jabber Server using

>> Service ejabberd Start

Step 11
Create an API on your custom URL and enjoy sending Push notifications to the Offline Users Cheers!!

ios notification
P.S This post was written by Mit Bhatt, a Team Leader at Space-O. If you want to integrate chat functionality or Ejabberd in your app, you can directly ask Mit about development hours. Fill this form now.


Ask Development Hours to Integrate Chat Functionality in Your App.