portfolio / jbend
JBend
A Java backend I wrote from scratch, starting at the socket. No Netty, no Jetty, no Spring. It is running on this server right now.
▶ Open the live demo Read the documentation Source on GitHub
- Built with
- Java, MySQL (JDBC), JavaScript
- When
- 2020 to 2022; version 2.0 in 2026
- Size
- About 8,500 lines of Java, no dependencies beyond the MySQL driver
Why I built it
I wanted to know what a web framework actually does, so I wrote one without using one. JBend starts with a non-blocking TCP server on Java NIO (ServerSocketChannel and a Selector) and everything else sits on top of that: HTTP, WebSockets, logins and a database layer.
It wasn't only an exercise. I used it to build the repair ticket system at RZ Tech & Web, where employees entered and tracked customer jobs on it, and it served an earlier version of this site.
An application is one config file
JBend's main method is two lines long. What an application consists of is written in config/application.conf: each block becomes a thread, and they start in order, each waiting for the one before it to be ready.
app-id:shop
dataserver:
source env:DB_DSN # MySQL, address taken from the environment
login:
sessions 7d # accounts, sessions, lockouts, bans
webserver:
listen 8080
home res/front
handler com.shop.Site # my class, extending JBend's HTTP server
upgrade /ws 8081
websocket:
listen 8081
bind 127.0.0.1
handler com.shop.LiveUpdates
app:
class com.shop.NightlyReport
The config language has its own lexer, parser and interpreter. A line it does not understand stops the startup with the line number, before anything is half-running. The configuration reference lists every block.
How it fits together
Launcher (reads config/application.conf) ├── DatabaseManager one MySQL connection, queries wait in a queue ├── LoginHandler accounts, sessions, lockouts, bans ├── HTTP server ─┐ ├── WebSocket ─┤ ├── TCP server ─┼── each a protocol on the same ├── Bridge ─┤ non-blocking server core ├── Proxy ─┘ ├── your threads a game loop, a scheduler └── Console commands while it runs: status, kick, ban, mysql
- One server core. Every server is the same class: one thread and one selector per port, with per-connection read buffers and write queues. Partial reads, large responses and slow clients are handled once, for every protocol.
- HTTP by hand. HTTP/1.1 with keep-alive, static files that cannot escape their directory, routes, cookies, sessions, and a handler hook with request and response objects.
- WebSockets by hand. RFC 6455: the handshake, masking, fragmented frames of any length, ping and pong, the closing handshake, origin checks.
- A database thread. Queries go into one queue and run as prepared statements. A query is an object whose
done()method runs when the result is in, so nothing blocks the network threads. - Accounts. Passwords are salted PBKDF2 hashes. Sessions are random tokens, stored hashed. Five wrong passwords lock an account and address out for five minutes.
- A bridge. WebSocket in, TCP out. It puts a plain TCP server in a browser without changing the server.
- An ORM, experimentally. A class that extends
CRUDObjectgets a table, and JBend adds or drops columns at startup until the two match.
What using it looks like
Application code extends one of JBend's protocols and is named in the config. A page with logic:
public class Site extends HTTP {
public Site(String home, int port) { super(home, port); }
@Override
public Response handle(ServerConnection c, Request req) {
if (req.path.equals("/hello"))
return Response.text("Hello, " + req.param("name", "world") + "!");
return null; // not mine: fall through to routes and static files
}
}
A database query, with the code that uses its result attached to it:
new ServerQuery(c, "SELECT x, y FROM players WHERE username = ?", name) {
public void done() {
c.sendMessage("pos:" + value("x") + "," + value("y"));
}
};
The live demo
kanetempleton.com/JBend/demo is one JBend process. This site's Go server passes requests under that address to it. Its config file starts a web server, two games and the plumbing between them:
- JBGame, the multiplayer tile world I built on JBend in 2021: a WebSocket handler and a game loop ticking five times a second.
- The 2018 Java game, my first multiplayer server: a shared map with walking and chat, running as a thread and reached from the browser through the bridge. The page shows every packet as it crosses.
It uses the same MySQL database as the rest of this site, in tables of its own.
Version 2.0
The 2020 version worked for the projects I built on it, on machines I controlled. Version 2.0, in 2026, is a pass over every class to make it something that can sit on the public internet, and it finishes the config language I had started and never committed. Some of what that pass turned up:
- All connections shared one read buffer, and a response went out in a single write, so large files arrived cut short. Every connection now has its own buffers and a send queue.
- Passwords were encrypted with a key that sat in the source code, and a user was identified by a cookie holding their name. Now: hashes and random session tokens.
- SQL was built by joining strings. Now: prepared statements with parameters.
GET /../../etc/passwdworked. Paths are now resolved and confined.- Four threads spun in loops that never slept, each holding a CPU core at 100%.
The full list is in the documentation.
Built on JBend
- RZ Tech & Web's repair ticket and support request system (2022)
- JBGame, a browser multiplayer game over WebSockets, playable in the demo
- jbend-proxy, a proxy that forwards each hostname to its own port, now a built-in block
Open the live demo Documentation Source on GitHub Back to the portfolio