There are a number of application that work well on there own and are great for the purpose they are designed for. However everything links together some how and understanding how applications work on their own is only the start of things.
Whilst I have been learning about a number new applications, I have also been looking at how applications can work with each other to give an end to end feel of how certain Internet businesses or back end technologies really work.
There are hundreds or some times thousands of different way in which applications can link with each other within a business or over the Internet. Whether it be using a different storage database at the back end or it be writing a front end client in a different language, I have been documenting a number of working solutions already in my blog http://vidorsolutions.com
Some of the technologies I have learnt and experimented successfully with are as follows:
1. Grails application deployed into Glassfish application server, using PostgreSQL as a database. Then using tools such as PGAdmin to connect to my databases locally on my machine.
2. Ejabberd Instant Messaging server installed on a server instance in the cloud, configured to use postgreSQL as a data source. Then using PSI Desktop chat client to connect with Ejabberd account. Tools such as PGAdmin can also be used here to view the back end database, tables and structure.
To further extend this learning experience I then created a basic Android XMPP client application that could also connect to the Ejabberd Server instance.
A lot of different technologies going on here and a lot to learn about the applications alone if you've never used any of these. However once you see how all these work together you get a greater appreciation of how the end to end user experience works.
Remember for some instructions on how to use these check out my previous posts
http://vidorsolutions.com
Showing posts with label chat. Show all posts
Showing posts with label chat. Show all posts
Wednesday, 19 January 2011
Tuesday, 18 January 2011
Writing a basic XMPP/Jabber chat application for Android
There are many chat applications for android open source that you can use to connect to an XMPP or Jabber server instance. This could be you own server such as Ejabberd or one such as Facebook / yahoo etc.
I have started to look at how to write a basic android XMPP client that I can use to connect to my Ejabberd server. I have pulled a number of sources available to gain knowledge in android, initially stating with Professional Android 2 Application Development.
There are a number other sample applications in this book that will aid in learning the android programming language.
However to understand how to create an XMPP application I started looking at Smack, an XMPP client library.
This is the library you will need to import and refer to when writing your android XMPP client. Google no longer has it's own native XMPP library.
Smack has be heavily patched to work with android devices, re-badged asmack, library downloads are available from http://code.google.com/p/asmack/
.
Once you have this imported into your project workspace you can now write a basic chat client. I used the following example code for my first point of call:
http://davanum.wordpress.com/2008/12/29/updated-xmpp-client-for-android/
After understanding what each bit of code here meant I was able to write my own similar app. Initially the original sample I'm going to show is very similar to the link above, so I cannot take full credit, I'm just sharing resources.
I have now heavily added to the app with shared preferences custom views and content providers etc, I may share the source in later posts.
You will need to write xml layouts to go with the code but the code behind the main application is as follows:
notice the org.jivesoftware.smack imports:
ChatClient.java:
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;
public class ChatClient extends Activity {
private ArrayList<String> messages = new ArrayList();
private Handler handler = new Handler();
private xmppsettings settings;
private EditText recipient;
private EditText text;
private ListView list;
private XMPPConnection connection;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
recipient = (EditText) this.findViewById(R.id.recipient);
text = (EditText) this.findViewById(R.id.text);
list = (ListView) this.findViewById(R.id.messageList);
setListAdapter();
//Window for getting settings
settings = new xmppsettings(this);
//Listener for chat message
Button send = (Button) this.findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String to = recipient.getText().toString();
String text1 = text.getText().toString();
Message msg = new Message(to, Message.Type.chat);
msg.setBody(text1);
connection.sendPacket(msg);
messages.add(connection.getUser() + ":");
messages.add(text1);
setListAdapter();
}
});
}
//Called by settings when connection is established
public void setConnection (XMPPConnection connection) {
this.connection = connection;
if (connection != null) {
//Packet listener to get messages sent to logged in user
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
connection.addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message.getFrom());
messages.add(fromName + ":");
messages.add(message.getBody());
handler.post(new Runnable(){
public void run() {
setListAdapter();
}
});
}
}
}, filter);
}
}
private void setListAdapter() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this, R.layout.list, messages);
list.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.settings:
settings.show();
return true;
case R.id.quit:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
xmppsettings.java: (I have made this a menu item)
import android.app.Dialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
//settings get input and then connection is established
public class xmppsettings extends Dialog implements android.view.View.OnClickListener {
private ChatClient chatClient;
public xmppsettings(ChatClient chatClient){
super(chatClient);
this.chatClient = chatClient;
}
protected void onStart() {
super.onStart();
setContentView(R.layout.settings);
setTitle("Connection Settings");
Button ok = (Button) findViewById(R.id.ok);
ok.setOnClickListener(this);
}
public void onClick(View v) {
String host = getText(R.id.host);
String port = getText(R.id.port);
String service = getText(R.id.service);
String username = getText(R.id.userid);
String password = getText(R.id.password);
// Create connection
ConnectionConfiguration connectionConfig =
new ConnectionConfiguration(host, Integer.parseInt(port), service);
XMPPConnection connection = new XMPPConnection(connectionConfig);
try {
connection.connect();
} catch (XMPPException ex) {
chatClient.setConnection(null);
}
try {
connection.login(username, password);
// Set status to online / available
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
chatClient.setConnection(connection);
} catch (XMPPException ex) {
chatClient.setConnection(null);
}
dismiss();
}
private String getText(int id) {
EditText widget = (EditText) this.findViewById(id);
return widget.getText().toString();
}
}
After these initial files I then when one to create a shared preferences which will save the settings that a user inputs.
Also then when on to write a separate array adapter within the chatclient.java class which then aided in the creation of a content provider to store the messages in a db.
This is the application at the most basic, but will get you started in developing any high end chat app.
I have started to look at how to write a basic android XMPP client that I can use to connect to my Ejabberd server. I have pulled a number of sources available to gain knowledge in android, initially stating with Professional Android 2 Application Development.
There are a number other sample applications in this book that will aid in learning the android programming language.
However to understand how to create an XMPP application I started looking at Smack, an XMPP client library.
This is the library you will need to import and refer to when writing your android XMPP client. Google no longer has it's own native XMPP library.
Smack has be heavily patched to work with android devices, re-badged asmack, library downloads are available from http://code.google.com/p/asmack/
.
Once you have this imported into your project workspace you can now write a basic chat client. I used the following example code for my first point of call:
http://davanum.wordpress.com/2008/12/29/updated-xmpp-client-for-android/
After understanding what each bit of code here meant I was able to write my own similar app. Initially the original sample I'm going to show is very similar to the link above, so I cannot take full credit, I'm just sharing resources.
I have now heavily added to the app with shared preferences custom views and content providers etc, I may share the source in later posts.
You will need to write xml layouts to go with the code but the code behind the main application is as follows:
notice the org.jivesoftware.smack imports:
ChatClient.java:
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.filter.MessageTypeFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.packet.Message;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.util.StringUtils;
public class ChatClient extends Activity {
private ArrayList<String> messages = new ArrayList();
private Handler handler = new Handler();
private xmppsettings settings;
private EditText recipient;
private EditText text;
private ListView list;
private XMPPConnection connection;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
recipient = (EditText) this.findViewById(R.id.recipient);
text = (EditText) this.findViewById(R.id.text);
list = (ListView) this.findViewById(R.id.messageList);
setListAdapter();
//Window for getting settings
settings = new xmppsettings(this);
//Listener for chat message
Button send = (Button) this.findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String to = recipient.getText().toString();
String text1 = text.getText().toString();
Message msg = new Message(to, Message.Type.chat);
msg.setBody(text1);
connection.sendPacket(msg);
messages.add(connection.getUser() + ":");
messages.add(text1);
setListAdapter();
}
});
}
//Called by settings when connection is established
public void setConnection (XMPPConnection connection) {
this.connection = connection;
if (connection != null) {
//Packet listener to get messages sent to logged in user
PacketFilter filter = new MessageTypeFilter(Message.Type.chat);
connection.addPacketListener(new PacketListener() {
public void processPacket(Packet packet) {
Message message = (Message) packet;
if (message.getBody() != null) {
String fromName = StringUtils.parseBareAddress(message.getFrom());
messages.add(fromName + ":");
messages.add(message.getBody());
handler.post(new Runnable(){
public void run() {
setListAdapter();
}
});
}
}
}, filter);
}
}
private void setListAdapter() {
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this, R.layout.list, messages);
list.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.settings:
settings.show();
return true;
case R.id.quit:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
xmppsettings.java: (I have made this a menu item)
import android.app.Dialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;
//settings get input and then connection is established
public class xmppsettings extends Dialog implements android.view.View.OnClickListener {
private ChatClient chatClient;
public xmppsettings(ChatClient chatClient){
super(chatClient);
this.chatClient = chatClient;
}
protected void onStart() {
super.onStart();
setContentView(R.layout.settings);
setTitle("Connection Settings");
Button ok = (Button) findViewById(R.id.ok);
ok.setOnClickListener(this);
}
public void onClick(View v) {
String host = getText(R.id.host);
String port = getText(R.id.port);
String service = getText(R.id.service);
String username = getText(R.id.userid);
String password = getText(R.id.password);
// Create connection
ConnectionConfiguration connectionConfig =
new ConnectionConfiguration(host, Integer.parseInt(port), service);
XMPPConnection connection = new XMPPConnection(connectionConfig);
try {
connection.connect();
} catch (XMPPException ex) {
chatClient.setConnection(null);
}
try {
connection.login(username, password);
// Set status to online / available
Presence presence = new Presence(Presence.Type.available);
connection.sendPacket(presence);
chatClient.setConnection(connection);
} catch (XMPPException ex) {
chatClient.setConnection(null);
}
dismiss();
}
private String getText(int id) {
EditText widget = (EditText) this.findViewById(id);
return widget.getText().toString();
}
}
After these initial files I then when one to create a shared preferences which will save the settings that a user inputs.
Also then when on to write a separate array adapter within the chatclient.java class which then aided in the creation of a content provider to store the messages in a db.
This is the application at the most basic, but will get you started in developing any high end chat app.
Labels:
android,
app,
asmack,
chat,
chat client,
development,
Ejabberd,
Facebook,
jabber,
library,
smack,
xmpp
Saturday, 4 December 2010
Using PSI chat client to access Ejabberd Chat messaging server
PSI is one of many jabber/XMPP clients available across the net. You can use these type of clients to test connectivity to or use functions available on most instant messaging server instances; in this case ejabberd.
1. Firstly ensure ejabberd is running on the server and there is a user account you can log in with.
use ./ejabberdctl [start/stop/status] to view status or start instance
use ./ejabberdctl register username password to register a new user
2. Install PSI from source or package
Cross platform support available from http://psi-im.org/download/
3. Launch PSI, add a new account, ensuring you fill in the following fields:
jabber id - this is the account you create on the ejabberd server.
password - corresponding password with above account
under connection settings:
specify server IP address or host name, ensuring you complete the port number; usually 5222
4. Save all settings and view the account in PSI application.
Change status to online.
5. Test chat to another user using same method of setup above, or log into web admin page of ejabberd server at http://SERVERIP:5280/admin to confirm successfully connectivity.
1. Firstly ensure ejabberd is running on the server and there is a user account you can log in with.
use ./ejabberdctl [start/stop/status] to view status or start instance
use ./ejabberdctl register username password to register a new user
2. Install PSI from source or package
Cross platform support available from http://psi-im.org/download/
3. Launch PSI, add a new account, ensuring you fill in the following fields:
jabber id - this is the account you create on the ejabberd server.
password - corresponding password with above account
under connection settings:
specify server IP address or host name, ensuring you complete the port number; usually 5222
4. Save all settings and view the account in PSI application.
Change status to online.
5. Test chat to another user using same method of setup above, or log into web admin page of ejabberd server at http://SERVERIP:5280/admin to confirm successfully connectivity.
Installing Ejabberd Chat Messaging Server on Linux
Ejabberd is instant messaging server software that can be incorporated into any environment. Basic installation instructions for Unix / Linux are as follows:
1. Install Erlang:
sudo apt-get install erlang.
or from source,
Ensure all requirements are installed, Libncurses and OpenSS. Use apt-get for these.
Erlang:
wget http://www.erlang.org/download/otp_src_R14B.tar.gz
tar -xzvf otp_src_R14B.tar.gz
cd otp_src_R14B.tar.gz
LANG=C; export LANG
./configure
make
make install
Verify installation, type erl
The following should be displayed:
Erlang R14B (erts-5.8) [source] [smp:2:2] [rq:2] [async-threads:0] [kernel-poll:false]
Eshell V5.8 (abort with ^G)
1>
2. Install Ejabberd:
debian package:
apt-get install ejabberd
binary installer:
wget http://www.process-one.net/downloads/ejabberd/2.1.6/ejabberd-2.1.6-linux-x86_64-installer.bin.gz
gunzip ejabberd-2.1.6-linux-x86_64-installer.bin.gz
chmod a+x ejabberd-2.1.6-linux-x86_64-installer.bin.gz
./ejabberd-2.1.6-linux-x86_64-installer.bin
Accept all default options, installation complete
or from source:
wget http://www.process-one.net/downloads/ejabberd/2.1.5/ejabberd-2.1.5.tar.gz
tar -xzvf ejabberd-2.1.5.tar.gz
cd ejabberd-2.1.5/src
./configure
make
make install
/bin/ejabberd.cfg to specify settings
Use ejabberdctl start to start node instance.
Browse to http://localhost:5280/admin/ for server web admin, you will need to add exception for user admin to browse to web admin page.
Enter the following in /bin/ejabberd.cfg:
{acl, admin, {user, "admin_user", "your_sever"}}.
N.B ejabberd uses an internal Mnesia database by default. But it possible to an alternative relational database or LDAP server to ensure data is persistent.
In later posts I will discuss using a PostgreSQL database to store data for various applications such as ejabberd.
Refer to Ejabberd installation and operation guide
1. Install Erlang:
sudo apt-get install erlang.
or from source,
Ensure all requirements are installed, Libncurses and OpenSS. Use apt-get for these.
Erlang:
wget http://www.erlang.org/download/otp_src_R14B.tar.gz
tar -xzvf otp_src_R14B.tar.gz
cd otp_src_R14B.tar.gz
LANG=C; export LANG
./configure
make
make install
Verify installation, type erl
The following should be displayed:
Erlang R14B (erts-5.8) [source] [smp:2:2] [rq:2] [async-threads:0] [kernel-poll:false]
Eshell V5.8 (abort with ^G)
1>
2. Install Ejabberd:
debian package:
apt-get install ejabberd
binary installer:
wget http://www.process-one.net/downloads/ejabberd/2.1.6/ejabberd-2.1.6-linux-x86_64-installer.bin.gz
gunzip ejabberd-2.1.6-linux-x86_64-installer.bin.gz
chmod a+x ejabberd-2.1.6-linux-x86_64-installer.bin.gz
./ejabberd-2.1.6-linux-x86_64-installer.bin
Accept all default options, installation complete
or from source:
wget http://www.process-one.net/downloads/ejabberd/2.1.5/ejabberd-2.1.5.tar.gz
tar -xzvf ejabberd-2.1.5.tar.gz
cd ejabberd-2.1.5/src
./configure
make
make install
/bin/ejabberd.cfg to specify settings
Use ejabberdctl start to start node instance.
Browse to http://localhost:5280/admin/ for server web admin, you will need to add exception for user admin to browse to web admin page.
Enter the following in /bin/ejabberd.cfg:
{acl, admin, {user, "admin_user", "your_sever"}}.
N.B ejabberd uses an internal Mnesia database by default. But it possible to an alternative relational database or LDAP server to ensure data is persistent.
In later posts I will discuss using a PostgreSQL database to store data for various applications such as ejabberd.
Refer to Ejabberd installation and operation guide
Subscribe to:
Posts (Atom)