I just created node js app using socket.io
For me in local it works fine, but when I uploaded it to bluemix, it gives me errors
Here is files that are working fine on localhost
app.js file
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.engine('html', require('swig').renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.get('/', function(req, res){
res.render('index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function() {
console.log( 'A user disconnected' );
});
socket.on('chat message', function(msg) {
console.log('Chat message ' + msg);
var response = {
'msg': msg,
'address': socket.request.connection.remoteAddress
};
io.emit('chat message', JSON.stringify( response ));
});
});
http.listen(1234, '0.0.0.0', function(){
console.log('listening on *:1234');
});
and index.html file
<!doctype html>
<html>
<head>
<title>Socket.IO chat</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font: 13px Helvetica, Arial; }
form { background: #000; padding: 3px; position: fixed; bottom: 0; width: 100%; }
form input { border: 0; padding: 10px; width: 90%; margin-right: .5%; }
form button { width: 9%; background: rgb(130, 224, 255); border: none; padding: 10px; }
#messages { list-style-type: none; margin: 0; padding: 0; }
#messages li { padding: 5px 10px; }
#messages li:nth-child(odd) { background: #eee; }
</style>
</head>
<body>
<ul id="messages"></ul>
<form action="">
<input id="m" autocomplete="off" /><button>Send</button>
</form>
<script src="/socket.io/socket.io.js"></script>
<script src="https://cdn.socket.io/socket.io-1.2.0.js"></script>
<script src="http://code.jquery.com/jquery-1.11.1.js"></script>
<script>
var socket = io();
$('form').submit( function() {
socket.emit('chat message', $('#m').val());
$('#m').val('');
return false;
});
socket.on('chat message', function( info ) {
var response = JSON.parse( info );
$('#messages').append($('<li>').text( response.address + ': ' + response.msg ) );
});
</script>
</body>
</html>
And here are files on server
app.js file
/*eslint-env node*/
//------------------------------------------------------------------
// node.js starter application for Bluemix
//------------------------------------------------------------------
// This application uses express as its web server
// for more info, see: http://expressjs.com
var express = require('express');
// cfenv provides access to your Cloud Foundry environment
// for more info, see: https://www.npmjs.com/package/cfenv
var cfenv = require('cfenv');
// create a new express server
var app = express();
// serve the files out of ./public as our main files
app.use(express.static(__dirname + '/public'));
// get the app environment from Cloud Foundry
var appEnv = cfenv.getAppEnv();
// start server on the specified port and binding host
app.listen(appEnv.port, function() {
// print a message when the server starts listening
console.log("server starting on " + appEnv.url);
});
var http = require('http').Server(app);
var io = require('socket.io')(http);
app.engine('html', require('swig').renderFile);
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.get('/', function(req, res){
res.render('index.html');
});
io.on('connection', function(socket){
console.log('a user connected');
socket.on('disconnect', function() {
console.log( 'A user disconnected' );
});
socket.on('chat message', function(msg) {
console.log('Chat message ' + msg);
var response = {
'msg': msg,
'address': socket.request.connection.remoteAddress
};
io.emit('chat message', JSON.stringify( response ));
});
});
index.html file is same
and I get this error on my google chrome console
GET http://testingchat.mybluemix.net/socket.io/?EIO=3&transport=polling&t=1440575370819-112 404 (Not Found)
I used codes from this tutorial
Who can help me ? Thank you
according to your code you set socket.io listening on 1234 port. On Bluemix runtime only ports 80, 443 and 9080 will be enabled for routing. Moreover runtimes engine will assign your local port dinamically and you have to read it from vcap services before setting socket.io using it. Check on Bluemix documentation for how to do it.
About the 404 error, it is due to the request done to /socket.io/ path