A
A
Alexander Ivanov2018-05-10 13:31:23
Node.js
Alexander Ivanov, 2018-05-10 13:31:23

Creating a chat on node?

installed node on the hosting, opened port 3000, the port is listening, when lptf.it:3000 or 185.53.160.215:3000 is accessed , the Hello Node.js Server! message is displayed. Everything seemed to be fine! but! I want to create a chat further, and in the root of the site I create a file (server) with code (example from the Internet, I'm new):

var express = require('express');
    var app = express();
    app.set('port', process.env.PORT || 3000);
    app.set('host', process.env.HOST || 'localhost');
    
    var logger = require('log4js').getLogger();
    var server = require('http').Server(app);
    var io = require('socket.io')(server);
    var port = 3000;


     app.use(express.static('/home/urlyogyj/public_html/views/chat.php'));
    app.get('/chat.php', function(req,res){
    res.sendFile('/home/urlyogyj/public_html/views/chat.php');
    });

    app.use('/fonts', express.static('/home/urlyogyj/node_modules/uikit/src/fonts/'));

     app.get('/socket.io.js', function(req,res){
    res.sendFile('/home/urlyogyj/node_modules/socket.io-client/dist/socket.io.js');
    });

    app.get('/jquery.js', function(req,res){
    res.sendFile('/home/urlyogyj/node_modules/jquery/dist/jquery.min.js');
     });

     app.get('/uikit.js', function(req,res){
    res.sendFile('/home/urlyogyj/node_modules/uikit/dist/js/uikit.min.js');
    });

    app.get('/uikit.css', function(req,res){
    res.sendFile('/home/urlyogyj/node_modules/uikit/dist/css/uikit.almost-flat.min.css');
    });

    app.get('/animate.css', function(req,res){
    res.sendFile('/home/urlyogyj/node_modules/animate.css/animate.min.css');
    });

    function usersCountToLog(){
    logger.info('User count: '+io.engine.clientsCount);
     }

     io.on('connection', function(socket){

    function setName(name){
        if(name != undefined && name != ''){
            socket.session = {};
            socket.session.userName = name;
            socket.session.address = socket.handshake.address; 
            socket.session.id = socket.id;          
            
            socket.broadcast.emit('newUser', socket.session);
            socket.emit('userName', socket.session);
            

            socket.emit('userList', io.length);

            logger.info('User '+socket.session.userName+' join from IP: '+socket.session.address);
            usersCountToLog();
            var clients = io.sockets.connected;


            var clientsList = {}
            for(var key in clients){
                if(clients[key].session)
                    clientsList[key] = clients[key].session;
            }
            
            socket.emit('clientsList', clientsList);
            console.log(clientsList);
        }
        else
            socket.emit('setName');
    }
    setName(null);
    socket.on('setName', function(name){
        if(name.length > 0)
            setName(name);
        else
            socket.emit('setName');
    });

    socket.on('message', function(msg){
        if(socket.session){
            if(socket.session.userName === null || socket.session.userName == '' || socket.session.userName == undefined){
                socket.emit('setName');
            }else{
                logger.trace('-----------');
                logger.trace('User: ' + socket.session.userName + ' |  
    Message: ' + msg);
                logger.trace('====> Sending message to other chaters...');

                socket.broadcast.emit('messageFromClients', msg,  
    socket.session.userName);
                socket.emit('messageToClients', msg,      
    socket.session.userName);
            }
        }
    });

    socket.on('disconnect', function(){
        if(socket.session){
            io.sockets.emit('userDisconnected', socket.session);
            logger.info('User '+socket.session.userName+' left chat');
            usersCountToLog();
        }
    });
    });

    console.log('server started, tipa...');

As you can see from the code, I wrote the addresses to the files, they are correct, in the console I restart the server with the
node /home/urlyogyj/public_html/app.js screen command
, after that I update the address lptf.it: 3000 and still no change, displays Hello Node.js Server !
I don't understand why, and where and how to track the errors.
What I really want to achieve:
before the authorization of any user, the chat is not needed, as soon as the user logs in on the site, the socket opens and on the site for all other users and guests the inscription is visible that the authorized user is online, and opposite his login there is a button to create a chat, but only for authorized users, when you click on it, a chat window opens, like on the VKontakte or Facebook site, the recipient of the message has a block with a list of everyone who sent him a message, the recipient clicks on the author and users start communication, each dialogue with a new user must launch its own chat window. and this chat exists on all pages of the site. after the user exits via the exit button or the tab is closed, the socket is closed, and all messages are written to the database!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question