Git Product home page Git Product logo

yii2-longpoll's Introduction

Yii2 longpoll

Implements long polling AJAX mechanism.

Latest Stable Version Total Downloads Build Status Scrutinizer Code Quality Code Coverage

Installation

The preferred way to install this extension is through composer.

Either run

php composer.phar require --prefer-dist izumi-kun/yii2-longpoll

or add

"izumi-kun/yii2-longpoll": "~1.0.0"

to the require section of your composer.json.

Basic Usage

Controller

class SiteController extends Controller
{
    public function actions()
    {
        return [
            'polling' => [
                'class' => LongPollAction::class,
                'events' => ['eventId'],
                'callback' => [$this, 'longPollCallback'],
            ],
        ];
    }
    public function longPollCallback(Server $server)
    {
        $server->responseData = 'any data';
    }
}

View

LongPoll::widget([
    'url' => ['site/polling'],
    'events' => ['eventId'],
    'callback' => 'console.log',
]);

Model

\izumi\longpoll\Event::triggerByKey('eventId');

Example

https://github.com/Izumi-kun/yii2-longpoll-example

License

BSD-3-Clause

yii2-longpoll's People

Contributors

izumi-kun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

yii2-longpoll's Issues

Missing client parameters

I'm using longpoll for update news content on page.
In view:

\izumi\longpoll\widgets\LongPoll::widget([
    'url' => ['polling'],
    'events' => ['addTranslationBlock'],
    'requestParams' => [
        'id' => $post->id,
        'block_id' => $lastblock,
    ],
    'callback' => 'addTranslationBlock',
]);

In controller:

  public function actions() {
        return [
            'polling' => [
                'class' => 'izumi\longpoll\LongPollAction',
                'events' => ['addTranslationBlock'],
                'callback' => [$this, 'longPollCallback'],
            ],
        ];
    }

    public function longPollCallback(\izumi\longpoll\Server $server) {
        $id = Yii::$app->request->get('id');
        $block_id = Yii::$app->request->get('block_id');
        if(!$id || !$block_id) throw new \yii\web\NotFoundHttpException();

        $blocks = PostBlock::find()->
            where(['post_id' => $id])->
            andWhere(['>', 'id', $block_id])->
            orderBy(['created' => SORT_ASC, 'order' => SORT_ASC])->
            all();

        foreach($blocks as $block) {
            $server->responseData .= $this->renderPartial('_block.php', [
                'block' => $block,
                'yellow' => true,
            ]);
            $lastBlockId = $block->id;
        }
        
        $server->responseParams = [
            'id' => $id,
            'block_id' => $lastBlockId,
        ];
    }

It's do request to server like polling?id=36&block_id=97&event-addTranslationBlock=17
It's work fine.
BUT!
If longpoll connection closed by timeout, server returns {"data":null,"params":{"event-addTranslationBlock":17}} and the next request is polling?event-addTranslationBlock=17.
Additional parameters like id and block_id are missing.

Server::sendContent() echo json with error.

src/Server.php, if replace lines 143, 144

echo dechex(strlen($json)), "\r\n", $json, "\r\n";
echo "0\r\n\r\n";

with

echo $json;

then works well, else Ajax respond is
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 2 of the JSON data

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.