{"id":440,"date":"2022-08-30T15:01:23","date_gmt":"2022-08-30T15:01:23","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/basic-socket-io-application-not-working-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:01:23","modified_gmt":"2022-08-30T15:01:23","slug":"basic-socket-io-application-not-working-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/basic-socket-io-application-not-working-collection-of-common-programming-errors\/","title":{"rendered":"Basic socket.io application not working-Collection of common programming errors"},"content":{"rendered":"<p>I created and ran a <code>nodejs<\/code> file <code>server.js<\/code>:<\/p>\n<pre><code>var io = require('socket.io').listen(8080); \nio.set('log level', 1);\nio.sockets.on('connection', function (socket) {\n  \/\/ pass  \n});\n<\/code><\/pre>\n<p>and a client <code>client.js<\/code>:<\/p>\n<pre><code>window.onload = function(){\n    var connected = function(){\n    console.log(\"connect\")\n    }\n\n    var start = function() {\n        socket = new io.Socket();\n        socket.connect('http:\/\/localhost:8080');\n        socket.on('connect', connected);\n    };\n    start();\n}\n<\/code><\/pre>\n<p>I included <code>socket.io<\/code>:<\/p>\n<pre><code>\n<\/code><\/pre>\n<p>but in the debugger I look: Request URL:http: \/\/undefined\/socket.io\/1\/?t=1359905123022<\/p>\n<p>I&#8217;m using django, and I ran django server in 8000 port&#8230;<\/p>\n<p>WTF?<\/p>\n<ol>\n<li>\n<p>try to replace this lines in client.js:<\/p>\n<pre><code>socket = new io.Socket();\nsocket.connect('http:\/\/localhost:8080');\n<\/code><\/pre>\n<p>to:<\/p>\n<pre><code>var socket = io.connect('http:\/\/localhost:8080');\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-09 19:42:53. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I created and ran a nodejs file server.js: var io = require(&#8216;socket.io&#8217;).listen(8080); io.set(&#8216;log level&#8217;, 1); io.sockets.on(&#8216;connection&#8217;, function (socket) { \/\/ pass }); and a client client.js: window.onload = function(){ var connected = function(){ console.log(&#8220;connect&#8221;) } var start = function() { socket = new io.Socket(); socket.connect(&#8216;http:\/\/localhost:8080&#8217;); socket.on(&#8216;connect&#8217;, connected); }; start(); } I included socket.io: but in [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-440","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/440","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=440"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/440\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=440"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=440"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=440"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}