title: Android and Socket.io , send and receive data
body: <p>I can send data to server.js and it work well but i can't receive data from server.When i click button server.js gets data i think Server.js send data but my App need some code to get data.</p>
  
  <p><a href="https://i.stack.imgur.com/cjCHT.png" rel="nofollow noreferrer">enter image description here</a></p>
  
  <p>My server.js work well</p>
  
  <pre><code>var app = require('express')();
  var http = require('http').Server(app);
  var io = require('socket.io')(http);
  
  app.get('/', function(req, res){
    res.send('&lt;h1&gt;Hello world&lt;/h1&gt;');
  });
  
  io.on('connection',function(socket){
      console.log('one user connected '+socket.id);
  
  
  
      socket.on('CHAT' , function (data) {
          console.log('======CHAT message========== ');
          console.log(data);
          socket.emit('CHAT',data);
  
      });
  
      socket.on('disconnect',function(){
          console.log('one user disconnected '+socket.id);
      });
  
  })
  
  http.listen(3000,function(){
      console.log('server listening on port 3000');
  })
  </code></pre>
  
  <p>And my Activity:</p>
  
  <pre><code>import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  import org.json.JSONException;
  import org.json.JSONObject;
  import java.net.Socket;
  import java.net.URISyntaxException;
  import io.socket.client.IO;
  import io.socket.emitter.Emitter;
  
  
  public class MainActivity extends AppCompatActivity {
      public Button button;
      public TextView text;
      public EditText edit;
      private io.socket.client.Socket mSocket;
  
      {
          try {
              mSocket = IO.socket("http://192.168.1.4:3000");
  
  
          } catch (URISyntaxException e) {
              Log.d("myTag", e.getMessage());
          }
      }
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          button = (Button) findViewById(R.id.button);
          text =  (TextView) findViewById(R.id.text);
          edit =  (EditText) findViewById(R.id.edit);
          mSocket.connect();
          text.setText("");
  
          button.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
  
                  String message = edit.getText().toString().trim();
                  edit.setText("");
                  if (!message.isEmpty()) {
                      //send message
                      String jsonString = "{message: " + "'" + message + "'" + "}";
  
                      try {
                          JSONObject jsonData = new JSONObject(jsonString);
                          mSocket.emit("CHAT", jsonData);
                      } catch (JSONException e) {
                          Log.d("me", "error send message " + e.getMessage());
                      }
                  }
              }
          });//button.setOnClickListener
  
      }//on create
  }//end of class
  </code></pre>
  
  <p>This code need something like this to get data from Server.js , i do not know how can i use it</p>
  
  <pre><code>mSocket.on("CHAT", new Emitter.Listener() {
              @Override
              public void call(Object... args) {
                  try {
                      JSONObject messageJson = new JSONObject(args[0].toString());
                      String message = messageJson.getString("message");
                      text.setText(message);
                      runOnUiThread(new Runnable() {
                          @Override
                          public void run() {
                              //  mMessageAdapter.notifyDataSetChanged();
                              Toast.makeText(getApplicationContext(), "Your toast message.", Toast.LENGTH_SHORT).show();
                          }
                      });
                  } catch (JSONException e) {
                      e.printStackTrace();
                  }
              }
          });
  </code></pre>