English · Português
Note
Built during Alura's WebSocket courses: WebSockets with Node.js and WebSocket Authentication. Code comments are in Portuguese.
Several users open the same document and see each other's changes instantly, without reloading the page. All communication between browser and server, including sign-up and login, goes through Socket.IO events instead of HTTP routes.
- Collaborative editing: text typed by one user shows up for everyone else on the same document.
- Who's online: each document shows the list of connected users, updated as people join or leave.
- Create and delete documents: the home page list updates in real time for every client.
- Sign-up and login: passwords are stored with a
scrypthash and a random salt. Login returns a JWT stored in a cookie. - Protected access: a Socket.IO middleware validates the JWT during the handshake before granting access to documents.
- The user signs up (
register_user) and logs in (authenticate_user), receiving a JWT. - The client connects to the
/usersnamespace, sending the token in the handshake. TheauthorizeUsermiddleware checks the signature and rejects the connection if the token is invalid. - When a document is opened (
select_document), the socket joins a room named after the document and receives the text stored in MongoDB. - Each change (
text_editor) is saved to the database and relayed only to the other sockets in that room (text_editor_clients). - When someone joins or leaves, the server emits
users_on_documentwith the updated list. If the same user tries to open the document twice, they getuser_already_on_document.
- Rooms and namespaces: each document is a room, so edits don't leak between documents. Authenticated traffic lives in a separate namespace.
- Passwords with native
crypto:scryptSyncwith a 16-byte salt, compared withtimingSafeEqualto prevent timing attacks. - Events organized by domain: handlers live in
src/registerEvents/(start, document, register and login), and database access in its own modules.
| Layer | Technologies |
|---|---|
| Backend | Node.js, Express, Socket.IO |
| Database | MongoDB (official driver) |
| Auth | JWT (jsonwebtoken), crypto (scrypt) |
| Frontend | Plain HTML, CSS and JavaScript, served as static files |
Requirements: Node.js 18+ and a MongoDB instance (local or Atlas).
git clone https://github.com/giobroliatto/websocket-docs.git
cd websocket-docs
npm install
cp .env.example .env # fill in MONGODB_URI and SECRET_JWT
npm run devOpen http://localhost:3000. To see collaboration in action, open the same document in two tabs with different users.
| Variable | Description |
|---|---|
MONGODB_URI |
MongoDB connection string |
SECRET_JWT |
Secret used to sign the tokens |
src/
├── server.js # Express + HTTP server + Socket.IO instance
├── socket-back.js # Namespace and event registration
├── registerEvents/ # Handlers: start, document, register, login
├── middlewares/ # JWT validation on handshake
├── utils/ # Password hashing, JWT generation, connection tracking
├── db-connect.js # MongoDB connection
├── documents-db.js # Documents collection access
└── users-db.js # Users collection access
public/ # Login, sign-up, list and editor pages
Made by Giovani Broliatto.