Laravel Homestead, Socket.io connection refused

I use laravel Event broadcasting, socket.io, node.js and redis to send real-time notifications to the client side.

The code is quite simple, when I make a request to receive "/" on the server, the event will be fired, and some data will be transmitted to all browsers (on the client side) that listen to this event on the channel (CHANNEL test).

Contents of Routes.php:

Route::get('/', 'uses' => function () {

    Event::fire( new App\Events\UserHasRegistered('DummyData') );

    return view('test');
}]);

UserHasRegistered Event Class:

class UserHasRegistered extends Event implements ShouldBroadcast{
    use SerializesModels;

    public $name;

    /**
     * Create a new event instance.
     *
     * @return void
     */
    public function __construct($name){
        $this->name =  $name;
    }

    /**
     * Get the channels the event should be broadcast on.
     *
     * @return array
     */
    public function broadcastOn(){
        return ['test-channel'];
    }

}

The contents of the node server file:

/*General Configurations :
- Setting up the node Server 
- Server side Socket.io 
- node js Redis Client 
- instance of ioredis 
*/
var server  =  require('http').Server();
var io      =  require('socket.io')(server);
var Redis   =  require('ioredis');
var redis   =  new Redis();

/*listen to a channel , and when a Message Arrives we send it to All Listening clients via socket.io*/
redis.subscribe('test-channel');
redis.on('message' , function(channel , message){
    message = JSON.parse(message);
    // The Client Side Channel naming conversion => channelName:EventPublishedToServer => testChannel:UserHasRegistered
    io.emit(channel + ':' + message.event , message.data );
});

/*Booting Up the Server : port 3000 */
server.listen(3000 , function(){
    console.log('The Server Is Running');
});

Every thing works fine and the data is sent to the node server via redis, but on the client side, where I use socket.io to listen on a specific channel, I get this strange error

GET http://192.168.10.10:3000/socket.io/?EIO=3&transport=polling&t=1446018378941-633 net:: ERR_CONNECTION_REFUSED

:

<script type="text/javascript" src="/js/socket.io.js"></script>
    <script>
        /*Homestead IP Address , Port:3000 Node Server Port*/
        var socket = io('http://192.168.10.10:3000');

        /*When Data is sent to client side*/
        socket.on('test-channel:App\\Events\\UserHasRegistered' , function(data){
            console.log(data);
            //$('ul').append('<li><h5>'+ data.name +'</h5></li>');
        });
    </script>

Windows 10 Homestead.

?

+4
3

, 3000 VirtualBox. adn, Network- > Port Forwarding 3000 .

+2

ssh "vagrant ssh" ? A , windows- git -command

+1

You tried

<script type="text/javascript" src="/js/socket.io.js"></script>
<script>
    /*Homestead IP Address , Port:3000 Node Server Port*/
    var socket = io.connect('http://192.168.10.10:3000');

    /*When Data is sent to client side*/
    socket.on('test-channel:App\\Events\\UserHasRegistered' , function(data){
        console.log(data);
        //$('ul').append('<li><h5>'+ data.name +'</h5></li>');
    });
</script>
0
source

Source: https://habr.com/ru/post/1613445/


All Articles