Sometimes you just need to look at the same code with someone: a quick pairing session, an interview question, a snippet a colleague can't get working. Screen sharing is laggy and nobody else can type. Full collaborative IDEs are heavy for a five-minute job.
So I built Code Share, a share-a-link code editor, as part of ToolkitHub. This post is about the architecture, including the shortcuts I took on purpose and where they stop working.
Rooms are just URLs
There is no sign-up. Opening https://code.toolkithub.in/anything puts you in a room called anything; opening the bare domain generates a random room ID and pushes it into the address bar. Sharing the room means sharing the URL. You pick a display name, and everyone in the room sees each other's presence and cursors.
The sync model: whole document, last write wins
The editor is Ace in React, connected to a Node server over Socket.IO. When you type, the client sends the full document:
// server
socket.on('code-change', async ({ roomId, code, language } = {}) => {
if (!inRoom(roomId) || typeof code !== 'string') return;
// Broadcast first so collaborators aren't held up by the database write
socket.to(roomId).emit('code-update', { code, language, senderId: socket.id });
try {
await roomManager.updateRoomCode(roomId, code);
} catch (err) {
socket.emit('error', 'Your latest changes could not be saved.');
}
});
Two choices in those few lines matter:
- Broadcast before persisting. Other people see your keystroke without waiting for the database round trip. The write happens afterwards, and if it fails only the sender is told.
- Send the whole document, not a diff. This is the big shortcut. It's trivial to get right: there is no operation history to transform and no ordering problem, and a reconnecting client just receives the current text.
The cost is that it's last-write-wins. If two people type in the same instant, one keystroke can overwrite the other. For snippets and pairing, where people naturally take turns, that almost never shows. For long documents with many simultaneous editors you want a real merge algorithm: operational transformation, or a CRDT library such as Yjs or Automerge, which merges concurrent edits character by character. Cursors are separate, lightweight cursor-move events that the server relays to everyone else in the room.
Passwords without accounts
A room can be locked with a password. It's hashed with Node's built-in scrypt and a random salt, and checked with a constant-time comparison:
const crypto = require('crypto');
function hashPassword(password) {
const salt = crypto.randomBytes(16).toString('hex');
const derived = crypto.scryptSync(password, salt, 64).toString('hex');
return `${salt}:${derived}`;
}
function verifyPassword(password, stored) {
const [salt, key] = stored.split(':');
const keyBuf = Buffer.from(key, 'hex');
const derBuf = crypto.scryptSync(password || '', salt, 64);
return keyBuf.length === derBuf.length && crypto.timingSafeEqual(keyBuf, derBuf);
}
No dependency needed, and timingSafeEqual avoids leaking how many characters matched.
Running a site full of other people's content
A link-based editor is user-generated content, and that comes with obligations. A few things I'd do from day one:
- Expire rooms. Rooms with nobody in them are deleted after a day of inactivity, along with their files. Nobody needs a scratch pad from last month, and it limits what can pile up.
-
Keep it out of search. Every response carries
X-Robots-Tag: noindex. A detail that's easy to get wrong: robots.txt must still allow crawling. If crawlers are blocked, they never read the noindex and can still list the bare URLs. - Restrict uploads. Shared files are size-limited, and executables and installers are rejected so the domain can't be used to hand out malware.
- Give people a way to report abuse. A "Report this room" link that opens an email with the room URL filled in costs nothing to add.
Try it
Open code.toolkithub.in, send the link to someone, and type. There's built-in chat, a choice of themes and keymaps (VS Code, Sublime, Vim, Emacs) in Settings, and a Markdown preview. Please don't paste secrets: anyone with the link can open an unlocked room.
Disclosure: I built Code Share as part of ToolkitHub, a set of free developer tools.
Top comments (0)