I created an app with NodeJS and I'm using ws module. If I test the app in localhost it works and there isn't any problem to connect websockets. Now I've upload the app to Openshift and when I try to access from the client it returns that is not possible to stablish a connection to the websocket.
If I do a tail in putty to my app I have this message: DEBUG: This type of response MUST NOT have a body. Ignoring data passed to end().
The code that I have in the server is:
#!/bin/env node
//Openshift variables
var ipaddress = process.env.OPENSHIFT_NODEJS_IP || "192.168.69.42";
var port = process.env.OPENSHIFT_NODEJS_PORT || 8080;
//NodeJS require modules
var Enum = require('enum');
var WebSocketServer = require('ws').Server
wss = new WebSocketServer({host:ipaddress, port:port});
var fs = require('fs');
wss.on('connection', function(ws) {
console.log((new Date()) + ' Connection from origin: ' + ws._socket.remoteAddress);
});
console.log((new Date()) + " Server is listening on: " + ipaddress + ':' port);
And in the client:
var ws = new WebSocket("ws://192.168.69.42:8080/");
ws.onopen = function() {
console.log("Connected.");
ws.send("This is the client speaking.");
};
For all WebSocket connections on OpenShift you need to use port 8000 (for Secured sessions it would be 8443). So, your server example works well (I run them after removing the unnecessary line var Enum = require('enum');, you just need to hardcode the port on client to 8000:
var ws = new WebSocket("ws://YourApp-YourName.rhcloud.com:8000");
ws.onopen = function(){
console.log('opened')
}
More information here.
Here is an example on github that works that you can check out: https://github.com/developercorey/openshift-nodejs-http-and-websocket-example
Related
I'm working on small app using node.js and basic html/js web page.
On localhost, I have no problem, webSocket works as well, but when I try on my dedicated server (ovh/kimsufi server), my websocket client return timeout error :
On my server :
ws.js :
var http = require('http'),
WebSocket = require('ws'),
WebSocketServer = WebSocket.Server;
var server = http.createServer();
server.listen('3005', '0.0.0.0', function() {
console.log('Listening on ' + server.address().address + ':' + server.address().port);
var wss = new WebSocketServer({server: server});
wss.on('connection', function(client){
console.log('connected');
client.on('message', function(message) {
client.send('received');
console.log(data);
});
});
});
Result on Linux console :
-----#-----:~/---/---/nodeServer$ node ws.js Listening on 0.0.0.0:3005
On my client :
var p1 = new Promise(function(resolve, reject) {
ws = new WebSocket("ws://------.---:3005");
});
Result in chrome/firefox console :
comm.js:12 WebSocket connection to 'ws://------.---:3005/' failed:
Error in connection establishment: net::ERR_CONNECTION_TIMED_OUT
I think this error apears when I did a new virtualHost, can it block the websocket ?
Here is the current code that I am using to try to connect my web application running socket.io to my node application.
var options = {
key: fs.readFileSync('csgo2x.key').toString(),
cert: fs.readFileSync('csgo2x.crt').toString(),
ca : [fs.readFileSync("csgo2x.pem").toString()],
rejectUnauthorized: false
};
var server = require('https').createServer(options);
var portNo = 443;
server.listen(portNo, function() {
console.log((new Date()) + " Server is listening on port " + portNo);
});
However...when I try to connect my socket.io application running on my Ubuntu server...it gives the error net::ERR_INSECURE_RESPONSE.
I am using certificates from Cloudflare btw and for some reason it's not working
I'm trying to use PeerJS (a webRTC library) for a game and triyng to use the server they provide for doing user discovery. I want to manage a list of connected users and I'm struggling with PeerJS server.
In the doc they say we can have a PeerJs and an Express server in the same app.
Here is the code :
// this doesn't work
var express = require('express');
var app = express();
var ExpressPeerServer = require('peer').ExpressPeerServer;
app.get('/', function(req, res, next) { res.send('Hello world!'); });
var server = app.listen(9000);
var options = {
debug: true,
allow_discovery: true
}
app.use('/api', ExpressPeerServer(server, options));
server.on('connection', function(id) {
// we get a socket object as id :(
// should be a string
console.log(id)
});
server.on('disconnect', function(id) { console.log(id + "deconnected") });
Nevertheless, when a user connects, I get a socket object as id, which is not what I want. Also I can't access to the connected peers at the url http://localhost:9000/peerjs/peers
What is strange is that, using only PeerJS server, it works as expected (I get the string ID of the peer), and I can access to the connected peers at the url http://localhost:9000/peerjs/peers.
// this works
var ip = require('ip');
var PeerServer = require('peer').PeerServer;
var port = 9000;
var server = new PeerServer({port: port, allow_discovery: true});
server.on('connection', function (id) {
// id is correct (a string)
console.log('new connection with id ' + id);
});
server.on('disconnect', function (id) {
console.log('disconnect with id ' + id);
});
console.log('peer server running on ' +
ip.address() + ':' + port);
Any clues to make PeerJS server work with express ?
Is it a regression about the express compatibility ?
Thanks a lot :)
System infos :
node -v : v0.10.25
Ubuntu 14.04
peerJS server installed from github with : npm install peers/peerjs-server (version: "0.2.8")
var app = express();
var server = app.listen(8000);
var q = ExpressPeerServer(server, options);
app.use('/peer', q);
q.on('connection', function (id) {
console.log('user with ', id, 'connected');
});
this should work
Just incase anyone having same issue all you need to do is:
server.on('disconnect', function (client)
{
// this will give you id in text or whatever format you are using
console.log('disconnect with id ' + client.id);
});
You can use the undocumented listAllPeers(function cb(list){}) function if you run your own peerjs-server.
Just a reference to your own answer here: https://github.com/peers/peerjs-server/issues/86
And in combination with SocketIO: http://stephantabor.com/2015/07/11/express-peerjs-and-socket-io/
I have VPS with LAMP. I have free signed SSL certificate from Startssl.com (ssl certificate working correctly)
With http:// protocol I can connect to ws://chat.example.com:1337/some-variable but when I replace protocol http:// to https:// then I can't connect to wss://chat.example.com:1337/some-variable
When I try to connect to wss:// I'm getting error Error in connection establishment: net::ERR_CONNECTION_CLOSED
Why?
// http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/
"use strict";
// Optional. You will see this name in eg. 'ps' or 'top' command
process.title = 'node-chat';
// Port where we'll run the websocket server
var webSocketsServerPort = 1337;
// websocket and http servers
var webSocketServer = require('websocket').server;
var http = require('http');
var $ = require("jquery");
/**
* HTTP server
*/
var server = http.createServer(function(request, response) {
// Not important for us. We're writing WebSocket server, not HTTP server
});
server.listen(webSocketsServerPort, function() {
console.log((new Date()) + " Server is listening on port " + webSocketsServerPort);
});
/**
* WebSocket server
*/
var wsServer = new webSocketServer({
// WebSocket server is tied to a HTTP server. WebSocket request is just
// an enhanced HTTP request. For more info http://tools.ietf.org/html/rfc6455#page-6
httpServer: server
});
EDIT
I have source from http://ahoj.io/nodejs-and-websocket-simple-chat-tutorial
To critique my answer better, as the last one was a mistake and I didn't mean to submit it until after I finished digging, try this:
// Private key and certification
var options = {
key: fs.readFileSync('cert/server.key'),
cert: fs.readFileSync('cert/server.crt')
};
var server = https.createServer(options, function(request, response) {
console.log((new Date()) + ' Received HTTP(S) request for ' + request.url);
}
Trying to get a simple Diet.js server running on OpenShift (free plan) but the server never starts (runs fine locally). The domain itself (http://twilio-levelout.rhcloud.com/) returns a 503 and the logs throw:
events.js:72
throw er; // Unhandled 'error' event
^
Error: listen EACCES
Here's the code:
var server = require('diet'),
app = server();
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
var server_port = process.env.OPENSHIFT_NODEJS_PORT || 3000;
app.get('/', function ($) {
$.end('Hello World!');
});
app.listen("http://" + server_ip_address + ":" + server_port);
The server runs with the default example and a simple Express app but not the above.
Any ideas?
The access error usually means that you are not using the proper port or are trying to bind to interface you have no access to, but when I tried your example, It did not even work on my local machine.
The problem in your example is that app.listen should appear before app.get.
Your example should look like this:
var server = require('diet'),
app = server();
var server_ip_address = process.env.OPENSHIFT_NODEJS_IP || '127.0.0.1';
var server_port = process.env.OPENSHIFT_NODEJS_PORT || 3000;
app.listen('http://' + server_ip_address + ':' + server_port);
app.get('/', function ($) {
$.end('Hello World!');
});