0

I am currently following a link to the Yii2 framework extension called yiicod/yii2-socketio on https://www.yiiframework.com/extension/yiicod/yii2-socketio.

My objective is to implement a notification feature using this extension along with a ReactJS client. I have configured the server to listen on port http://localhost:1367. However, when the client attempts to establish a connection, a "connection refused" error is thrown, as shown in the attached file.

To troubleshoot the issue, I ran the command sudo netstat -tulpn to check which port the socket server is running on, but the result did not show any WebSocket server running on port 1367.

Server

Console configuration

`'controllerMap' => [
    'socketio' => [
        'class' => \yiicod\socketio\commands\SocketIoCommand::class,
        'server'=>  'http://localhost:1367'
        // 'server' => 'unix:///var/run/socketio.sock',
    ],
],
  'components' =>[
        'broadcastEvents' => [
            'class' => \yiicod\socketio\EventManager::class,
            'nsp' => 'some_unique_key',
            // Namespaces with events folders
            'namespaces' => [
                'app\socketio',
            ]
        ],
        'broadcastDriver' => [
            'class' => \yiicod\socketio\drivers\RedisDriver::class,
            'hostname' => 'localhost',
            'port' => 6379,
        ],    
    ]`

Socket controller

`  public function actionNotify()
    {
  \yiicod\socketio\Broadcast::emit(CountEvent::name(), ['count' => 10]);
  }`

CountEvent `

<?php

namespace app\events;

use yii\base\Event;
use yiicod\socketio\events\EventInterface;
    use yiicod\socketio\events\EventPubInterface;

   class CountEvent implements EventInterface, EventPubInterface
    {
        /**
         * Channel name. For client side this is nsp.
         */
        public static function broadcastOn(): array
        {
            return ['notifications'];
        }
    
        /**
         * Event name
         */
        public static function name(): string
        {
            return 'update_notification_count';
        }
            
        /**
         * Emit client event
         * @param array $data
         * @return array
         */
        public function fire(array $data): array
        {
            return $data;
        }
    }
`

Client

`import io from 'socket.io-client';


 useEffect(() => {
var socket = io("http://localhost:1367/notifications"); // i have tried ws://localhost:8085/notifications too 

socket.on('update_notification_count', function() {
    console.log('Connected to Socket.IO server');
});
  }, []);
`

0 Answers0