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;
public function __construct($name){
$this->name = $name;
}
public function broadcastOn(){
return ['test-channel'];
}
}
The contents of the node server file:
var server = require('http').Server();
var io = require('socket.io')(server);
var Redis = require('ioredis');
var redis = new Redis();
redis.subscribe('test-channel');
redis.on('message' , function(channel , message){
message = JSON.parse(message);
io.emit(channel + ':' + message.event , message.data );
});
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>
var socket = io('http://192.168.10.10:3000');
socket.on('test-channel:App\\Events\\UserHasRegistered' , function(data){
console.log(data);
});
</script>
Windows 10 Homestead.
?