Question

This is my situation. I have a chat-room website. People are publicly chatting together and everyone see who is writing what. All the chats are stored in database (mysql)

Now I want to implement this chatting on Android. So when user sends a text from his phone it should be sent to the chat-room website and vice versa, meaning the Android user should see all texts which are being sent from the chat webpage.

As a result:

1: Android user should see all the texts which people send via the webpage, 2: Android user should be able to send a text to this chat-room webpage (so other users which are using the webpage to chat should see his text).

The big question is, what is the best way to achieve this?

Could this process happen in real time like XMPP?

Is GCM the only way (although it is not real time)?

If i use web services to send the messages to the web, how can i set a listener for the incoming messages?

I don't know if i am clear. Any help is appreciated. Just give the head of the string i will go to the end...

Edit: a server side question: Is there anyway to make the server do something when a specific table in MYSQL is changed (for example when a new row is added)?

Était-ce utile?

La solution

The first thing that leapt into my mind was that this fits fairly well into the Pub/Sub paradigm. Clients publish chat messages to specific channels (rooms,) and also subscribe to the channels; the server subscribes to a channel and stores the data in a MySQL database.

You might try using an external real-time network like PubNub. PubNub is free for up to 1m messages (see the pricing page.) They have an Android SDK and PHP SDK (I assume you're using PHP on your server due to your use of the PHP tag.)

In your case, in your Android client, you'd subscribe to a channel:

Pubnub pubnub = new Pubnub("demo", "demo");

try {
    pubnub.subscribe("my_channel", new Callback() {
        //See full example for all Callback methods
        @Override
        public void successCallback(String channel, Object message) {
            System.out.println("SUBSCRIBE : " + channel + " : "
                  + message.getClass() + " : " + message.toString());
        }
    }
} catch (PubnubException e) {
    System.out.println(e.toString());
}

(Full example here.) Then, when you want to publish a message:

Callback callback = new Callback() {
    public void successCallback(String channel, Object response) {
        Log.d("PUBNUB",response.toString());
    }
    public void errorCallback(String channel, PubnubError error) {
        Log.d("PUBNUB",error.toString());
    }
};
pubnub.publish("my_channel", "This is an important chat message!" , callback); 

Neat! But what about your server, how does it receive these messages?

$pubnub = new Pubnub(
    "demo",  ## PUBLISH_KEY
    "demo",  ## SUBSCRIBE_KEY
    "",      ## SECRET_KEY
    false    ## SSL_ON?
);

$pubnub->subscribe(array(
    'channel'  => 'my_channel',         ## REQUIRED Channel to Listen
    'callback' => function($message) {  ## REQUIRED Callback With Response
        ## Time to log this to MySQL!
        return true;         ## Keep listening (return false to stop)
    }
));

I hope this helps your project. Let me know how it goes.

Autres conseils

SHORT ANSWER

Here's link to CODETUTS and to a SAMPLE

LONG ANSWER

For make a chat in realtime compatible with android using db like mysql you have various way. the first who come up to me is to do some api but is not the most good way cause you will have to do many request to your server. So i advice you to use technology like nodeJs and socketIO (just google them...you will find tons of example), take a look to the link i've found for you. Have a nice day. Antonio

You need websockets on the web to do this in real time, in android you need to send push notifications.

Maybe you want to check "Google Cloud Messaging for Android".

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top