I am developing a One to One chatting app with socket.io in android. I can send & receive messages from a single chat room. I am following this tutorial. My apps Chat module looks like that. Now, I want to sent & receive message from single user.
During development, i observed that each and every socket connection , socket.io gives client a new ID which looks like :
/#IyQ7LaKzLClf7g3DAAAA
For that reasons i can't track a the specific user to send message.
Qs 1. Will I have to connect any Database for storing user credential for send a message to specific user & also to send offline message to him/her? Chatting feature is additional function in my android app. Currently this app uses token based authentication.
I am novice in node js & socket.io. Give me a architectural guideline how to solve this. TIA.
My app.js code :
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
var port = process.env.PORT || 3000;
server.listen(port, function () {
console.log('Server listening at port %d', port);
});
// Routing
app.use(express.static(__dirname + '/public'));
// Chatroom
var numUsers = 0;
var clients = [];
io.on('connection', function (socket) {
var addedUser = false;
clients.push(socket);
console.log('one user connected: user name: ' +socket.username +"------ id : >> "+ socket.id);
console.log('Total User List:' + clients);
socket.on('connect', function (data) {
// we tell the client to execute 'new message'
socket.broadcast.emit('connect', {
username: socket.username,
numUsers: numUsers,
socket_id:socket.id
});
});
// when the client emits 'new message', this listens and executes
socket.on('new message', function (data) {
// we tell the client to execute 'new message'
socket.broadcast.emit('new message', {
username: socket.username,
message: data
});
});
// when the client emits 'add user', this listens and executes
socket.on('add user', function (username) {
if (addedUser) return;
// we store the username in the socket session for this client
socket.username = username;
++numUsers;
addedUser = true;
socket.emit('login', {
numUsers: numUsers
});
// echo globally (all clients) that a person has connected
socket.broadcast.emit('user joined', {
username: socket.username,
numUsers: numUsers,
socket_id:socket.id
});
});
// when the client emits 'typing', we broadcast it to others
socket.on('typing', function () {
socket.broadcast.emit('typing', {
username: socket.username
});
});
// when the client emits 'stop typing', we broadcast it to others
socket.on('stop typing', function () {
socket.broadcast.emit('stop typing', {
username: socket.username
});
});
// when want to send message to specific user
socket.on('say to someone', function (id, msg) {
socket.broadcast.to(id).emit('say to someone', {
username: socket.username,
id:id,
message: msg
});
});
// when the user disconnects.. perform this
socket.on('disconnect', function () {
clients.splice(clients.indexOf(socket), 1);
console.log('Disconnected... ' + socket.id);
if (addedUser) {
--numUsers;
// echo globally that this client has left
socket.broadcast.emit('user left', {
username: socket.username,
numUsers: numUsers
});
}
});
});