0

I want to add a live notification in my application using Socket.io but I am having issues in integrating it in my application. When I run the application and looked at the browser log(console),

I have connection refused

(GET http://localhost:1500/socket.io/?EIO=3&transport=polling&t=1434954742052-24 net::ERR_CONNECTION_REFUSED)

my application is in localhost:130. I have read about CORS and applied the persmission in my controller.

header('Access-Control-Allow-Origin: *');
header("Access-Control-Allow-Headers: X-API-KEY, Origin, X-Requested-With, 
       Content - Type, Accept, Access - Control - Request - Method ");
header("Access-Control-Allow-Methods: GET, POST, OPTIONS, PUT, DELETE");
$method = $_SERVER['REQUEST_METHOD'];
if ($method == "OPTIONS") {
    die();
}

parent::__construct();

This is my server.js

var io = require('socket.io').listen(1500);
io.sockets.on('connection', function (socket) {
// Let everyone know it's working
socket.emit('startup', { message: 'I Am Working!!' });
});

This is my client.js

$(function() {
    window.MY_Socket = {
        // Instantiate the Socket.IO client and connect to the server
        socket: io.connect('http://localhost:1500'),

        // This just indicates that a Socket.IO connection has begun.
        startupMessage: function(data) {
            console.log(data.message);
        },
    }
});

Still I am receiving the error:net::ERR_CONNECTION_REFUSED. I've been searching for work arround but I can still figure our how to solve this. Looking forward for you help

sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
chkm8
  • 1,302
  • 1
  • 13
  • 34
  • Node server should send headers, not PHP. Check this http://stackoverflow.com/questions/24058157/socket-io-node-js-cross-origin-request-blocked – Damir Miladinov Jun 22 '15 at 06:58
  • thank you @DamirMiladinov,Its now working .Can you make your post as Answer to this post to that I could credit you. thanks – chkm8 Jun 22 '15 at 11:52

1 Answers1

0

Thanks to @DamirMiladinov for helping me,below is the code.

var io = require('socket.io').listen(1500); //for the socket
io.set('origins', "http://localhost:130"); //the port of the application
io.sockets.on('connection', function (socket) {
// Let everyone know it's working
socket.emit('startup', { message: 'I Am Working!!' });
});
chkm8
  • 1,302
  • 1
  • 13
  • 34