laravel / ideas

Issues board used for Laravel internals discussions.
940 stars 28 forks source link

Blade templates, websockets and virtual DOM #1851

Open 0248991 opened 5 years ago

0248991 commented 5 years ago

Hey, I suggest implementing virtual DOM and WebSocket annotations to blade templates. Additionally, I suggest simplifying the use of current WebSocket implementation.

Something like this(in pseudo-code):

/app/channels/MyChannel.php

class MyChannel extends Channel {

    function canConsumer($user) {
        return $user->id === 2; //whatever
    }

    function registerConsumer($user) {
        this.registerConsumer($user->id);
    }

    function unregisterConsumer($user) {
        this.unregisterConsumer($user->id);
    }

    function consume($user, $event) {
        $user->updated_at = Carbon.now(); // whatever
        this.propagateToBlade($event);
    }

    function canProducer($user) {
        return $user->id === 2; //whatever
    }

    function registerProducer($user) {
        this.registerProducer($user);
    }

    function produce($data) {
        this.produce($data);
    }
}

UserController.php

class UserController {
    ...
    function store(Request $request) {
        $user = $request->fill()->save();
        $privateChannel = MyChannel::get;

        if($privateChannel->canConsumer($user)) {
            $privateChannel->registerConsumer($user);
        }
        if($privateChannel->canProducer($user)) {
            $privateChannel->registerProducer($user);
        }
        $privateChannel->produce(['info=> 'Hey, I just registered!''])
    }
    ...
}

myView.blade.php

<ul>
@for($item in $data) //data from controller, filled in the time of request
<li>{{$item}}</li>
@endFor)

@Channel('MyChannel', 'canConsumer')->$event //each time the event is fired on the channel, this will render new <ul> for it
<li>$event</li>
@EndChannel
</ul>
oprypkhantc commented 5 years ago

Blade is a template engine. It generates HTML with provided data.

WebSockets need JS to work. Your idea needs a whole front-end framework.

No.