yiicod / yii2-socketio

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Client to Server Handling

neovash23 opened this issue · comments

Hi,

I have created a receiver from Client to Server connection.
For some reason the function handle() is getting triggered twice, which is resulting duplicate messages when I save to a model.

What am I doing wrong?

Socketio version: 2.0.4

<?php
namespace backend\socketio\events;
use yiicod\socketio\events\EventSubInterface;
use yiicod\socketio\events\EventInterface;
use yiicod\socketio\events\EventPolicyInterface;
use backend\models\Notification;
use yiicod\socketio\Broadcast;
class ConnectedEvent implements EventInterface, EventSubInterface, EventPolicyInterface
{

    /**
     * @var $count
     */
     protected $count = 1;

    /**
     * Changel name. For client side this is nsp.
     */
    public static function broadcastOn(): array
    {
        return ['notifications'];
    }

    /**
     * Event name
     */
    public static function name(): string
    {
        return 'connected';
    }

    public function can($data): bool
    {
        //Broadcast::emit('update_notification_count', ['data'=>$data,'count'=>12312376127843]);
        // Check data from client
        return true;
    }

    /**
     * Emit client event
     * @param array $data
     * @return array
     */
    public function handle(array $data)
    {
        // Mark notification as read
        // And call client update

        $notification = new Notification;
        $notification->message = $data['message'];
        $notification->save();

        //$data['message'] .= $this->count;
        //Broadcast::emit(\backend\socketio\events\FlashMessageEvent::name(), $data);
    }

}

socket.emit('connected',{message:'hey',userId:1});

Check your process, i think you have duplicate of daemons. You have to delete all nodejs and php daemons and after run them again.

Thank you! it turns out that, a pm2 is running under root. and another one in a non-root account. weird that pm2 treat them as separate though.