RealTime Collaborative Text-Editor in Nodejs & Socket.io

Abhishek Kaushik picture Abhishek Kaushik · Jun 17, 2014 · Viewed 8.2k times · Source

I am developing a real-time text editor with paragraph locking property similar to https://quip.com/. in socket.io and nodejs.

It means when you write onto a given paragraph, other collaborators cant edit it.

Moment you hit enter or move cursor to a new line that paragraph becomes Editable for other Collaborators.

I am quite stuck after this. I am thinking a nice approach to move further. Suggestions please.

Below is my code which works perfectly. Till now i can get list of all collaborators and broadcast the content of editor to other collaborators.

index.html

<!DOCTYPE html>
<html>
<head>
    <title>Connected Clients</title>
    <!--<meta charset="UTF-8"> -->
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
    <!--<script type="text/javascript" src="jquery.js"></script>  -->
    <script src="/socket.io/socket.io.js"></script>
</head>

<body>
    <textarea id="editor" style="height:200px;width:300px">
        Thinknest Pragraph locking Test sample !
    </textarea>

    <script>
    function msgReceived(msg){
        //clientCounter.html(msg.clients);
        document.getElementById('people').innerHTML=msg.uid;
        //console.log(msg.id);  
    }

    var clientCounter;  

    $(document).ready(function () {
        clientCounter = $("#client_count");
        var socket = io.connect(
                         'http://localhost:5000', 
                         {'sync disconnect on unload':true}
                     ); 
        var uId=prompt("enter your userId",'');
        socket.emit('collabrator',uId);

        socket.on('message', function(msg){
            msgReceived(msg);
        });

        socket.on('online_collabs',function(data){  
            $('#online_ppl').html(data);
            clientCounter.html(data.length);
        });

        socket.on('remained_collabs',function(data){
            $('#online_ppl').html(data);
            clientCounter.html(data.length);
        });

        socket.on('note_collabs',function(data){
            $('#note_colabs').html(data);
        });

        socket.on('updated_para',function(data){
            //$('#editor').append(data);
            document.getElementById('editor').innerHTML=data;
        });

        $('#editor').on('keydown',function(){
            //var para=$('#editor').value;
            var para= $('#editor').val();
            //var para=document.querySelector('[contenteditable]');
            // var text=para.textContent;
            socket.emit('para',{paragraph:para});
        });
    });  
    </script>

    <p><span id="client_count">0</span> connected clients</p><br/>
    <ul id="people"></ul>
    <h3>Online Collaborators</h3>
    <span id="online_ppl"></span> <br>
    <h3>Note Collaborators</h3>
    <span id="note_colabs"></span>
</body>
</html>

server.js

var app = require('express')()
  , server = require('http').createServer(app)
  , io = require('socket.io').listen(server);

server.listen(5000);

app.get('/',function(req,res){
    res.sendfile("./index.html");
});

var activeClients = 0;
var Collaborators=['Colab1','Colab2','Colab3'];
var people=[];

io.sockets.on('connection', function(socket){
    clientConnect(socket);

    socket.on('disconnect', function(){
        clientDisconnect(socket);
    });

    socket.on('para',function(data){
        //io.sockets.emit('updated_para',data.paragraph);
        socket.broadcast.emit('updated_para',data.paragraph);
    });
});

function clientConnect(socket){
    //activeClients +=1;
    var userSocketId=socket.id;
    check_Collaborator(socket);

    io.sockets.emit('message', {uid:userSocketId});
}

var online_collabs=[];

function check_Collaborator(socket){
    socket.on('collabrator',function(data){
        if(Collaborators.indexOf(data)!=-1){
            socket.data=data;

            if(online_collabs.indexOf(data)==-1) {
                online_collabs.push(data);
            }

            io.sockets.emit('online_collabs',online_collabs);
            io.sockets.emit('note_collabs',Collaborators);
        } else {
            console.log("collabrator not found");
        }
    });
}

function clientDisconnect(socket){
    var index=online_collabs.indexOf(socket.data)

    if(index>-1)
        online_collabs.splice(index,1);

    //activeClients -=1;
    //io.sockets.emit('message', {clients:activeClients});
    io.sockets.emit('remained_collabs',online_collabs);
}

Answer

JanS picture JanS · Jun 17, 2014

I saw this yesterday already. What exactly is your question? Do you want to know how to 'lock' a text area with javascript? I am confused as to why you put such a strong emphasis on node/socket.io in your question.

Also, next time please format your code. You want help, I get it, but then make it easier for others to help you.

What you have to do in order to make a paragraph not editable by others, I don't know. But let me suggest what I'ld do in socket.io:

Store each paragraph separately and remember who has a lock on it. For locking, I would use the sessionID in case users don't have to register. This would look something like this:

var paragraphs = {
    data : [
        {
            text: "this is an unlocked paragraph",
            lock: ""
        },
        {
            text: "this is a locked paragraph",
            lock: "oWEALlLx5E-VejicAAAC"
        }
    ]
}

Now, users will likely be allowed to add a paragraph before an existing one. Therefore you should keep an additional index like:

var paragraphs = {
    index : [
        1,
        0
    ],
    data : [
        {
            text: "this the second paragraph",
            lock: "oWEALlLx5E-VejicAAAC"
        },
        {
            text: "this is the first paragraph",
            lock: ""
        }
    ]
}

The amount of data being sent over the sockets should now be very small - altough with additional client/server-side logic.