I am trying to create a net socket Nodejs server for my embedded device to send data to on OpenShift.
I am trying to create this simple echo service
var net = require('net');
var HOST = process.env.OPENSHIFT_NODEJS_IP;
var PORT = process.env.OPENSHIFT_NODEJS_PORT || 3000;
console.log('IP:' + HOST + ' Port:' + PORT);
var server = net.createServer(function(connection) {
console.log('client connected');
connection.on('end', function() {console.log('client disconnected');});
connection.write('Hello World!\r\n');
connection.pipe(connection);
});
server.listen(PORT, HOST , function() {
console.log('server is listening');
});
I according to OpenShift's Port Binding Guide I had my client application connect to Port 8000.
For Testing I am using the following code from my desktop machine.
var net = require('net');
var HOST = 'nodejs-myapplication.rhcloud.com';
var PORT = 8000;
var client = net.connect(PORT, HOST, function() {
console.log('connected to server!');
});
client.on('data', function(data) {
console.log(data.toString());
client.end();
});
client.on('end', function() {
console.log('disconnected from server');
});
The Client Scripts gets to Connected to server and gets stuck there itself. Nothing takes place after that.
Now if I open the address nodejs-myapplication.rhcloud.com:8000 in my browser, the NodeJS Server logs a client connected and disconnected, but when the NodeClient is connected the server doesn't show any update. The Node Client just says connected and stays there without doing anything.
If I run the same scripts locally it works fine, ( Locally i use HOst as 127.0.0.1 and port as 3000).
How can I get a TCP Application to connect to the Openshift NodeJS Server?
The Device will be sending ASCII output over the socket eg. $VAR,12,23,21\r\n
I need to be able to read that data, convert it to JSON and send it out to another server.
It has to be loaded on a platform like DigitalOcean with a firewall enabled.
OpenShift doesn't allow custom ports by default so need a workaround for that.
Related
I have a Flightcell DZMX configured to send data to a ip and port via iridium SBD, where i have a server with a simple code running that recive any request and display on the screen:
var net = require('net');
var server = net.createServer(function(socket) {
socket.on('data', function(data) {
console.log(data.toString());
})
});
server.listen(8080, 'my-server-ip-adress');
console.log('Server listening on port 8080');
But no message is recived from the DZMX. Is iridium SBD not in TCP protocol or is my DZMX misconfigured?
It is inded TCP/IP.
You need to configure your equipament IMEI, IP and port in: https://spnetpro.iridium.com
Everything working fine now.
I create app with nodejs socket io. It works clearly at localhost (port: 3000). But when i deploy it to my server in there i can run my app on 3000 port but client side throw timeout. How can i solve it?
var fs = require('fs');
var https = require('https');
var options = {
key: fs.readFileSync('ssl.my-key.key'),
cert: fs.readFileSync('ssl.my-cert.crt')
};
var server = https.createServer(options);
var io = require('socket.io').listen(server);
var port = 3000;
const database = require('./Database');
io.on('connection', (socket) => {
socket.on('message', async (msg) => {
// I do some action here.
});
socket.on('disconnect', (msg) => {
// some action in here too
});
});
server.listen(port, () => {
console.log('listening on *:' + port);
});
It seems like your issue is with port forwarding.
In order for your server to be publicly accessed, it needs to have all ports forwarded appropriately. Locally and on the router.
Check this link to learn more about how to port forward on linux: https://linuxacademy.com/guide/11630-internal-port-forwarding-on-linux-using-the-firewall/
And this to learn more about router port forwarding, but this will really depend on your router.
https://www.noip.com/support/knowledgebase/general-port-forwarding-guide/
However, I don't recommend you to take care of hosting on your own machine(s). I
suggest you use Heroku, you can op in for their free servers, you don't need to pay.
More about heroku and NodeJS: https://linuxacademy.com/guide/11630-internal-port-forwarding-on-linux-using-the-firewall/
let we debug your node js app.
1) add some logs on database connection, http.createserver, also where you have to check if not success then catch exception
2) you should have to open port on centOs before start your node js app
3) you should have test you with domain name or ip address
as per you comment you got connection timeout , you mean node js server trying to connect with port 3000 but node not able to connect and its throws error with connection timeout
also send your sample code of your main index file so we can investigate your problen
thanks.
Okay, I have the following setup: https://i.stack.imgur.com/4T9SX.jpg ( If image below is too small )
The problem is that Computer 2 can not connect with socket.io to computer 1.
Yes i included socket.io in computer 2:
Any ideas as to why Computer 2 cannot connect to computer 1 with socket.io whilst ping can?
Extra information:
- Socket.io version 1.4.5
- Both computers are windows 10
- Computer 2 javascript is in Phonegap
- Computer 2 connects via wi-fi, computer 1 via ethernet
Greetings
EDIT
Code from client (computer 2, init is called upon start):
KerstAppHome.prototype.init = function(){
var address = 'http://192.168.2.120:2017';
console.log("Connecting to: " + address);
this.socket = io.connect(address);
this.socket.on('connect', this.proxy(function(){
console.log("Connected to socket!");
this.socketIsConnected = true;
this.socket.on('disconnect', this.proxy(function(){
console.log("Disconnected from socket!")
this.socketIsConnected = false;
}));
this.socket.on('musicBlob', this.proxy(this.onMusicBlobReceived))
}));
};
KerstAppHome.prototype.onMusicBlobReceived = function(musicBlob){
console.log("RECEIVED SOMETHING");
this.audioCtx.decodeAudioData(musicBlob).then(this.proxy(function(audioBuffer) {
var source = this.audioCtx.createBufferSource();
source.buffer = audioBuffer;
source.connect(this.audioCtx.destination);
source.start();
}));
}
Code from server (computer 1):
var port = 2017;
var io = require('socket.io')(port);
console.log("Listening for socket connections on port " + port);
io.on('connection', function (socket) {
console.log("Connection made!");
socket.on('musicBlob', function(musicBlob){
socket.broadcast.emit('musicBlob', musicBlob);
});
});
Relevant code from browser ( computer 1 ):
var socket = io.connect('http://localhost:2017');
var socketIsConnected = false;
socket.on('connect', function(){
console.log("Connected to server!");
socketIsConnected = true;
});
socket.on('disconnect', function(){
console.log("Disconnected from server!")
$scope.socketIsConnected = false;
});
I want to know why computer 2 can't even connect to the server,
The console.log("Connected to socket!"); is not even called
NOTE: If I execute the javascript of the client (computer 2) on computer 1, it works perfectly, makes connection and receives data!
NOTE: I tested it with computer 1 (server) his firewall turned off and it worked perfectly!
please check you firewall settings, or turn it off for few minutes and than try, also make sure both 2 computer should be connected with same lan/wifi. and
allow phonegap, Evented I/O for v8 JavaScript and Secure Socket Tunneling Protocol through Windows Firewall,
As referencing your code in image - you need to check the following things first
What is the exposed url for your nodejs like e.g.localhost:2017 - if you have set it up as locahost - your node server is running as uri of hostname of localhost and ip 127.0.0.1 which cannot be accessed from another machine - if this is the case you need to assign the actual ip to node process 192.168.2.120 (in your case) only then you can access it from another machine
its good to have a namespace to socket,io connect process
are you using windows machine? you need to open network and sharing center - if you are connected to public network - connections will never work - at least the machine running nodejs server should be connected to home or office network
if you are stll facing the issue you can refer the following code for starting the server
const http = require('http');
const url = '192.168.2.120';
var port = 2017;
console.log("Listening for socket connections on port " + port);
var requestListener = function (req, res) {
res.writeHead(200);
res.end('Hello, World!\n');
}
var server = http.createServer(requestListener);
var io = require('socket.io')(server);
server.listen(port,url);
console.log('Server running at:', url+':'+port);
io.on('connection', function (socket) {
console.log("Connection made!");
socket.on('musicBlob', function(musicBlob){
socket.broadcast.emit('musicBlob', musicBlob);
});
This should work out for you.
The net module has a createServer function that allows you to create a network wrapper. This works fine on a local runtime of Nodejs, but when running in Bluemix it is unable to determine the host address. The server seems to get created, but upon further inspection I find the server.address to be blank.
var tls = require('tls');
var fs = require('fs');
var net = require('net');
var tunnelHost = (process.env.VCAP_APP_HOST || 'localhost');
var tunnelPort = 8888;
var server;
var gatewayOptions = {
host: 'http://cap-sg-prd-5.integration.ibmcloud.com/',
port: '15133',
key: fs.readFileSync('key.pem'),
cert: fs.readFileSync('cert.pem'),
ca: fs.readFileSync('ca.pem')
};
console.log("starting createSecureTunnel");
//create a server end point to use as a network wrapper for the secure gateway
server = net.createServer(function (connListener){
console.log('net server created');
connListener.on('end', function() {
console.log('client disconnected');
});
connListener.on('uncaughtException', function(err){
console.log('exception caught: ' + JSON.stringify(err));
});
//connect to farside, local/private server
connectFarside(connListener, function(err, remoteSocket){
if (err){
console.log(err);
}
console.log('connection made');
remoteSocket.pipe(connListener);
console.log('remote socket connecte to local connListener');
connListener.pipe(remoteSocket);
console.log('local connListener connected to remote socket');
});
});
//setup listener for network wrapper
server.listen(tunnelPort, tunnelHost, function(){
console.log('tunnel created at: ' + tunnelHost +":"+ tunnelPort); //.address +":"+ server.address().port);
});
//createa a TLS connection to the secure gateway
function connectFarside(conn, callback) {
console.log("starting connectFarside");
try {
console.log("initiating farside connection");
var socket = tls.connect(gatewayOptions, function(){
console.log("tunnel connected to " + gatewayOptions.host +":"+ gatewayOptions.port);
callback(null, socket);
});
socket.on("error", function(err){
console.log("Socket error: " + JSON.stringify(err));
});
} catch(err) {
console.log(err);
callback(err);
}
}
Bluemix gives your app a port to run on, this is the reason it is not working in Bluemix. You are starting to start your app on port 8888 with the following line of code.
var tunnelPort = 8888;
It should be changed to
var tunnelPort = process.env.VCAP_APP_PORT || 8888;
The above line will read an environment variable called VCAP_PORT where Bluemix assigns a port to your app, if it is not running Bluemix it will run on port 8888.
Your app will be accessible over the web on port 80 and 443. Bluemix will load balance to your app for you.
You can specify the server address when listening to the server
var net = require('net')
var server = net.createServer(handler)
server.listen(port, address)
Try with address = '0.0.0.0' and see if it works
Partially solved by using the cf-autoconfig module. It helps to reconfigure modules for use on Cloud Foundry platforms. By including this as the first line in my app, it mostly works. It doesn't use the port number. But at least I can access the wrapper.
So I added this as the first line
require("cf-autoconfig");
Then I changed the server.listen to this
//setup listener for network wrapper
server.listen(tunnelPort, function(){
console.log('tunnel created at: ' + tunnelHost +":"+ tunnelPort); //.address +":"+ server.address().port);
});
Now if I use my app name, I can connect to the server created by net.createServer().
I would still like to know how to get the port to work, so this can be used inside of a web application to provide the tunneling.
I'm integrating socket.io into my project. I'm using the code below and it's creating 6 connections after the first request. Is this normal?
server.listen(
port,
function()
{
console.log('Node.js server listening on port ' + port);
}
);
server.on(
'connection',
function(socket)
{
console.log('socket.io connection');
}
);
And here is the console.log output:
Node.js server listening on port 3000
socket.io connection
socket.io connection
socket.io connection
socket.io connection
socket.io connection
socket.io connection
You get this result because (as far as I understand) your server object is an instance of node's http.Server class, and is not connected with Socket.IO at all. In your example, 'connection' event is being fired on any request the your node server. It looks like browser sends 6 requests to your node server: page, favicon.ico, and 4 other requests (it might be images, javascripts, css, etc.).
To integrate socket.io into your project you may use the following code:
var http = require('http');
var sio = require('socket.io');
var server = http.createServer(function(req, res) {
//you request handler here
});
var io = sio(server);
io.on('connection', function(socket) {
console.log('socket connected');
//now you can emit and listen messages
});
var port = 3000;
server.listen(port, function() {
console.log('Node.js server listening on port ' + port);
});
And, of course, the official documentation might be very helpful. Good luck :)