yiicod / yii2-socketio

46 stars 30 forks source link

Client to Server Handling #9

Closed neovash23 closed 6 years ago

neovash23 commented 6 years ago

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});

lexxorlov commented 6 years ago

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

neovash23 commented 6 years ago

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.