Nitro

Features

Browser APIs

Nitro::browser() gives your methods the browser's own APIs, written in PHP. You can use the clipboard, local and session storage, a canvas to draw on, and WebSockets.

On this page

Some things only exist in the browser. Nitro::browser() lets your browser methods use them from PHP. Nitro checks what you write when it builds the bundle, and compiles it to the browser's own calls. The bundle only includes the APIs your application uses. On the server, Nitro::browser() throws an exception.

Nitro is Nitro's facade. In a class, import it with use Nitro\Facades\Nitro;. In a view, write Nitro::browser() as it is: Laravel registers Nitro as an alias, so a view needs no @use.

Storage, the clipboard and a canvas live, in this page View source ↓

Drawn on a canvas

Type a note, then reload the page. The note was saved in this browser's local storage, and mounted() reads it back. Try copying it to the clipboard. When you move the sliders, the chart is drawn again on the canvas.

The component

Browser + server
<?php

namespace App\Nitro\Components;

use Nitro\Component;
use Nitro\Facades\Nitro;

class BrowserDemo extends Component
{
    public string $note = '';

    public bool $copied = false;

    public array $values = [3, 7, 5, 9];

    /** Runs in the browser once the component is live. It reads the note saved last time, and draws the chart. */
    public function mounted(): void
    {
        $this->note = Nitro::browser()->storage()->get('docs-note', '');
        $this->draw();
    }

    public function updated(string $property): void
    {
        if ($property === 'note') {
            Nitro::browser()->storage()->set('docs-note', $this->note);
            $this->copied = false;
        }

        if (str_starts_with($property, 'values')) {
            $this->draw();
        }
    }

    public function copy(): void
    {
        Nitro::browser()->clipboard()->write($this->note, fn () => $this->copied = true);
    }

    public function draw(): void
    {
        $canvas = Nitro::browser()->canvas('chart')->size(260, 110)->clear()->fillStyle('#3b82f6');

        foreach ($this->values as $i => $value) {
            $canvas->fillRect(12 + $i * 62, 108 - $value * 10, 46, $value * 10);
        }
    }
}

Available APIs

API Methods
clipboard() write($text, $then) and read($then) . They work on https pages and on localhost
storage() , session() localStorage and sessionStorage: get($key, $default) , set($key, $value) , has($key) and forget($key) . Values are stored as JSON
canvas($ref) The 2D context of a <canvas nitro:ref="..."> , with the context's own method names ( fillStyle , fillRect , arc , fillText , ...). size($width, $height) sets its size in CSS pixels, scaled for the screen
socket($url) A WebSocket, one per URL and component: onMessage($then) , onOpen($then) , onClose($then) , send($data) and close() . JSON messages arrive as arrays. The socket reconnects after a drop, and closes when the component leaves the page

Callbacks are closures. When a callback changes a property, the component renders again as it does for any other change, just like $this->copied = true in the example above.

Using WebSockets

PHP Browser
use Nitro\Facades\Nitro;

public function connect(): void
{
    Nitro::browser()->socket('/ws/orders')
        ->onMessage(fn (array $order) => $this->orders[] = $order)
        ->send(['subscribe' => 'orders']);
}

Using an API from a view

nitro:ref gives an element a name that canvas() can use, and nitro:init calls a method once the element appears. An event handler in a view can also call an API directly:

Blade
<canvas nitro:ref="chart" nitro:init="draw"></canvas>
<button nitro:click="Nitro::browser()->storage()->forget('draft')">Discard the draft</button>

What stays in the browser

Storage and the clipboard belong to the visitor's browser, and the server never sees them. To send something to the server, pass it to a #[Server] method.

Next steps