{"id":873,"date":"2018-04-13T12:43:43","date_gmt":"2018-04-13T12:43:43","guid":{"rendered":"http:\/\/smartfoxserver.com\/blog\/?p=873"},"modified":"2018-04-13T13:32:54","modified_gmt":"2018-04-13T13:32:54","slug":"sfs2x-and-node-js","status":"publish","type":"post","link":"https:\/\/smartfoxserver.com\/blog\/sfs2x-and-node-js\/","title":{"rendered":"SFS2X and Node.js"},"content":{"rendered":"<p>Some time ago we have been asked by the developers at <a href=\"http:\/\/www.gameshastra.com\" target=\"_blank\">Gameshastra<\/a> how to integrate our <strong>SFS2X JavaScript client API<\/strong> in <a href=\"https:\/\/nodejs.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">Node.js<\/a>, in order to create a Node application capable of interacting\u00a0with SmartFoxServer 2X.<\/p>\n<blockquote><p>We have a strategy game for which we built native client apps in Android, iOS and HTML5. We are also exposing the client functionality as a rest api for apps to be built for Amazon Alexa and Google Assistant. That rest api is being built on Node.js and we want to consume the same services hosted on SFS2X, so that we can manage all the platforms from the same server base and provide unified experience.<\/p><\/blockquote>\n<p><!--more--><br \/>\nAt the time, the bundling system\u00a0we used to generate our\u00a0API library caused some compatibility issues with Node, so we had to rethink the whole process. Today, after the release of <strong>v1.7.10<\/strong> of the API, compatibility is fully guaranteed and integration is very easy to achieve.<\/p>\n<p>In this post we are going to show how to connect to SmartFoxServer and handle the very basic connection and disconnection events. We will use the <strong><a href=\"https:\/\/www.npmjs.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">npm package manager<\/a><\/strong> to install the required libraries (including our JS API), so we assume that you have it installed (together with Node, of course) and that you have a basic knowledge of this tool.<\/p>\n<h2>Setup<\/h2>\n<p>Create a new folder called, for example,\u00a0<em>sfs-connect<\/em>. Open\u00a0it in a console window of your operating system and\u00a0run the npm&#8217;s\u00a0<em>init<\/em> command to generate a new <strong>package.json<\/strong> file. You can keep the default settings suggested by\u00a0the initialization process.<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">npm init<\/pre>\n<p>It is now time to\u00a0install the required dependencies.<\/p>\n<p>First of all, Node.js doesn&#8217;t support the <strong>websocket<\/strong> protocol out-of-the-box (like browsers). This is mandatory to connect to SmartFoxServer, so you\u00a0need to look for a specific library. The <a href=\"https:\/\/www.npmjs.com\/package\/ws\" target=\"_blank\" rel=\"noopener noreferrer\">ws: a Node.js WebSocket library<\/a>\u00a0package works just fine. You\u00a0can install it with npm&#8217;s <em>install<\/em> command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">npm install ws --save<\/pre>\n<p>Now you need the actual <strong>SFS2X JavaScript API<\/strong>. We provide an <a href=\"https:\/\/www.npmjs.com\/package\/sfs2x-api\" target=\"_blank\" rel=\"noopener noreferrer\">official npm package<\/a> which you can install with the following command:<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">npm install sfs2x-api --save<\/pre>\n<p>The <em>&#8211;save<\/em> flag adds the libraries to the <em>dependencies<\/em> section of the <strong>package.json<\/strong> file, which should now look like this:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n{\r\n  &quot;name&quot;: &quot;node-test&quot;,\r\n  &quot;version&quot;: &quot;1.0.0&quot;,\r\n  &quot;description&quot;: &quot;&quot;,\r\n  &quot;main&quot;: &quot;index.js&quot;,\r\n  &quot;scripts&quot;: {\r\n    &quot;test&quot;: &quot;echo \\&quot;Error: no test specified\\&quot; &amp;&amp; exit 1&quot;\r\n  },\r\n  &quot;author&quot;: &quot;&quot;,\r\n  &quot;license&quot;: &quot;ISC&quot;,\r\n  &quot;dependencies&quot;: {\r\n    &quot;sfs2x-api&quot;: &quot;^1.7.10-a&quot;,\r\n    &quot;ws&quot;: &quot;^5.1.1&quot;\r\n  }\r\n}\r\n<\/pre>\n<p>You are now ready to write the actual JavaScript code of a simple application.<\/p>\n<h2>Connection code<\/h2>\n<p>Create the <em>index.js<\/em> file in the project&#8217;s folder and past the code below:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n\/\/ Load libraries\r\nWebSocket = require(&amp;quot;ws&amp;quot;); \/\/ https:\/\/www.npmjs.com\/package\/ws\r\nSFS2X = require(&amp;quot;sfs2x-api&amp;quot;); \/\/ https:\/\/www.npmjs.com\/package\/sfs2x-api\r\n\r\n\/\/------------------------------------------------------------------------------\r\n\r\n\/\/ Create configuration object\r\nvar config = {};\r\nconfig.host = &amp;quot;localhost&amp;quot;;\r\nconfig.port = 8080;\r\nconfig.debug = true;\r\nconfig.useSSL = false;\r\n\r\n\/\/ Create SmartFox client instance\r\nlet sfs = new SFS2X.SmartFox(config);\r\n\r\n\/\/ Add connection event listeners\r\nsfs.addEventListener(SFS2X.SFSEvent.CONNECTION, onConnection, this);\r\nsfs.addEventListener(SFS2X.SFSEvent.CONNECTION_LOST, onConnectionLost, this);\r\n\r\n\/\/ Connect to SmartFoxServer\r\nsfs.connect();\r\n\r\n\/\/------------------------------------------------------------------------------\r\n\r\n\/\/ Connection event handler\r\nfunction onConnection(event)\r\n{\r\n\tif (event.success)\r\n\t{\r\n\t\tconsole.log(&amp;quot;Connected to SmartFoxServer 2X!&amp;quot;);\r\n\t\tconsole.log(&amp;quot;SFS2X API version: &amp;quot; + sfs.version);\r\n\t}\r\n\telse\r\n\t\tconsole.warn(&amp;quot;Connection failed: &amp;quot; + (event.errorMessage ? event.errorMessage + &amp;quot; (&amp;quot; + event.errorCode + &amp;quot;)&amp;quot; : &amp;quot;Is the server running at all?&amp;quot;));\r\n}\r\n\r\n\/\/ Disconnection event handler\r\nfunction onConnectionLost(event)\r\n{\r\n\tconsole.warn(&amp;quot;Disconnection occurred; reason is: &amp;quot; + event.reason);\r\n}\r\n<\/pre>\n<p>The initial section of the code loads the required libraries using CommonJS <em>require<\/em> syntax. This makes the <strong>WebSocket<\/strong> object available to the SFS2X API and the <strong>SFS2X<\/strong> namespace available to your code.<\/p>\n<p>In the next code section, the connection with SmartFoxServer is actually attempted after\u00a0instantiating the <strong>SmartFox<\/strong> class. This requires a configuration object and listener to be attached to make our code react to the connection and disconnection events fired by the server.<\/p>\n<p>This example of course only scratches the surface of the interaction with SmartFoxServer, but it should help you to get started in developing a multiplayer application in <strong>Node.js<\/strong>.<\/p>\n<p><em>A big thank you to Gopal at <a href=\"http:\/\/www.gameshastra.com\" target=\"_blank\">Gameshastra<\/a> for his feedback and help in refining our JS API.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Some time ago we have been asked by the developers at Gameshastra how to integrate our SFS2X JavaScript client API in Node.js, in order to create a Node application capable of interacting\u00a0with SmartFoxServer 2X. We have a strategy game for which we built native client apps in Android, iOS and HTML5. We are also exposing [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[23],"tags":[26,84,43,80,95,93,7,9],"_links":{"self":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/873"}],"collection":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/comments?post=873"}],"version-history":[{"count":25,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/873\/revisions"}],"predecessor-version":[{"id":898,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/posts\/873\/revisions\/898"}],"wp:attachment":[{"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/media?parent=873"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/categories?post=873"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/smartfoxserver.com\/blog\/wp-json\/wp\/v2\/tags?post=873"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}