Ziex - Full-stack web framework for Zig!

Here is Server-Action Demo! But looking for feedback on the most reasonable signature.

https://youtu.be/rLaKDNwl2FI

Client/Server Event Handler Signature
  • fn(*zx.Event.Server) void | fn(*zx.Event.Client) void
  • fn(*zx.client.Event) | fn(*zx.server.Event)
  • fn(*zx.ClientEvent) | fn(*zx.ServerEvent)
  • fn(*zx.Event) | fn(*zx.ServerEvent)
  • fn(*zx.Event.Client) | fn(*zx.Event.Server)
0 voters

Code Sample:


pub fn ProfileForm(ctx: *zx.ComponentContext) zx.Component {
    const count = ctx.state(i32, 0);

    return (
        <div @allocator={ctx.allocator} style="display: inline-flex; flex-direction: column">
            <button onclick={clientEvent} value="I'm on the client">Client Event</button>
            <button onclick={serverEvent} value="I'm on the server">Server Event</button>
            <h1>{count.get()}</h1>
            <button onclick={ctx.bind(serverIncrement)} value="I'm on the server">Increment: Server</button>
            <button onclick={ctx.bind(clientIncrement)} value="I'm on the server">Increment: Client</button>
        </div>
    );
}

fn clientEvent(ctx: zx.EventContext) void {
    const val = ctx.value() orelse "";
    std.log.info("onclick: {s}\n", .{val});
}

fn serverEvent(ctx: zx.ServerEventContext) void {
    const val = ctx.value() orelse "";
    std.log.info("onclick: {s}\n", .{val});
}

fn clientIncrement(ctx: *zx.EventContext) void {
    const val = ctx.state(i32);
    val.set(val.get() + 1);
    std.log.info("increment: {d}\n\n", .{val.get()});
}

fn serverIncrement(_: zx.ServerEventContext, sc: *zx.StateContext) void {
    const val = sc.state(i32);
    val.set(val.get() + 1);
    std.log.info("increment: {d}\n\n", .{val.get()});
}
4 Likes

Just saw this and looks very interesting, it would be cool if you can swap the renderer and use it as TUI framework like is common with react, or even gui using raylib or similar

Hey, thank you for taking a look! This was already in the future scope (though TUI is the last in the priority unless someone contributes) and things are designed in a way to easily and new renderer, currently there are renderer for browser (HTML Document) and renderer for server (HTML String). And there is an impressive Zig library that renders markdown on the terminal and it’s pretty good. GitHub - JacobCrabill/zigdown: Markdown toolset in Zig ⚡ · GitHub

2 Likes

Thanks all for voting! I got the API laid out and is pretty happy about how it ended up!

2 Likes

Rebuilt zx dev command along with DevServer, Error Overlay, cleaner TUI for error printing and source mapping to actual .zx file!

Thanks to https://codeberg.org/ziglang/zig/src/branch/master/lib/std/Build/WebServer.zig!


Demo: https://youtu.be/ZQue2FvCZ3E

1 Like

Release with Zig 0.16.0 migration!

7 Likes

Ziex v0.1.0-dev.1570 release with Zig 0.17.0 migration!

Tooling

  • Ziex CLI: incremental builds, improved reliability with BSP, cleaner commands, improved Windows support.
  • Ziex LSP: tag/attribute complete and hover, improved VSCode with better LSP integration.
  • Ziex Devtools: click/hover highlight with auto-scroll, live host data, refreshed UI.

Core

  • Richer client forms and events, optional ZXON fields, shared page/API routes, broader caching.
  • WASM: blocking fetch (JSPI), optional off / optimize, stabler WASI export.
  • Modularized `httpz` and added pure `std` backend with the ability to optionally use `std` backed.

Docs & site

  • Playground app mode and updated Zig/WASI compiled from official Zig repository.
2 Likes

Ziex 0.1.0 has arrived!

Today is our first public release and Ziex’s first birthday.

4 Likes

Ziex DevTools is now on Firefox Add-Ons!

2 Likes

First and foremost, I just wanna say that I have no experience in web development (and just a little with zig), but I have a present need to do so.
Please do not get offended with my question. I am not attempting to make a value judgment regarding Ziex. My mind would only like to conceive of the difference in approach between Ziex versus zio + dusty + zt. I had just chosen the later to start doing the web app I was required to develop, before learning about the existence of Ziex.
Thank you sir.

All types of question are fine, there is nothing to get offended about, even if you want to do value judgment that is perfectly reasonable too.


I’ll have high level fundamentals first which may sound cryptic to you but will included before I go to details:

zt: A templating language. (partially competing with Ziex’s .zx syntax for the server side rendering)
dusty: A http server/client. (can be used with Ziex as well, by default Ziex uses http.zig but provides ability to switch to std.http.Server or to Dusty if wanted, and uses JS bindings when deployed to Edge runtime - Cloudflare/Vercel/AWS Edge Lambda)
zio: Something not relevant to the comparison, you can use that with Ziex as well.

Ziex: Full-stack web framework, includes templating language (.zx) (comparable to zt), that gets rendered in the server as well as in the browser (for interactive things). Ziex has it’s own server (by default it uses http.zig, but configurable to use std.http.Server or Dusty), but Ziex provides it’s own interface to build routes with file system routing support.


As you can see there are many overlaps (in fact can be the same thing in case of server), however Ziex covers broader and more advance full-stack use cases, like client side interactivity that you can write with Zig, server actions, hydration, streaming, file system routing, and many more. It also includes built-in zx.db and zx.kv, and Ziex provides official support to deploy to Cloudflare Workers (WASI target).

I’ll share some Ziex examples in here with you:

  1. Counter
pub fn Page(ctx: zx.PageContext) !zx.Component {
    return (
        <main @allocator={ctx.arena}>
            <Counter @client />
        </main>
    );
}
pub fn Counter(ctx: *zx.ComponentCtx(void)) zx.Component {
    const count = ctx.state(i32, 0);
    return (
        <div @allocator={ctx.allocator} class="counter">
            <button onclick={ctx.bind(increment)}>
                Click Me: {count}
            </button>
        </div>
    );
}
fn increment(e: *zx.client.Event.Stateful) void {
    const count = e.state(i32);
    count.set(count.get() + 1);
}

The counter logic (fn increment) runs in the browser with Zig compiled to web assembly.

  1. Routing

app/routes/api/route/zig → /api (GET/POST)

pub fn GET(ctx: zx.RouteContext) !void {
    try ctx.response.json(.{ .status = "ok" }, .{});
}

pub fn POST(ctx: zx.RouteContext) !void {
    const Data = struct { id: u32 };
    const data =
        try ctx.request.json(Data, .{});

    try ctx.response.json(.{
        .id = if (data) |d| d.id else 0,
        .created = true,
    }, .{});
}

The rest of the things, I’ll let you try in the playground, you can run Ziex in the browser and play around here.


The above was the concrete difference. Now these are my opinions:

zt can be a good option when you don’t need a full-stack framework e.g. you If your app is mostly “render HTML on the server and move on,” zt should be lighter and faster in that case. You can still choose Ziex if you like the .zx syntax over zt for the same use case. Otherwise Ziex will be something you can choose when you want the broader full-stack pieces (client interactivity in Zig, file-system routes, zx.db/zx.kv, edge/WASI deploy) etc.

3 Likes