iisnode
iisnode copied to clipboard
Using socket.io 2.0.3 with iisnode
Hi, I'm trying use socket.io version 2.0.3 with iisnode latest version,and node.js version 6.11.2, but there are some problems with that and I cannot resolve it. Dose the iisnode can work with socket.io version 2.0.3? I try to migrate from socket.io 0.9 (work well) to socket.io 2.0.3 (does not work). Can anyone help me. The error is:
Uncaught SyntaxError: Unexpected token < interface:21 Uncaught ReferenceError: io is not defined
server.js
var app = require('http').createServer(handler);
var io = require('socket.io')(app);
io.set('resource', '/project/socket.io');
......
web.config
<rewrite>
<rules>
<rule name="interface">
<match url="interface"/>
<action type="Rewrite" url = "interface.html"/>
</rule>
<rule name="LogFile" patternSyntax="ECMAScript">
<match url="socket.io"/>
<action type="Rewrite" url="server.js"/>
</rule>
</rewrite>
interface.html
<script src="socket.io/socket.io.js"></script>
<script>
var address = window.location.protocol + '//' + window.location.host;
var details = {
resource: (window.location.pathname.split('/').slice(0, -1).join('/') + '/socket.io').substring(1)
}
var client = io.connect(address,details);
...
});
for your interface.html
you have no </script>
tag also, the });
shouldn't be there as there is no opening for it i.e. its a invalid syntax.
Basically delete everything after var client = io.connect(address,details);
and then put a </script>
also for server don't use io.set - that is dont use this code -
var io = require('socket.io')(app);
io.set('resource', '/project/socket.io');
use
var io = require('socket.io')(app, {
transports: [
'flashsocket',
'htmlfile',
'xhr-polling',
'jsonp-polling',
'polling'
],
path: '/project/socket.io'
});
this eliminates the requirement of using process.env and also automatically sets ports as per any server framework like hapi
also make sure internal websockets are disabled in Web.config i.e. in between </rewrite> & </system.webServer>
Use:
<directoryBrowse enabled="false"/>
<webSocket enabled="false"/>
when working depending on your code it should look something like this
I have done what you show me I now can load the socket.io, but it has another error. I think it is my package.json have something wrong.
Package.json
{
"name": "dante",
"version": "1.0.1",
"dependencies": {
"graceful-fs": "^4.1.11",
"socket.io": "^2.0.3",
"webpack": "^3.5.5"
},
"devDependencies": {
"webpack-dev-server": "^1.16.5"
}
}
index.html
<script>
var address = window.location.protocol + '//' + window.location.host;
var details = {
'path': ('/' + window.location.pathname.split('/').slice(0, -1).join('/') + '/socket.io').substring(1)
};
var client = io(address,details);
client.on("news",function(data){
console.log(data);
});
</script>
Edited -
Turns out the problem is your variable i.e.
namespaces
the reason being that it outputs
'[object'
if you use
new RegExp( '^' + '[object]' + '$');
in a test script, everything runs, however if you use
new RegExp( '^' + '[object' + '$');
it throws an error the same as yours does
that means changing
new RegExp( '^' + namespaces + '$');
to
new RegExp( '^' + namespaces.substr(1) + '$');
should fix the issue
as it changes the output from
'[object'
to
'object'
as for IO error, either debug.js is part of your socket.io code and therefore that error is causing the IO error or the path you specified in index.html for socket.io is incorrect and therefore IO is not defined
Hi @gundambandai were you able to get this implementation working?
@gundambandai @gciluffo did any of you get it working?
works fine on all 1.x socket.io but gives the 200 response with socket 2.x.
if i run 2.x directly from node without iisnode it works fine som has something to do with iisnode.
@rramachand21 will you add support for socket.io 2.x to IISNode?
@phict @gciluffo socket.io 2.x works just fine for me as per https://github.com/tjanczuk/iisnode/issues/475 after upgrading from 0.x to 1.x to 2 .x it worked for me after tweaking a few things and as per @gundambandai his code was working on 2.0.x as well, his problem was a syntax error because he was not using substring commands (i.e. it was a coding error and not an implementation error with 2.x)
@phict @AsimRamay Think I am now onto a different problem. How are you keeping track of your sockets? I think Azure hosts Node IIS as a stateless application under a stable subscription, so the sockets are not actually being cached in memory.
EDIT: Nvm I have found using https://www.npmjs.com/package/socket.io-redis works for caching sockets