docs/NIM_PKJS

Nim Full-Stack Development (PebbleKit JS in Nim)

Nebble supports writing both the watchapp (C target) and the phone-side component (JavaScript target) entirely in Nim. This allows for seamless code sharing, type-safe communication, and a unified developer experience.

Project Structure

When you create a new project with nebble new, the following files are generated:

Sharing Code

The most powerful feature of the full-stack approach is sharing enums and constants between the watch and phone.

Message Keys

Nebble automatically generates AppMessageKey enums in src/gen/app_keys.nim. You can import this file in both your watch code and your pkjs.nim code.

In `pkjs.nim`:

import nebble/pkjs
import gen/app_keys

proc onMessage(e: AppMessageEvent) {.cdecl.} =
  # Check for a specific key using the shared enum
  if e.payload.hasOwnProperty("WatchReady"):
    let data = newJsObject()
    data[cstring"JSReady"] = 1.toJs()
    Pebble.sendAppMessage(data)

API Reference (nebble/pkjs)

The nebble/pkjs module provides type-safe bindings for the PebbleKit JS environment.

Events

Communication

System

Build Process

When you run nebble build, the CLI performs the following steps:

  1. Compiles src/<app_name>.nim to C for the watch.
  2. Compiles src/pkjs.nim to src/js/pebble-js-app.js using the Nim JS backend.
  3. Bundles everything into the final .pbw file.

Running nebble clean will remove the generated JavaScript file along with other build artifacts.