I m very new to the whole socket stuff using node. What I am trying to achieve is make a chat application using Socket.io on Node Js, with Laravel and Redis. I have followed the documentation for Socket and made a test app. User enters a message, it is received by Laravel, broadcasted through Redis to node, and then the socket should emit. Reason for Laravel is, that the chat stuff is a module of an already made Laravel app. Anyways, the issue here is, the message is received by Redis and it console.logs it to the terminal, but socket is not emitting. Here is my code for the android part:
EditText message;
TextView display;
Button send;
String text;
Socket mSocket;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
display = (TextView)findViewById(R.id.message);
message = (EditText)findViewById(R.id.text);
send = (Button)findViewById(R.id.send);
ChatApplication app = new ChatApplication();
mSocket =app.getmSocket();
send.setOnClickListener(this);
mSocket.on(Socket.EVENT_CONNECT, onConnect);
mSocket.on(Socket.EVENT_DISCONNECT, onDisconnect);
mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
mSocket.on("new message", onNewMessage);
mSocket.connect();
}
@Override
public void onClick(View view) {
text = message.getText().toString().trim();
Message mes = new Message(text);
final Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Constants.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
final APIStuff service = retrofit.create(APIStuff.class);
// Message message = new Message();
Call<List<Message>> mess = service.message(mes);
mess.clone().enqueue(new Callback<List<Message>>() {
@Override
public void onResponse(Call<List<Message>> call, Response<List<Message>> response) {
Message mes = new Message(text);
if (!mSocket.connected()) {
return;
}
if (text != null) {
mSocket.emit("message", text);
}
}
@Override
public void onFailure(Call<List<Message>> call, Throwable t) {
}
});
}
private Emitter.Listener onConnect = new Emitter.Listener(){
@Override
public void call(Object... args) {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
isConnected = true;
Toast.makeText(MainActivity.this, "STUFF CONNECTS", Toast.LENGTH_SHORT);
}
});
}
};
private Emitter.Listener onDisconnect = new Emitter.Listener(){
@Override
public void call(Object... args) {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
isConnected = false;
}
});
}
};
private Emitter.Listener onNewMessage = new Emitter.Listener() {
@Override
public void call(final Object... args) {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
JSONObject data = (JSONObject) args[0];
String message;
String all;
try {
message = data.getString("username");
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT);
System.out.println("Data is: " + data);
} catch (JSONException e) {
return;
}
}
});
}
};
private Emitter.Listener onConnectError = new Emitter.Listener() {
@Override
public void call(Object... args) {
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.e(TAG, "Error connecting");
Toast.makeText(MainActivity.this.getApplicationContext(),
"WOOPS", Toast.LENGTH_LONG).show();
}
});
}
};
Socket initialization:
public class ChatApplication {
private Socket mSocket;
{
try{
mSocket = IO.socket(Constants.CHAT_URL);
}catch(URISyntaxException e){
throw new RuntimeException(e);
}
}
public Socket getmSocket(){
return mSocket;
}
}
Here is my Laravel Implementation:
Route::post('/message', function(Request $request){
$message = $request['message'];
$data = [
'event'=>'message',
'data'=> $message,
'name'=>'yolo'
];
Redis::publish('test-channel', json_encode($data));
return;
});
And finally my Node.js code:
var server = require('http').Server();
var io = require('socket.io')(server);
var Redis = require('ioredis');
var redis = new Redis();
redis.subscribe('test-channel');
redis.on('message', function(channel, message){
message = JSON.parse(message);
console.log(message.data);
io.emit(message.data);
});
server.listen(3500, '192.168.1.105');
console.log("Server is running at address 192.168.1.105")
As I have said, I m completely new to the node and socket stuff, and have been following tutorials, documentations, etc. For this implementation, I followed Laracasts, and then dug around the docs, but i cannot understand what exactly is going on. Any help to improve the code, how to fix it, or even any guidance or pointing to the right direction will be much appreciated!