Openshift 3 Socket io - node.js

I have uploaded a site in openshift, everything seems to be working except socket io.
I can see the network tab that polling is taking place but no action is happening.
Here is my server side socket code
port= process.env.OPENSHIFT_NODEJS_PORT || 8080
io = require('socket.io').listen(applisten);
io.configure(function(){
io.set("transports", ["xhr-polling"]);
});
and below is my client side code to connectto socket.io
socket = io.connect("http://something:8000");
Thanks for any help :)

I have the same issue after socket.io changed to version 1.0.
Here's the changes that will make it work again:
io = require('socket.io').listen(applisten);
io.set('transports', [ 'polling', 'websocket' ]);
On client side:
socket = io();
Correct me if I'm wrong, another side effect from the changes I realised from the changes was that this function does not work:
socket.on('connect', function(data){
});
I change the keyword 'connect' to another something else and it works.
Hope that helps.

Related

Websocket request sometimes doesn't work after connection establishment

I have a Node.js script which is supposed to regularly access a SailsJS application via a socket connection. Client and server run on physically different machines on different networks. The SailsJS application is proxied behind nginx. That works in general. However, at random times, the connection is established but the first post request within the websocket connection never reaches its destination.
The code looks basically like this:
var socketIOClient = require('socket.io-client');
var sailsIOClient = require('sails.io.js');
var io = sailsIOClient(socketIOClient);
io.sails.url = 'https://foo.foo:443';
io.sails.rejectUnauthorized = false;
io.socket.on('connect', function() {
console.log("Connected!")
io.socket.post('/someroute', { someOptions: "foo" } ,
function(data) {
console.log(data);
});
});
io.socket.on('disconnect', function(){
console.log("Disconnected!");
});
io.socket.on('connect_error',function () {
console.log("connect_error!");
});
In case of a failure, simply nothing happens after console.log("Connected!"). Nothing appears in nginx's logs (in contrast to successful cases), the callback of io.socket.post never gets executed.
The most important question for me is: At which side is the problem? Client or server?
How can I debug this and narrow down the problem? Could it be a networking issue? Or something wrong the configuration, implementation or with the script itself?

socket io connection working fine in localhost but not when deploying on heroku server

When I am running my code on localhost it is working fine but after deploying on heroku it shows error (GET Error)
Here Is My Code
Server Side Code
app = express()
app.listen(app.get('port'), function() {
console.log('Node app is running on port..', app.get('port'));
});
var server = app.listen(4200);
var io = require('socket.io')(server);
io.on('connect',(socket)=>{
console.log('connected..........');
})
Client Side Code
private socket = io('My-Heroku-server-address:4200');
Error
https:My-Heroku-server-address:4200/socket.io/?EIO=3&transport=polling&t=MDpJszb
Instead of specifying the URL to connect in the client side code like private socket = io('My-Heroku-server-address:4200'); just do
var socket = io();
This will try to connect to the host that serves the page.
Refer Socket.IO for more information.
Thanks all.
Problem has been solved.
I just removed the port no.
private socket = io('My-Heroku-server-address');
Thanks all

Socket.io : How to exclude Flashsocket

I'm using Socket.io 0.9.16 to send notifications to my users.
Everything is working fine, expect for Firefox (version 47). This browser is obviously using the Flashsocket callback and display a nasty warning :
"Firefox has prevented the outdated plugin "Adobe Flash" from running..."
1) Why the last version of Firefox is using Flashsocket as a callback, and not websocket ?
2) I don't want to use Flash at all. I tried to disable Flashsocket by setting which transports I want to use:
Client JS :
var socket = io.connect('myIP:XXXX',
{transports : ["websocket", "xhr-polling", "htmlfile", "jsonp-polling"]});
Server JS :
var io = require('/Path/TO/socket.io').listen(XXXX);
io.configure(function () {
io.set("transports", ["websocket", "xhr-polling", "htmlfile", "jsonp-polling"]);
io.set("polling duration", 10);
});
io.sockets.on('connection', function (socket) {
etc...
But what I did above is not working. Firefox continues to use Flash and shows the warning (as Chrome is using websocket all right)
Do you have any idea what I'm doing wrong ?
PS : I know socket.io v1.x is not using Flash as a callback anymore so it would be solution to upgrade, but I can't just figure out how to adapt my script to make it work with the v1.x version, so I'd like to stay with v0.9.16 if it possible.
Thank you for your help!
I had same problem, but updating version of socket.io and node solved the problem. Sample code given below:
Client JS
socket = io.connect('ip:port', {transports : ["websocket", "xhr-polling", "htmlfile", "jsonp-polling"]});
Server JS
var http = require('http');
//creating server
var server = new http.createServer();
//setting server listening port and domain
server.listen(PORT, DOMAIN);
var io = require('socket.io').listen(server, {transports: ["websocket", "xhr-polling", "htmlfile", "jsonp-polling"]});

Nodejs Error: This socket has been ended by the other party

I have setup a simple server and client, however whenever I close the client, it seems not possible to reconnect. Here's my client:
const net = require('net');
var client = net.connect({port: 8080, host: '127.0.0.1'});
var response = '';
// events
client.on('data', function(chunk) { response += chunk });
client.on('end', function() {
console.log(response);
client.end()
});
// main execution
client.write('test');
And here's my server:
const net = require('net');
var server = net.createServer();
server.listen(8080, '127.0.0.1');
server.on('connection', function(sock) {
sock.on('data', function(chunk) {
sock.write('test received');
sock.end();
});
});
This is just outline code representing my issue. When I execute my client the first time, everything works correctly. However, when I execute it again, the server outputs the error mentioned in the title and crashes. The same happens if I remove 'client.end()' and instead Ctrl+C out of the client program to cause it to end.
My understanding of sockets is that they represent endpoints in a stream between the client and the server. When that stream is no longer necessary (i.e, when the client does what it needs to do), I want that stream to be completely removed. I would think that calling end() on both the client and server endpoints of that single stream would achieve this, like sending two FIN messages, but as explained it does not. The reasons I want to do this are so: (a) the client file will actually finish execution and (b) the server will no longer have its socket endpoint of that stream in its system/waste resources listening to it.
Any insight into the source of my problem would be appreciated.
You should use the 'connect' event on the client-side to be sure that you perform requests only when your socket is ready. So, in the callback on the event, you can invoke write() function.
You can check if socket is not destroyed before writing.
if (!socket.destroyed) socket.write("something");
Your server only closes the socket when data is received, and your client never sends any.

Change port without losing data

I'm building a settings manager for my http server. I want to be able to change settings without having to kill the whole process. One of the settings I would like to be able to change is change the port number, and I've come up with a variety of solutions:
Kill the process and restart it
Call server.close() and then do the first approach
Call server.close() and initialize a new server in the same process
The problem is, I'm not sure what the repercussions of each approach is. I know that the first will work, but I'd really like to accomplish these things:
Respond to existing requests without accepting new ones
Maintain data in memory on the new server
Lose as little uptime as possible
Is there any way to get everything I want? The API for server.close() gives me hope:
server.close(): Stops the server from accepting new connections.
My server will only be accessible by clients I create and by a very limited number of clients connecting through a browser, so I will be able to notify them of a port change. I understand that changing ports is generally a bad idea, but I want to allow for the edge-case where it is convenient or possibly necessary.
P.S. I'm using connect if that changes anything.
P.P.S. Relatively unrelated, but what would change if I were to use UNIX server sockets or change the host name? This might be a more relevant use-case.
P.P.P.S. This code illustrates the problem of using server.close(). None of the previous servers are killed, but more are created with access to the same resources...
var http = require("http");
var server = false,
curPort = 8888;
function OnRequest(req,res){
res.end("You are on port " + curPort);
CreateServer(curPort + 1);
}
function CreateServer(port){
if(server){
server.close();
server = false;
}
curPort = port;
server = http.createServer(OnRequest);
server.listen(curPort);
}
CreateServer(curPort);
Resources:
http://nodejs.org/docs/v0.4.4/api/http.html#server.close
I tested the close() function. It seems to do absolute nothing. The server still accepts connections on his port. restarting the process was the only way for me.
I used the following code:
var http = require("http");
var server = false;
function OnRequest(req,res){
res.end("server now listens on port "+8889);
CreateServer(8889);
}
function CreateServer(port){
if(server){
server.close();
server = false;
}
server = http.createServer(OnRequest);
server.listen(port);
}
CreateServer(8888);
I was about to file an issue on the node github page when I decided to test my code thoroughly to see if it really is a bug (I hate filing bug reports when it's user error). I realized that the problem only manifests itself in the browser, because apparently browsers do some weird kind of HTTP request keep alive thing where it can still access dead ports because there's still a connection with the server.
What I've learned is this:
Browser caches keep ports alive unless the process on the server is killed
Utilities that do not keep caches by default (curl, wget, etc) work as expected
HTTP requests in node also don't keep the same type of cache that browsers do
For example, I used this code to prove that node http clients don't have access to old ports:
Client-side code:
var http = require('http'),
client,
request;
function createClient (port) {
client = http.createClient(port, 'localhost');
request = client.request('GET', '/create');
request.end();
request.on('response', function (response) {
response.on('end', function () {
console.log("Request ended on port " + port);
setTimeout(function () {
createClient(port);
}, 5000);
});
});
}
createClient(8888);
And server-side code:
var http = require("http");
var server,
curPort = 8888;
function CreateServer(port){
if(server){
server.close();
server = undefined;
}
curPort = port;
server = http.createServer(function (req, res) {
res.end("You are on port " + curPort);
if (req.url === "/create") {
CreateServer(curPort);
}
});
server.listen(curPort);
console.log("Server listening on port " + curPort);
}
CreateServer(curPort);
Thanks everyone for the responses.
What about using cluster?
http://learnboost.github.com/cluster/docs/reload.html
It looks interesting!

Resources