socket.io works on heroku but not on azure - azure

I'm using git to deploy my application not FTP. Also I've set at client side:
var socket = io({transports:['websocket']});
Also in server
var port = process.env.PORT || 3000;
...
io.on('connection', function(socket) { io.set('transports', ['websocket']);
console.log('new connection on socket.io');
socket.on('move', function(msg) {
socket.broadcast.emit('move', msg);
});
});
Websocket and 'Always On' is set on at azure and web.config does have:
But still emit to all sockets fails. I made simpler test version of my application. Deployed it again to heroku and had no problems.
Exact deployed application code can be seen here:
https://github.com/jmietola/testexpress

Please disable perMessageDeflate header when using socket.io ..
var io = require('socket.io')(server,{
perMessageDeflate :false
});

Related

How to run NodeJs SocketIO on server (Centos 7)

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.

WebSocket connection to 'ws://chattestarpit.herokuapp.com/' failed: Error during WebSocket handshake: Unexpected response code: 503

I am new to WebSocket (webRTC).I created a video chat app. it's working on localhost.
Server
app.listen(process.env.PORT || 8081, function () {
console.log("Example app listening at 8081" )
});
app.get('/', function(req, res){
res.sendfile('index.html');
});
//require our websocket library
var WebSocketServer = require('ws').Server;
//creating a websocket server at port 9090
var wss = new WebSocketServer({server: app});
//all connected to the server users
var users = {};
Client
//connecting to our signaling server
var HOST = location.origin.replace(/^http/, 'ws');
var conn = new WebSocket(HOST);
conn.onopen = function () {
console.log("Connected to the signaling server");
};
Heroku showing some problem with WebSocket. Thanks in advance.
Actually if you see "heroku logs", then you can find that the port on which app is running is a random 5 digit number. ( like 28896, 53365, etc.) It never actually runs on the second half || 8081.
You need to debug the actual root cause, what you can do is
Open terminal at your app folder
Type command heroku logs -t --app <your-app-name>
It will give all logs, including your nodejs based consoles
Based on that apply fixes accordingly
Hope that helps!
For me is was got the port from process.env.PORT and then it starts to work.
Heroku ignore completely my '.dev`s file

Why is my connection of a NodeJS server (Sails) to another NodeJS Server (Express4) using Socket.IO failing?

I am trying to connect my Node.JS (written using Sails.JS) app to another Node.JS server (Express4 / Socket.io) using socket.io-client.
My Sails Service app/services/Watcher.js looks like
var client = require('../../node_modules/sails/node_modules/socket.io/node_modules/socket.io-client');
// callback of the form function(socket)
exports.connect = function(callback) {
sails.log.debug("will connect socket to", sails.config.watcher.uri, "with Socket.io-client version", client.version);
var socket = client.connect(sails.config.watcher.uri);
socket.on('connect', function(){
sails.log.debug("connected");
socket.on('disconnect', function(){
sails.log.debug("Disconnected");
});
socket.on('error', function(err){
sails.log.debug("Could not connect", err);
});
callback(socket);
});
};
This is invoked from config/bootstrap.js as follows:
Watcher.connect(function(socket){
sails.log.debug("Connected watcher to relay with socket", socket);
});
On the Express side my server relay.js is as simple as:
var app = require('express')(),
http = require('http').Server(app),
io = require('socket.io').listen(http),
port = process.env.RELAY_PORT || 8000;
app.get('/', function(req, res) {
var response = {message: "some response"}; // to be implemented.
res.json(response);
});
http.listen(port, function () {
console.log("Relay listening on port " + port);
});
io.sockets.on('connection', function (socket) {
console.log("Connection opened", socket);
socket.on('disconnect', function () {
console.log("Socket disconnected");
});
});
When I run node relay it dutifully reports
Relay listening on port 8000
When I sails lift my other server it dutifully reports
will connect socket to http://localhost:8000 with Socket.io-client version 0.9.16
But I never see an actual connection.
If I point a browser at localhost:8000 I get the {"message":"some response"} JSON response I expect.
Why isn't my relay server accepting a connection from my socker.io-client app?
The issue here is probably that you're trying to re-use the
socket.io-client from inside of Sails. In general, if you're require()-ing dependencies of Sails directly in your project, you're heading in the wrong direction. In this case, socket.io-client caches configurations and connections, so your require isn't getting a fresh copy.
Instead, do
npm install socket.io-client#~0.9.16 --save
in your project and require with
var client = require('socket.io-client');
that'll give you a fresh copy of the socket client to work with, and avoid any conflicts with the Sails core's version.

Deploying a website to windows azure with web sockets

I have a node.js project that works fine on my local machine running a node server. However when I deploy it to azure I can not connect to the websocket server. I heard somewhere that you may need to edit the web.config file to turn on web sockets, but I cant find that anywhere.
The server sets up a websocket like follows:
var http = require('http'),
port = process.env.port || 1337,
NodeSimpleRouter = require('node-simple-router'),
router = new NodeSimpleRouter(),
WebSocketServer = require('ws').Server,
wss = new WebSocketServer({port: 8080});
//create the server
http.createServer(router).listen(port);
console.log('Web server running on port ' + port);
and the client like this:
var socket = new WebSocket('ws://localhost:8080');
do I need to change any of these settings, such as the value of 'localhost'?
Azure can be a pain sometimes with websockets and node.js . Here is what I got to work using socket.io . I have my code deployed out as a cloud service. Be careful if you do VIP swaps from staging to prod because I have noticed it doesn't play nice with websockets.
app.set('port', process.env.PORT || 3000);
var server = http.createServer(app);
var io = require('socket.io').listen(server);
server.listen(app.get('port'));
//Chat room
io.sockets.on('connection', function (socket) {
socket.on('send', function (data) {
io.sockets.emit('message', data);
});
});
You can also override the websockets and force default of long-polling by using this:
//Over ride the Azure defaults
io.configure(function () {
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});

Can't connect to nodejs application on heroku

iam trying to connect php application with another nodejs application on heroku using socket.io
code using for connection on client (php app)
<script src="http://mynodeapp.herokuapp.com/socket.io/socket.io.js/"></script>
<script language="javascript" type="text/javascript">
var reguser_socket = io.connect('http://mynodeapp.herokuapp.com');
console.log("connected");
<script>
code using on server (nodejs app)
var http = require('http');
var port = process.env.PORT || 4000;
var app = http.createServer(handler).listen(port);
var io = require('socket.io').listen(app);
io.configure(function(){
io.set('log level', 1);
io.set("transports", ["xhr-polling"]);
io.set("polling duration", 10);
});
io.sockets.on('connection', function (socket) {
console.log("client has connection established ");
});
But while runnig this i get
GET http://mynodeapp.herokuapp.com/socket.io/1/?t=1384779309815
(Internal Server Error)
In order to use Web Sockets on Heroku, you need to run:
heroku labs:enable websockets
because it is only in public beta.
See here for a general overview on how to use it with Heroku:
https://devcenter.heroku.com/articles/node-websockets
This section of code works fine my connection was interrupted because i have another part of code cause this problem

Resources