Monday, May 4, 2015

Starting out with NodeJS, Socket.io, and more server-side Javascript

So, recently out-of class I've been working on quite a few projects, many of which are relating to Javascript. A lot of them have been specifically aimed at websites that are using APIs to communicate, then connecting the client-end code to NodeJS on the server-side.

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
Server-End
Since a lot of new things were mentioned in this post, I'll try to get more into detail about them throughout the next week.

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!

1 comment:

  1. 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