Friday, 2 June 2017

How to make socket.io server aware of id's retrieved from mysql server

I am working on a chat, where users can send private messages. This is done by using the id's of users that are logged in. However, it only works for users that are logged in for the first time. If they disconnect and log in again, the node server is not aware of the id. Hence, other users cannot message them.

So my question is: Is there a way to make the server aware of "old" id's?

Here's my server-side code, where you can see how I handle the creation of new users, and the ones logging in:

// Setting up the server
var express = require('express');

var mysql = require('mysql');
var connection = mysql.createConnection({
        host: "localhost",
        user: 'root',
        password: '1234',
        database: 'mad_app' 
});

//Logging error connected to the database
connection.connect(function(err) {
        if (err) {
                console.log(err);
        return;
        } 
});

var app = express();
var path = require('path');
var server = require('http').createServer(app);
var socket = require("socket.io").listen(server);
var uuid = require ('node-uuid');
var users = {};

server.listen(3000);
console.log("Server is running");

app.get('/', function(req, res){
  res.sendFile(__dirname + '/index.html');
});

// Gets the css file
app.use(express.static(path.join(__dirname, 'public')));




socket.on('connection', function(socket){
console.log('user connected - NO ID', socket.id);
  //var id = uuid.v4();
  //socket.id = id;
  console.log('a user connected', socket.id);

//Checking if user already exists
        socket.on("checkUser", function(newUserName, newUserPassword, fn){
                connection.query("SELECT * FROM users WHERE (name= '" + newUserName +"' AND password= '" + newUserPassword + "') ", function(error, results){
                                if (error) {
                                        console.log(error);
                                }
                                else if (results) {
                                        fn({result: results[0]});
                                }
                });
        });

 //Creating new user
        socket.on("createNewUser", function(newUserName, newUserPassword){
                //var id = uuid.v4();
                var user = {
                        name: newUserName,
                        id: socket.id,
                        password: newUserPassword
                }
                var query = 'INSERT INTO users SET ?';
                connection.query(query, user);
                users[socket.id] = {"name" : newUserName, "id" : socket.id};
                console.log("Created new users. These users are online:", users);
        });


 //Login
        socket.on("login", function(myUserName, myUserID){
                socket.id = myUserID;
                users[socket.id] = {"name" : myUserName, "id" : socket.id };
                console.log("Logged in. These users are online:", users);
        });


//Checking user to connect with
        socket.on("checkUserConnect", function(userName, fn){
                connection.query("SELECT * FROM users WHERE name= '" + userName +"' ", function(error, results){
                                if (error) {
                                        console.log(error);
                                }
                                else if (results) {
                                        console.log(results);
                                        fn({result: results[0]});
                                }
                });
        });


//Connect to user
        socket.on("connectToUser", function(myUserName, UserID){
                var userWithRequest = myUserName;
                console.log("98", UserID, userWithRequest);
                socket.broadcast.to(UserID).emit("connectNotification", userWithRequest);
        });


//Testing
        socket.on("test", function(){
                console.log("Reached testing");
        });


  socket.on('disconnect', function(){
    console.log('user disconnected', socket.id);
  });
});


via JonasSH

No comments:

Post a Comment