ERR_CONNECTION_REFUSED While Running Server on Other Laptop - javascript

I can host a server on my laptop and it will run fine, but on other computers I get the error:
ERR_CONNECTION_REFUSED
Here is the code:
var express = require('express');
var app = express();
var server = app.listen(8000);
app.use(express.static('public'));
console.log('Sever runnning');
var io = require('socket.io').listen(server);
io.sockets.on('connection', newConnection);
It works fine on my computer, but fails elsewhere.

your local server working on your machine,app is running fine but on other laptop app is not running right? so on other laptop give your network IP then see the result.

Related

SocketIO is not working in Heroku Environment [Works in Localhost]

I developed chat program with NodeJS and SocketIO. Then I deployed to Heroku Server and I found that SocketIO is not working in Heroku.
I already checked the log in Heroku but there are nothing. But it is weird that it is working in localhost but not in Heroku environment. I think there are some problem with PORT Setting.
[Server Side Code]
/* Socket IO Settings */
const server = require('http').Server(express);
const io = require('socket.io')(server);
let port = process.env.PORT || 3001;
server.listen(port, function () {
console.log(`SocketIO Listening on port : ${port}`)
});
[Client Side Code]
/* Chat Functions */
var socket = io.connect(`${window.location.hostname}:3001`);
At first time I just use var socket = io.connect('http://localhost:3001'); code and it works well in Localhost Env. But after I deployed to heroku I add Heroku Variable to use it in production.
[PORT Setting]
NODEJS Server : 3000
SOCKET IO : 3001
Heroku process.env.PORT : 3001 (But When I check Heroku console it changes everytime when I reload the dynos and in Logs said me that the ports are already in use.)
I already read Heroku Document but it seems that I create NodeJS App with Express Generator I think it should be different solution. I already tried heroku document but it's not working
Set your server ip in server.listen function
var ip='127.0.0.1';
var port ='3001' ;
server.listen(ip,port, function() {console.log('server stsrt in ip: ' +ip+' and port: ' +port);}) ;

Correct ports for node server-client application with Heroku

Well, I'm near to finish my starting app... but port issue is hard...
My server.js has this
const port = process.env.PORT;
const express = require('express'), http = require('http');
const app = express();
const server = app.listen(port);
const io = require('socket.io').listen(server);
And now the issue. What should I do in the client?
var socket = io.connect('https://myweb.herokuapp.com:3000/');
var socket = io.connect('https://localhost:3000/');
I tried those two... the second one, localhost (but with 8080) works in local... but displays the error
GET http://localhost:3000/socket.io/?EIO=3&transport=polling&t=M9zP_yz net::ERR_CONNECTION_RESET
I read that the client is picking the CLIENT PC localhost and that fails, that has to pick the SERVER PC route... so I tried the first option, the application URL... because I suppose I don't have to use the server IP (I also don't have it). But nothing works...
Try using port 80, that should be the default port accessible outside heroku dynos: https://myweb.herokuapp.com:80/. If it does not work, try connecting without the port. Use the URL only: https://myweb.herokuapp.com

Connect to Heroku socket from another server/localhost

I have a nodejs/express app deployed on heroku with socket.io listening on port 80.
I want to connect to the app's socket from localhost which will be pushed to github-pages. I'm getting net::ERR_SSL_PROTOCOL_ERROR error in the GET request.
This is my server code deployed on heroku.
var app = require('express')();
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(80);
io.on('connection', function(){
});
My localhost is running on webpack-dev-server.
import io from 'socket.io-client';
const socket = io.connect('https://tank3d.herokuapp.com:80');
Everything is fine if I run the server locally.

Connecting 2 servers via Node Websockets

I have 2 Raspberry Pi running on the same Network.
I am using one as a local web server for my house, I then have another one connected to some devices. I want them to both be able to communicate to each other via web sockets but am having some problems.
My server looks like this:
express = require('express'); //web server
app = express();
server = require('http').createServer(app);
io = require('socket.io').listen(server); //web socket server
server.listen(8080); //start the webserver on port 8080
app.use(express.static('public')); //tell the server that ./public/ contains the static webpages
io.sockets.on('connection', function (socket) { //gets called whenever a client connects
socket.on('Testing',function(data){
console.log("Testing connection");
});
});
My problem comes with the client connection I am really not sure what code to use to try and connect.
I have installed Express and Socket.io on my client and used this code:
console.log('1');
// Connect to server
var io = require('socket.io')
var socket = io.connect('http://192.168.1.138:8080', {reconnect: true});
console.log('2');
// Add a connect listener
socket.on('connect', function(socket) {
console.log('Connected!');
});
console.log('3');
But this leads to an error on the io.connect is not a function.
I am not really sure how to get the client to work so any advice is appreciated.
I should add that connecting to my webserver directly via the ip and port does load the webpages I have created successfully.
When using socket.io on the server side as a client you need to do var socket = require('socket.io-client')('http://192.168.1.138:8080', ...);. See https://www.npmjs.com/package/socket.io-client

Issues with using Socket.io

I am trying to use socket.io for the first time, Im using it with a MEAN Stack. I have set it up the following way.
Server.js
var express = require('express');
var app = express(); // create our app w/ express
var server = require('http').createServer(app);
var io = require('socket.io')(server);
app.listen(port);
io.on('connection', function(socket) {
socket.emit('message', {
'message': 'hello world'
});
});
HTML File
<script>
var socket = io.connect();
socket.on('message', function(data) {
console.log(data.message);
});
</script>
I couldn't find the socket.io.js file so I searched for a CDN for the script. I used the following.
<script src="https://cdnjs.cloudflare.com/ajax/libs/socket.io/1.4.6/socket.io.js"></script>
I tried downloading the socket.io.js from their website but the website seems to be down. I can access it, I get the 502 BAD GATEWAY error (http://socket.io)
When I load my HTML page after setting everything up as shown above I get the following error in the console.
Any help will be greatly appreciated!
UPDATE:
The socket.io server will perform some magic to provide that file when you retrieve it like this:
<script src="/socket.io/socket.io.js"></script>
EDIT: ah, I see the problem: your Express setup is incorrect.
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var io = require('socket.io')(server);
app.listen(port);
You're attaching socket.io to a server created by http.createServer, but with app.listen() you're actually creating a new server, one that socket.io isn't attached to.
You can make the setup a bit simpler:
var express = require('express');
var app = express();
var server = app.listen(port);
var io = require('socket.io')(server);
No need to use http.
As of now, socket.io website is still down. If you don't have the file in your server, it does not help using /socket.io/ because this points to a route on your server and that route doesn't seem to exist on your app. With this config, you need to be serving the .js file statically from your server - but I don't see that on your server code.
When you try the CDN, make sure the file is being loaded.

Categories