socket io tutorial - Example Server Side code for handling Users - socket io android - socket io client



Example Server Side code

  • Firstly it's important to note that when a new socket is created it is assigned a unique Id which is retrieved by calling socket.id. This id can then be stored within a user object and we can assign an identifier such as a username which has been used in this example to retrieve user objects.
/**
 * Created by Liam Read on 27/04/2017.
 */

var express = require('express');
var app = express();
var http = require('http').Server(app);
var io = require('Socket io')(http);


function User(socketId) {

    this.id = socketId;
    this.status = "online";
    this.username = "bob";

    this.getId = function () {
        return this.id;
    };

    this.getName = function () {
        return this.username;
    };

    this.getStatus = function () {
        return this.status;
    };

    this.setStatus = function (newStatus) {
        this.status = newStatus;
    }
}

var userMap = new Map();

/**
 * Once a connection has been opened this will be called.
 */
io.on('connection', function (socket) {

    var user;

    /**
     * When a user has entered there username and password we create a new entry within the userMap.
     */
    socket.on('registerUser', function (data) {

        userMap.set(data.name, new User(socket.id));

        //Lets make the user object available to all other methods to make our code DRY.
        user = userMap.get(data.name);
    });

    socket.on('loginUser', function (data) {
        if (userMap.has(data.name)) {
            //user has been found

            user = userMap.get(data.name);
        } else {
            //Let the client know that no account was found when attempting to sign in.
            socket.emit('noAccountFound', {
                msg: "No account was found"
            });
        }
    });

    socket.on('disconnect', function () {
        //Let's set this users status to offline.
        user.setStatus("offline");
    });

    /**
     * Dummy server event that represents a client looking to send a message to another user.
     */
    socket.on('sendAnotherUserAMessage', function (data) {

        //Make note here that by checking to see if the user exists within the map we can be sure that when
        // retrieving the value after && that we won't have any unexpected errors.
        if (userMap.has(data.name) && userMap.get(data.name).getStatus() !== "offline") {
            var OtherUser = userMap.get(data.name);
        } else {
            //We use a return here so further code isn't executed, you could replace this with some for of
            //error handling or a different event back to the user.
            return;
        }

        //Lets send our message to the user.
        io.to(OtherUser.getId()).emit('recMessage', {
            msg: "Nice code!"
        })
});


});
click below button to copy the code. By Socket tutorial team

This is by no means a complete example of even close to what is possible but should give a basic understanding of an approach to handling users.


Related Searches to Example Server Side code for handling Users