Socket.io-client simple client/server test wont connect - Node.js - javascript

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');

Related

How to convert local server to online server Node.js

I've been developing this game for a school project. It is supposed to be online multiplayer, but this far i have online used it locally. I can figure out how to change my server code for it to be able to act as a "real" online server.
The server code:
// Dependencies
var express = require('express');
var http = require('http');
var path = require('path');
var socketIO = require('socket.io');
var app = express();
var server = http.Server(app);
app.set('port', 5000);
app.use('/static', express.static(__dirname + '/static'));
// Routing
app.get('/', function(request, response)
{
response.sendFile(path.join(__dirname, 'index.html'));
});
// Starts the server.
server.listen(5000, function()
{
console.log('Starting server on port 5000');
});
// Add the WebSocket handlers
io.on('connection', function(socket)
{
console.log('New player arrived');
});
Would appreciate any help greatly.
Most of the shared servers run at port 8080
so you may change the port then upload it into the hosting, If you just need it to be live u can use https://heroku.com/ so u can deploy it there
also here's quick option
npm install -g localtunnel
lt --port 8000
You will receive a URL, for example, https://school.localtunnel.me, that you can share with anyone for as long as your local instance of lt remains active. Any requests will be routed to your local service at the specified port
For more info: https://localtunnel.github.io/www/

Is there a way to restart node server in runtime?

I am trying to define an endpoint in my express server that whenever this end point is called, the server restarts automatically in runtime.
for example, using express my server would look something like this ...
var express = require('express')
var app = express();
app.post('/restart', (req,res)=>{
//restart or create a new instance of the server
// then reply
res.json({
'message': 'server restarted successfully'
})
})
// =======================
// start the server ======
// =======================
var port = process.env.PORT || 8000;
app.listen(port);
console.log('server running at http://localhost:' + port);
NOTE: Although I am using expressJS, I am open to other solutions like HAPI for example.
Thanks in advance
The only way I know of how to restart a node instance is in the CLI level via npm forever or the pm2, but this is for deployment level xP.
You would need npm module forever to be globally installed on your system and Shelljs as a dependency. Initially start your server as forever start {Path to server.js}. Then you can do
var express = require('express')
var shell = require('shelljs')
var app = express();
app.post('/restart', (req,res)=>{
//restart or create a new instance of the server
shell.exec('forever restart {Path to server.js}');
// then reply
res.json({
'message': 'server restarted successfully'
})
})
// =======================
// start the server ======
// =======================
var port = process.env.PORT || 8000;
app.listen(port);
console.log('server running at http://localhost:' + port);
Also see that you will not get a response as the server would have restarted. You would just get a refused to connect.
You can use PM2 to start, stop your server using simple commands.
Starting an application in production mode is as easy as:
pm2 start app.js
Stop all apps
pm2 stop all
Restart all apps
pm2 restart all
I hope this will work for you.
HTH Thanks!
Since express uses the HTTP from Node, you might initialize the Express server by yourself with the Node HTTP functions, noted here.
Once you have the server started, you might close it and restart it as you wish, as mentioned here.
Just you have to be careful with the already opened connections, as calling the HTTP instance for close will leave the already opened connection(s) still open. More information about closing them all; can be found here.

Node js and socket.io not have any error but not work when access by url, how can i do?

Node js and socket.io not have any error but not work when access by url, how can i do ?
My step to install is
cd /home/admin/web/my-doamin-name/public_html
npm init
npm install --save express socket.io
node app.js
...............................................................
After call node app.js, it's will show
start server on port :3000
...........................................................
app.js
var app = require('express')();
var http = require('http').Server(app);
app.get('/', function(req, res){
res.sendfile('index.php');
});
http.listen(3000, function(){
console.log('start server on port :3000');
});
.......................................................
index.html
<html>
<head></head>
<body>HELLO WORLD</body>
</html>
.......................................................
When i test telnet on port 3000 by this command line
telnet my-domain-name.com 3000
It's show like this.
Trying xxx.xx.x.xxx...
telnet: connect to address xxx.xx.x.xxx: Connection refused
.......................................................
And i test port 3000 by this command line
ss -nltp|grep :3000
But it's not show anything.
.......................................................
And then i tried to access to mysite (on google chrome)
https://www.my-domain-name.com:3000
In google chrome it's show error
This site can’t be reached
How can i do ?

Express generator with socket.io configuration

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.

Connecting a terminal based node.js app to a socket.io server?

I am able to connect a web based app to a socket.io server, but not a terminal based app, this is what i use to connect to the socket.io server:
var socket = require('socket.io')
var connection = socket.connect('http://127.0.0.1:8080');
I get the following error:
TypeError: socket.connect is not a function
How can I connect a terminal app to a socket.io server, written in node.js
The socket.io is for creating WebSocket server. You can't use this module as a client. Instead try socket.io-client
var socket = require('socket.io-client')('http://127.0.0.1:8080');
socket.on('connect', function(){});
socket.on('event', function(data){});
socket.on('disconnect', function(){});

Categories

Resources