Below code is my current configuration, it worked but I'm confused.
server side
var server = require("http").Server(express);
var io = require("socket.io")(server);
server.listen(5000);
io.on('connection', function(client) {
});
cilent
var socket = io.connect('http://localhost:5000');
Why we need to create another server for socket at port 5000 for an application? can't socket use 3000? which is the express's running port. I removed the line of server.listen('5000') and do server.listen() and try connect to port 3000 at client side it doesn't work.
By default express 4 create a server and run it bin/www. I solved this issue by commenting out server.listen(port); in that file.
Related
I am implementing a simple game in Node.js. I have a client.js for my client side code, and a server.js running on a remote server, which both use sockets to communicate on port 3000
I am also running Apache on port 80, and using ProxyPass in my apache configuration file, to route the url mywebsite.io/agario to my nodejs server.
<Location /agario>
ProxyPass http://localhost:3000/
ProxyPassReverse http://localhost:3000/
</Location>
I am also using cloudflare to route my webserver 167.179.xx.xx through the url https://agario.mywebsite.io for SSL so that I can use HTTPS.
The problem
When I try to connect to my website https://agario.mywebsite.io/agario I am receiving the following error:
socket.io-1.4.5.js:1 GET https://localhost:3000/socket.io/?EIO=3&transport=polling&t=MakAMgZ net::ERR_CONNECTION_REFUSED
I am unclear why my client code is trying to connect to localhost, when I have specified in the code to connect to the remote server. Potentially I am just confused on how to run the node.js server as this is my first taste of Node.js and sockets.
client.js
...
var socket;
socket = io.connect('https://agario.mywebsite.io/agario');
...
server.js
var app = express();
var server = app.listen(3000, listen);
// This call back just tells us that the server has started
function listen() {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://' + host + ':' + port);
}
app.use(express.static('public'));
var io = require('socket.io')(server);
io.sockets.on('connection',
function(socket) {
console.log("We have a new client: " + socket.id);
...
});
If I have missed out any vital information please let me know and I will update my question, thank you.
You server is listening on port 3000 and you're trying to connect with it via 443, you should try something like this
socket.connect('https://ip:3000');
However, if you're sure that ur client is using the same port as the server or u have a port forwarding then try to use netcat just to make sure the the problem is with your script not the network config :
nc -zv -w1 ip port
I have set up a node.js server that is supposed to serve HTML files from a directory to clients. It should also log any connecting and disconnecting clients. Both work perfectly, but only locally. I have already forwarded traffic on port 3000 to my server and deactivated its firewall. What else can I do to enable external access?
var express = require('express');
var socket = require('socket.io');
//app setup
var app = express();
var server = app.listen(3000, ready);
function ready(){
console.log('setup completed\nlistening on port 3000\n\n');
}
app.use(express.static('public'));
//socket setup
var io = socket(server);
io.on('connection', newConnection);
function newConnection(socket){
//some code
}
i have already forwarded traffic on port 3000 to my server
Thats the wrong way round. Your server listens on port 3000, but webservers offer there service under Port 80, so you have to forward Port 80 to Port 3000.
and deactivated its firewall.
Bad idea. Just open one port (80 or 3000 depending on where you do port forwarding). And usually Routers on the way will also block certain ingoing connections, so check that too.
I have tried socket.io on localhost and it works perfectly. However, when in production I get the following error:
GET http://77.235.46.164:3000/socket.io/?EIO=3&transport=polling&t=1448754369321-21 net::ERR_CONNECTION_TIMED_OUT
On the server side I have the following code:
var express = require('express');
var mysql = require('mysql');
app = express(),
server = require('http').createServer(app),
io = require('socket.io').listen(server);
connection.connect();
server.listen(process.env.PORT || 3000);
While on the client side:
var socket = io.connect('http://77.235.46.164:3000'); .... etcs
I am also starting the node server (I have VPS account in EuroVPS).
I have searched everywhere but I don't seem to find a solution that works for me.
Is it working without firewall?
In some controlled network Firewall may block websockets & ports other than 80,443.
You can create an HTTP server yourself,instead of having the Express framework create one for you, so you will be able to reuse your HTTP server, in order to keep the same server instance.
You can try :
var express = require('express');
var app = express();
//Create your server
var server = app.listen(3000);
var io = require('socket.io')(server);
//Then you can listen for connection
io.on('connection', function(socket){
socket.emit('an event sent to all connected clients')
//Awesome things
})
I am trying to get some simple communication to work in Node.js using socket.io and socket.io-client.
I have two scripts, server.js and client.js.
The server.js script can bind to a webfacing port and act like a normal server/socket.io host, which all works when used with the browser client,
but the client.js (running in another node script) doesn't work, it just waits not outputting anything. I expect my socket.io-client in client.js to connect to the socket.io instance in server.js and for both to display a message in their console to say they're connected.
server.js code:
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.get('/', function(req, res){
console.log("\nNew server request: "+req.url+"\n");
// sends a html file with a script that connects
// to socket.io running in server.js
res.sendFile(__dirname+'/webroot/index.html');
});
// listens for connections
io.on('connect', function(socket){
console.log('a client connected');
});
// start listening on server
http.listen(9000, function(){
console.log('listening: 9000');
});
client.js code:
var io = require('socket.io-client');
// connect to server.js socket
var socket = io('http://hostsite.com', {
port: 9000
});
socket.on('connect', function(){
console.log("connected\n");
});
installed with npm
npm install socket.io // version: 1.0.6
npm install socket.io-client // version: 1.0.6
npm install express // version: 4.8.5
Ideally I don't want to be using express or an http server, just a socket communication between two Node.js scripts, one on a server machine, one on a client machine.
Thanks for any help :)
For some reason, it wouldn't connect when i used:
npm install socket.io-client
and
require('socket.io-client');
But it does connect if I use the client that comes with the main package
npm install socket.io
and
require('socket.io/node_modules/socket.io-client');
Took a while to figure out, but hopefully now it will take someone else less time; if they face the same problem.
have you tried:
var socket = io.connect('http://hostsite.com:9000');
Currently my application based on Expressjs + angularjs. I want to start few 2 way calls along with existing http calls. I went through few websocket chat tutorials but nonew of them integrated with expressjs.
Do I start websocket connection on new port? How do I integrate my angularjs with websocket?
Can I just create few more routes and controller functions and have some of them work 2 way?
Nothing special is needed, you can use the same port for Socket.IO and express.
e.g. in my project I do something like this:
var express = require('express');
var io = require('socket.io');
var app = express();
var server = http.createServer(app).listen(SERVER_PORT, function() {
console.log('Express server listening on port ' + SERVER_PORT);
});
// let socket.IO listen on the server
io = io.listen(server);
io.on('connection', function(socket) { /* handle connection */ });
AFAIK there is also an example with express on the Socket.IO wiki.