Christopher Bengtsson Christopher Bengtsson - 11 months ago 57
Node.js Question

Multiple parameters from android to nodeJS server using socket

How do I send multiple parameters on Android to a node.js sever, with the socket.emit? Im trying with

mSocket.emit("new message", username, message, String.valueOf(longitude), String.valueOf(latitude));
but it wont work. Any ideas? The android device receives messages but the webclient doesnt receive from my android.

This is the server:

socket.on('new message', function (data) {
socket.broadcast.emit('new message', {
username: socket.username,
message: data.message,
longitude: data.longitude,
latitude: data.latitude

This is client on the website, which is working:

socket.emit('new message', {
username: username,
message: message,
longitude: longitude,
latitude: latitude

Answer Source

I think your server is expecting just one parameter (a JSONObject). Try this code

JSONObject jsonObject = new JSONObject();
jsonObject.put("username", username);
jsonObject.put("message", message);
jsonObject.put("longitude", longitude);
jsonObject.put("latitude", latitude);
mSocket.emit("new message", jsonObject);