So, for a quick overview:
I've been using NodeJS on my server-end to handle things such as saving files, getting data, and doing other stuff that Javascript can't. A good example of this is that I'm using a bot that connects to the SteamAPI, and I can show information about this on the client-side through sockets.
Sockets are essentially the main thing we use to connect client-side code to server-side code. I've come to learn that they're extremely helpful, as lots of time Javascript code lacks certain features due to security restrictions. I've been writing my sockets in PHP recently. Here's a small example of a socket I used in my code recently:
Client-End
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sendToWebsite(info) { | |
var net = require('net'); | |
var port = 5501; | |
var ip = "127.0.0.1"; | |
var client = net.connect({ | |
port: 5501 | |
}, function () { | |
logData("Connected to web server [sending]."); | |
client.write(info); | |
logData("Wrote: " + info + " to server."); | |
}); | |
client.on('data', function (data) { | |
logData("Received data from server: " + data.toString()); | |
client.end(); | |
}); | |
client.on('end', function () { | |
logData("Disconnected from server."); | |
}); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
echo 'Sent data. Time to listen!'; | |
$socket = socket_create_listen(5501); | |
while(true){ | |
//$client = socket_accept($socket); | |
while($client = socket_accept($socket)) { | |
$buffer=socket_read($client, 512); // Time to implement server-side commands >:D | |
if(strrpos((string)$buffer, "alert") !== false){ | |
$message = substr($buffer, 6); | |
echo("Alert request received from bot. Message was "); | |
echo($message); | |
// Load the HTML document, add element | |
} | |
elseif((string)$buffer == "close"){ | |
echo("Requested from bot to quit server. \n"); | |
echo("Quitting...\n"); | |
echo("Closed client.\nClosing socket...\n"); | |
// socket_close ($socket); | |
} | |
elseif(strrpos((string)$buffer, "otherID") !== false) { | |
echo($buffer); | |
echo("\nisbuffer"); | |
$array=json_decode($buffer); | |
$otherID = $array->{"otherID"}; | |
// We now have all that information. Better log it and get it to the website! | |
} | |
socket_write($client,$buffer); | |
socket_close ($client); | |
} | |
} |
As you can see above, the Client-End is coded in PHP, while the Server-End is coded in NodeJS. Essentially, our Server-End is listening constantly on port 5501, while our Client-End is occasionally sending data to it. We can send the data as JSON, and then later parse it on the server-end, and send more information back based on what the sent information was.
This is all good, however, I found a new library yesterday called Socket.io. It's still doing the same things, but when you see how much time it saves, it's ultimately worth the switch. I'm going to be switching all my code to Socket.io based code over the next few weeks.
Socket.io makes socket communication amazingly simple. Their example of a simple chat server showed how you could easily have socket-based communication and an entire chat server in just a few lines. I thought that was amazing, especially thinking back to our old AndroidRPG project from earlier this year - we could have saved all that time with a simple plugin. The best thing is, it's been ported to all sorts of mobile platforms, including Android, and it's pretty efficient.
I've also been working with a few more libraries:
-Progressbar.js - Truly amazing progress bars. Allows for really nice ways of showing loading of items, live item counts, etc.
-net module for NodeJS - Allows for socket connections. However, I'm currently in the process of merging to socket.io as previously stated.
-Spotify Android SDK - I'm also in the process of making a music app for Android. I use Spotify to authenticate and play music.
-Steam Web API - I'm working on a bot that is 'talking' to the Steam Web API to grab virtual item information
-Bootstrap - Quite possibly the best API I've ever dealt with. It makes all your pages look amazing, it's open source, and so many people use it.
I'm also working with several more APIs [at least 5-10 more], but my progress with them isn't as far. As you can see, I've really been working hard on API-based programming and Javascript/NodeJS in my spare time, mainly because I'm surprised at how much things they can do!
Great post! It is not accurate to say that you are using Node.js to do things that "JavaScript can't", since Node.js is nothing more than server side JavaScript. But yes, you use node to do server side programming, whereas JavaScript before Node.js was limited to the client side.
ReplyDelete