Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Homestead , Socket.io Connection Refused

I`m using laravel Event broadcasting , socket.io , node.js , and redis to pass notifications to the client side in real time.

The code is fairly simple, when i make a get request to '/' on the server an event will be fired and some data will be broadcast to all browsers(client side) which listening to this event on a channel(test-Channel).

The Routes.php content:

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

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

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

The 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 node Server file content :

/*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 is working fine and the data is passed to the the node server through redis but on the client side where i use socket.io to listen to a specific channel , i get this weird error

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

The Client side :

<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>

I`m Using Windows 10 and Homestead virtual box.

any Help ?

like image 837
Abd El-Rahman Rafaat Avatar asked Sep 27 '22 09:09

Abd El-Rahman Rafaat


2 Answers

i had this same issue and i solved it opening port 3000 in VirtualBox. For that, you should go into homestead settings inside adn then Network->Port Forwarding and there put 3000 on both ports.

like image 193
akatche Avatar answered Sep 30 '22 13:09

akatche


did you try to ssh by "vagrant ssh" to your virtual machine and start your server-file from there? A had the same problem while i was starting my server file from windows- or git-command

like image 37
Thommis Avatar answered Sep 30 '22 13:09

Thommis