The server app was running quite fine on Heroku but after migrating to Azure the server would not just start.
Here is the code..
const PORT = process.env.PORT || 2498;
const INDEX = '/index.html';
const server = express()
.use((req, res) => res.sendFile(INDEX, { root: __dirname }))
.listen(PORT, () => console.log(`We\'re live on channel : ${PORT}`));
const wss = new Server({ server });
wss.on('connection', (ws) => {
console.log('Client connected');
ws.on('close', () => console.log('Client disconnected'));
ws.on('message', (message) =>{
// this stays within the server
console.log('[SERVER]: Received a message => %s', message );
})
})```
.........
Clients connected are returning not establish an handshake ....
Azure web app only supports the exposure of port 80 and 443 for website access.
My previous requirements and test results:
I think your problem is a typical problem. I also had related requirements before, and the test failed in the webapp. All documents of Azure app service, talking about websocket, all use SignalR. I also tested the signalr service and it was successful, but this service is free and currently only supports 20 client connections.
What I thought before was to create a console program and put it in webjob to let webjob provide websocket service, but it all failed in the end.
Because azure webapp runs in a sandbox environment, and only open ports 80 and 443, we can't do more configuration. Obviously, the port that your websocket service starts is 2498, so it is not supported.
Suggestion
If you must use websocket, regardless of programming language, you must support custom port opening, such as using Virtual Machine.
You can compare the charges of azure cloud service and virtaul machine. Which one is right for you and which one to use.
Another suggestion is that you can buy a third-party intranet penetration tool and use a fixed IP or URL, such as ngrok, so that you can put the websocket service on your company's internal server. (The test is valid).
Of these three options, intranet penetration may be the most cost-effective choice. You can choose according to your needs.
Related
I am using VM from Google Cloud Platform running Windows Server 2019. I currently have a simple TCP server(node.js) that receives data and then POSTs it to my firestore database using Axios. It is important for me that the server doesn't crash and in the case that it does, it can restart and "stay alive". How can I accomplish this?
New to VM and TCP servers. Just made a simple
TCP
const net = require('net');
const axios = require('axios')
const chalk = require('chalk');
const server = net.createServer(conn => {
console.log(chalk.green('new client'));
conn.on('data', data => {
// let add = conn.address()
console.log(data)
if(data.length > 0){
axios.post('https://abc', {
data: data
})
.then(response => {
conn.write('ACK');
}).catch(err =>{
conn.write('ERR', err)
console.log(err)
})
}
});
conn.on('end', () => {
console.log(chalk.red('client left '));
console.log(chalk.white.bold('----------------------------------'));
});
});
server.listen(8080);
Running a node.js application on Windows Server has several challenges.
Installation and setup of a monitoring application that detects that the node.js application has failed and restarts the application. An example product that provides this ability is node-windows:
https://www.npmjs.com/package/node-windows
This is an example and not a recommendation for any specific product.
The monitoring application must be installed and set up as a Windows service so that the monitoring application restarts automatically. The application node-windows also provides this capability.
Install the application files outside of your user environment. This means creating a directory with the correct ownership and rights so that the Windows service can access the files.
This article written by Charan Tej might help to understand the details:
Node Windows Service for Running Node.js Application
Our website has been running on an internal test machine where it could be accessed by all computers inside the network.
Now we want to deploy this website on a webserver (Apache2) to make it available to our clients. We want to use https and this is where we encountered a problem.
The Socket.io client canĀ“t connect to the node.js server since switching to https. We are using a signed certificate from a trusted CA. I have tried every solution I could find but none seem to work for our case.
Constructor used with ngx-socket-io in angular:
constructor() {
super({url: 'https://mywebPage.com:8080',options:{secure: true, origin: '*', transport:['websocket']}})
}
Our certificate seems to be valid since it works for our angular page. We are also able to make HTTPS GET/POST requests to our API which is located on the same server.
node.js socket.io server code:
var options = {
key: fs.readFileSync('/etc/apache2/folder/certificate.com.key'),
cert: fs.readFileSync('/etc/apache2/folder/certificate.com.public.crt'),
ca: fs.readFileSync('/etc/apache2/folder/certificate-intermediate.crt'),
requestCert: true
};
let server = require('https').createServer(options);
let io = require('socket.io')(server);
server.listen(8080);
console.log("Server started on Port 8080");
the client tries to connect to the socket-Server but fails and gets the net::ERR_CONNECTION_CLOSED the rest of the web page loads fine and has a valid certificate
We have also tested to see if the port on the web server is accesible and it seems to be open in netstat and nma.
If any more information is needed I am glad to provide.
EDIT: have tested the setup with rejectUnauthorized:false on the client side but that does not change the error.
similar stack overflow questions which i have considered in solving the problem:
socket.io net::ERR_CONNECTION_CLOSED
Setup Server-Server SSL communication using socket.io in node.js
EDIT 2: added requestCert: false, rejectUnauthorized: false into my node.js options.
Now the previous Error has been resolved now:
error during WebSocket handshake: Unexpected response code: 400
I've homework for Client Server Programming course and it want me to create Multi-Client Web Servers. But the rule is web server must implement HTTP version 1.0 protocol, where separated HTTP requests are sent for each component of the Web Page.
Unfortunately, I'm just know to work with Node.js. I know C, but it long times (around 10 years ago) and only do very basic programming such as arithmetic operation, work with string and arrays, not OOP in C.
So the question, how to create web server with HTTP/1.0 protocol in Node.js?
Currently, I have node v.10.15.1 installed on my laptop (using macos). I've tried with http and net module but can't find how to configure the protocol to use HTTP/1.0
We need to require the http module first and bind our server to the port which we listen on.
Inside the index.js :
// content of index.js
const http = require('http')
const port = 3000
const requestHandler = (request, response) => {
console.log(request.url)
response.end('Hello Its Your Node.js Server!')
}
const server = http.createServer(requestHandler)
server.listen(port, (err) => {
if (err) {
return console.log('something bad happened', err)
}
console.log(`server is listening on ${port}`)
})
So lets start it with:
$ node index.js
I have a Nodejs client app, and a Nodejs server app. I use Apollo GraphQL for network.
These communicate fine if run on the same pc. They also work fine if the client is on another pc and server on my pc, over LAN only. They fail to communicate via internet.
My code that works right now:
server:
server.listen(PORT, '0.0.0.0',() ...
client:
const wsLink = new WebSocketLink({
uri: 'ws://192.168.10.41:8081/subscriptions',
Firs of all you must be sure that both nodes have a public IP, otherwise if you are behind a symmetric NAT you will not be able to do that.
After that you can use some of the modules of node, for example UDP(user datagram protocol) to try the connection.
Good Luck
I am trying really hard to make my MQTT client work inside my application. The broker is provided by CloudMQTT.
When trying to run the following code in NodeJS, the client connects properly to MQTT;
var mqtt = require('mqtt');
var client = mqtt.connect(
"mqtt://m20.cloudmqtt.com",
{
port: 11212,
username: "XXXXXXXX",
password: "XXXXXXXX"
}
);
client.on('connect', function () {
console.log('connected');
});
However, when I run the same code (without the require of course) in the frontend using the Bower package, the client does not connect. I have also tried other MQTT browserfied JS packages. I prefer MQTT.js and not Paho, because I would like to use multiple subscribes with one connected client.
If console.log(client);, NodeJS uses protocol: 'mqtt' and protocolId: 'MQTT'. The frontend uses protocol: 'ws' and protocolId: 'MQTT'. Could this be the problem? Adding these options to mqtt.connect has no effect.
From with in the browser the only option is going to be to connect via MQTT over Websockets. CloudMQTT use different port numbers for native MQTT and MQTT over websockets so you will need to also change the port number for the browser based code to the Websocket port listed in your CloudMQTT dashboard.