Skip to main content
⠀⢀⣤⣤⣤⣤⣀ ⠀⠀⢸⣿⣿⣿⣿⣿⣷⡀ ⠀⠀⠘⠉⠉⠙⣿⣿⣿⣷ ⠀⠀⠀⠀⠀⠀⢸⣿⣿⣿⣧ ⠀⠀⠀⠀⠀⠀⣼⣿⣿⣿⣿⣆ ⠀⠀⠀⠀⠀⣼⣿⣿⣿⣿⣿⣿⡀ ⠀⠀⠀⠀⣴⣿⣿⣿⠟⣿⣿⣿⣷ ⠀⠀⠀⣰⣿⣿⣿⡏⠀⠸⣿⣿⣿⣇ ⠀⠀⢠⣿⣿⣿⡟⠀⠀⠀⢻⣿⣿⣿⡆ ⠀⢠⣿⣿⣿⡿⠀⠀⠀⠀⠀⢿⣿⣿⣿⣿⡇ ⢀⣾⣿⣿⣿⠁⠀⠀⠀⠀⠀⠈⠿⣿⣿⣿⡇
Background image

Litepad RTC Editor

In this experiment you can find the lightest-ever version of a real-time collaborative text editor. This project does not account for any complex features like user authentication or document persistence. It's objective is to be as simple as possible.

Open demo
  • Experimental
  • JavaScript
  • WebSockets
Litepad RTC Editor

About this Demo

The Litepad editor is just 100 lines of code, yet it is a fully functional real-time collaborative text editor. It uses WebSockets to share messages across all the connected clients.

The Code

In the client, the editor's change and socket's refresh/change are handled like this:

js

let socket = io.connect()
//Editor
editor.on("change", function (i, op) {
  socket.emit("change", op)
  socket.emit("refresh", editor.getValue())
})

//Socket events
socket.on("refresh", function (data) {
  editor.setValue(data.body)
})
socket.on("change", function (data) {
  console.log("Change received:", data)
  editor.replaceRange(data.text, data.from, data.to)
})

In the server, a socket connection, refresh and change are handled as follows:

js

let body = "Welcome to Litepad RTC Editor"

io.sockets.on("connection", function (socket) {
  socket.emit("refresh", { body }) //Propagate the value to the connected client

  //Socket events
  socket.on("refresh", function (_body) {
    body = _body
  })
  socket.on("change", function (op) {
    if (op.origin == "+input" || op.origin == "paste" || op.origin == "+delete")
      socket.broadcast.emit("change", op)
  })
})

That's it! This is probably the simplest implementation of a real-time collaborative text editor that you can find. It is a great starting point if you want to create your own real-time collaborative editor or to create -like I did- my own Virtual Shell in the browser with authentication and persistence.

See Also

If you are interested in a more complex implementation of a real-time collaborative text editor, you can check the following projects:

  • Etherpad Lite: A full-featured collaborative editor with a rich set of features.
  • Code Sync: A real-time collaborative editor with a nice UI.
  • Firepad: A Firebase-powered collaborative editor.