I have difficulties to work with socket.io, I thought my project used modules which could enter in confilct with socket.io, so I started a new KrakenJs project, and installed just socket.io.
As the examples found in the official socket.io website, I tried this :
//index.js
'use strict';
var kraken = require('kraken-js'),
app = require('express')(),
server = require('http').Server(app),
io = require('socket.io')(server),
options = {
onconfig: function (config, next) {
//any config setup/overrides here
next(null, config);
}
},
port = process.env.PORT || 8000;
app.use(kraken(options));
io.on('connection', function(socket){
console.log('a user connected');
});
app.listen(port, function (err) {
console.log('[%s] Listening on http://localhost:%d', app.settings.env, port);
});
My route controller :
'use strict';
module.exports = function (router) {
router.get('/', function (req, res) {
res.sendfile('./public/templates/index.html');
});
};
And the html page (the same as the example in the website) :
<!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>
var socket = io();
</script>
</body>
</html>
With that code, I have the same error than in my other kraken app... So I decided to test a simple node app without kraken framework, and works fine.
Here's the error which appears with krakenJs :
Where does it come from ? I also test with and without requireJs, but doesn't affect...
EDIT : I saw the error is visible when I enter the client-side script before calling require.js :
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
<script data-main="/js/app" src="/components/requirejs/require.js"></script>
And if I put script after :
<script data-main="/js/app" src="/components/requirejs/require.js"></script>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
</script>
...there's no errors, but the script is not used (no "connected" message in the console).
Any idea ?
I finally found what was the problem.
In fact, socket.io has to initialize its program on the server to use inside it. But when you use :
You are using ther server defined by the
server
variable. So you have to use thelisten()
method on this variable.But, by using this variable, I don't think Kraken will be well used in the application. Unfortunately, I didn't find any possibilities to use directly the
app
variable forlisten()
method.