2

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
            });
        }
    });
});
Shihab Uddin
  • 6,699
  • 2
  • 59
  • 74
  • 2
    If connection ID changed dynamically then to communicate with a particular user on chat you could maintain a registry at server side. You could use a database or simple HashMap for small amount of data for runtime – rev_dihazum Jun 16 '16 at 06:12
  • @rev_dihazum thanks for reply. Qs. 1 .for node js which db is suitable ? My app already use MySQL for authentication. Qs 2. For offline message; will i store it to db? – Shihab Uddin Jun 16 '16 at 06:16
  • 1
    You could use your existing database. Also for simply maintain only a registry you could also use any NoSQL database like mongoDB, couchDB etc for better performance. Off-line message at android side may be not be connected at server side mysql when your app at off-line!. You could save your off-line message at your device; e.g. SQLite. – rev_dihazum Jun 16 '16 at 06:20

1 Answers1

0

There is problem with your implementation of socket.on('say to someone', function (id, msg)

please see the link below

Sending message to a specific ID in Socket.IO 1.0

https://auth0.com/blog/2014/01/15/auth-with-socket-io/

Community
  • 1
  • 1
Abu Sufian
  • 157
  • 1
  • 11