{"id":2211,"date":"2022-08-30T15:22:58","date_gmt":"2022-08-30T15:22:58","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2014\/01\/04\/socket-io-on-method-callback-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:22:58","modified_gmt":"2022-08-30T15:22:58","slug":"socket-io-on-method-callback-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/socket-io-on-method-callback-collection-of-common-programming-errors\/","title":{"rendered":"socket.io on method callback-Collection of common programming errors"},"content":{"rendered":"<p><strong>server.js<\/strong><\/p>\n<pre><code>socket.on('adduser', username, successcallback){\n\nif(users.hasOwnProperty(username)){\n\n} else {\n\n    socket.username = username;\n\n    users[username] = { name: username };\n\n    socket.emit('updatechat', 'SERVER', 'you have connected');\n\n    socket.broadcast.emit('updatechat', 'SERVER', username + ' has connected');\n\n    io.sockets.emit('updateusers', users);\n\n    successcallback({ msg: 'User ' + username + ' has been successfully added' });\n}\n\n});\n<\/code><\/pre>\n<p><strong>index.html<\/strong><\/p>\n<pre><code>socket.on('connect', function(){\n\n    var username = prompt('Whats your name?');\n\n    socket.emit('adduser', username, function(data){\n        console.log(data);\n    });\n\n});\n<\/code><\/pre>\n<p>This makes my app crash. Isn&#8217;t this how a callback should be implemented?<\/p>\n<ol>\n<li>\n<p>The definition of the adduser message handler is missing a <code>function(...)<\/code>. It should be:<\/p>\n<pre><code>socket.on('adduser', function(username, successcallback) {\n  ...\n});\n<\/code><\/pre>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2014-01-04 14:19:38. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>server.js socket.on(&#8216;adduser&#8217;, username, successcallback){ if(users.hasOwnProperty(username)){ } else { socket.username = username; users[username] = { name: username }; socket.emit(&#8216;updatechat&#8217;, &#8216;SERVER&#8217;, &#8216;you have connected&#8217;); socket.broadcast.emit(&#8216;updatechat&#8217;, &#8216;SERVER&#8217;, username + &#8216; has connected&#8217;); io.sockets.emit(&#8216;updateusers&#8217;, users); successcallback({ msg: &#8216;User &#8216; + username + &#8216; has been successfully added&#8217; }); } }); index.html socket.on(&#8216;connect&#8217;, function(){ var username = prompt(&#8216;Whats your name?&#8217;); [&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-2211","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2211","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=2211"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/2211\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=2211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=2211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=2211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}