2

I have a managed server at cloudways. I am trying to use socket.io.

client.php:

var socket = io('http://localhost:3000');
console.log(socket);

server.js:

var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);

server.listen(3000);

io.on('connection', function(socket){
console.log('is connected');
});

When visiting the page, I see two instances of 200 OK in the network section of the console. The console in the browser also prints the socket variable, which has "connected : true".

However, I DO NOT see "is connected" in the terminal (from server.js), it is just blank.

This site has worked locally, but these problems are appearing when trying to get it to work at Cloudways.

Any ideas?

I have tried switching "localhost" to both the domain name and the IP-address, and I have tried different ports, but 3000 is the only one that will let me start the server.js without error.

enter image description here

EDIT

Cloudways have confirmed that port 3000 should be used.

I have tried changing the url in io() to:

var socket = io('http://my-cloudways-url:3000'); (net::ERR_EMPTY_RESPONSE)
var socket = io('http://www.my-domain.club:3000'); (net::ERR_EMPTY_RESPONSE)
var socket = io('http://ip-adr:3000'); (net::ERR_EMPTY_RESPONSE)

And today I am getting

var socket = io('http://localhost:3000'); (net::ERR_CONNECTION_REFUSED)

I have also tried io.connect(using the same adresses / ports).

Bergkamp10
  • 265
  • 2
  • 10

1 Answers1

0

Maybe is your php index... Because you are setting the localhost and the port.

I'm not sure about how Cloudway works. But, when we run the server.js, some clouds give some url for access our projects...

In your cloudways try to use the official URL that they given to you access the project:

var socket = io.connect('url.from.cloudway');

For example, I'm using cloud9, and when I execute the server.js, return one url like:

Your code is running: https://demo-some-id.c9.io

And when I'm using socket.io, I need to set in my index the URL parameter inside the IO.

var socket = io('https://demo-some-id.c9.io');

You need to add the client-side socket.io.js version and add the script tag with the path, like:

<script src="js/socket.io.js"></script>

Obs.: Some clouds, you need to set the url in your <script> tag

<script src="https://demo-some-id.c9.io/socket.io/socket.io.js"></script>
Sayuri Mizuguchi
  • 5,250
  • 3
  • 26
  • 53