Quirky JavaScript Object Access

My friend asked me a simple JavaScript question last night… or so I thought.

The premise is that we have a javascript object like this:

Now the question is, how do you access that items  array?

From what I know:

  • An object’s keys are stringified so that they are strings.
  • Using object.X  is the same as writing object['X'] .

So, I told my friend that we can access that items  array by using object.1.items  or  object['1'].items .

However, object['1'].items worked, while object.1.items threw an error! To my surprise, object[1].items  also worked!

Now let’s say the javascript object is like this:

In this second case, I tested it in the Chrome browser console and confirmed that it is how I originally thought it would work: object['one'].items  and object.one.items  both work (and object[one].items  does not work).

I think I will just use the object['X']  format since it works in both cases: when the key is a number and when it is a word.

I still don’t know the reason for the quirkiness when the key is an integer, so feel free to leave a comment and let me know!

Pigeonhole Sort

Pigeonhole sort is an interesting sorting algorithm that makes use of an array filled with zeros and its indices. It can sort values in linear time and is relatively simple to implement. However, the tradeoff for its sorting speed is the space that it requires. Also, since it makes use of array indices, only positive integers can be sorted using this algorithm.

So the basic idea of pigeonhole sort is:

  • Create a new array (let’s call it pigeonholeArray) and fill it with zeros.
  • Iterate through the unsorted user’s array (inputArray).
  • For each value of the inputArray, add 1 to the index corresponding with the value in the pigeonholeArray.

Let’s say for this example, we want to sort this inputArray:

The first thing the algorithm needs to know is what the maximum value of the inputArray is. We need to create a pigeonholeArray that has indices that go up to the max value. If we don’t know the maximum, we’ll have to loop through the inputArray one time to figure it out. In this case, our biggest value is 7, thus we need to create a pigeonholeArray that can hold 8 elements (which will have indices 0 to 7).

Now we need to loop through the unsorted inputArray (again), and as we do that, we will add 1 to the pigeonholeArray’s value at the pigeonholeArray’s index corresponding with the inputArray’s value. So as we iterate our input array, our pigeonhole array will look like this:

As you can see, after looping through the entire inputArray, our pigeonholeArray will contain all of inputArray’s values. We will just have to make one last iteration through the pigeonholeArray. For each pigeonholeArray’s value that is greater than zero, we’ll add the pigeonholeArray’s index to the inputArray. We can replace the old values in the inputArray so that no new array needs to be created.

Check out the completed algorithm below:

How the JavaScript Event Loop Works

Event Loop

JavaScript has this system called an event loop. I’m not sure if my idea of it is exactly correct, but I will try to explain it from what I can recall from the lecture.

So basically, the event loop is something that is continuously running in the background of your website only if there is nothing in the call stack. It does 3 things:

  1. Invokes functions sitting in the call stack.
  2. Checks the event table for functions.
  3. Checks the message queue for functions.

Call Stack

The call stack contains functions that need to be invoked. If the functions have any callbacks, those will get added into the event table to be invoked when it is time to do so.

Example: Let’s say the current time is 2:00:00pm, and the call stack contains this function that logs “Hello World” after 1 second.

That will get invoked, and the callback function console.log("Hello World")  would be added to the event table with the time set to 2:00:01pm.

Event Table

The event table stores more functions with the specific time that they should be ran. If the time in the table is greater or equal to the current time, then the function will be moved to the message queue.

Example: console.log("Hello World")  would only be added to the message queue when the clock has turned to 2:00:01pm.

Message Queue

Lastly, the message queue contains functions that are next in line to be ran. These functions will be popped off the queue and be added to the call stack.

Example: console.log("Hello World")  will be moved into the call stack. This completes a full circle, and the event loop will repeat agian.

My Two-Day Solo MVP Project

YouTube Live

For my two-day solo Minimum Viable Product Project, I decided to make a website for sharing synchronized YouTube videos. This website would be good for people who want to watch a YouTube video together, at the same time, but aren’t in the same room. (Or maybe they are in the same room, but want to watch on separate devices.)

The user who enters a URL (a host) can play, pause, and seek the video. The users who watch the video (the viewers) will have their embedded players behave exactly the same as the host’s player. Pretty simple.

Scoping

My original idea had more features, but I quickly learned that I had to cut down the scope, and then cut it down again for it to be do-able in two days. I was going to have user sessions/authentication, rooms, and a chat box. I was also going to use Angular. But all those went away.

Requirements

The tools that I ended up using for this project are:

  • Node.js for the server
  • Firebase for the database
  • jQuery for getting the URL from the input box
  • YouTube Player IFrame API

Player API Functions

After embedding the YouTube player on my website, I needed access to some of the functionality. With the API, I was able to use these functions:

First, I use loadVideoById()  to load a YouTube video when a URL is submitted.

Then when the host’s player starts playing, the viewers will invoke playVideo() , otherwise pauseVideo()  in all other cases. I don’t use stopVideo()  because that will stop the player from buffering.

When the host is in paused state and moves the video to a new time position, this will invoke getCurrentTime() . The viewers will invoke seekTo()  to be at the new location.

Database

I’ve spent 15 minutes playing with Firebase during one of the other projects before, so I know how easy it is to set and get data. I love how Firebase automatically sends a trigger back to the website when a value is changed in the database.

In my Firebase database, I store 3 things:

  1. The state of the host’s video player (playing, paused, buffering, etc.)
  2. The YouTube video ID that the host is watching
  3. The time position of the host’s video player

Whenever host played or paused a video, Firebase sent a trigger which would invoke a callback function to play/pause all of the viewers’ video players. It was pretty simple.

If the host seeked the video ahead, Firebase would get the new time position, which would then again trigger a callback on the viewers’ video players and move them to the new time.

Same thing will happen when the host enters a new YouTube video ID.

Extra Feature

I also had some extra time, so I was able to add a search feature. The host can input search terms, and the first video result of the query will be loaded.

This is done by using the loadPlaylist()  function and passing this object for the argument:

Using this function will actually load a playlist of the first 20 videos found in the search, which I don’t want. So when the first video is playing, I invoke getVideoUrl()  to get the video’s URL, then I save the video ID to Firebase, and then play the video by ID to clear the playlist.

Stumbling Block

An obstacle that I ran into about 1 hour into the project was that I chose to use the YouTube Player JavaScript API. That doesn’t seem like a problem at all, right? Well, it wouldn’t be if the JavaScript API was able to embed the HTML5 video player. However, it only embeds the Flash video player, which iPhones and Androids don’t support! So the solution was kind of simple; I switched to the YouTube Player IFrame API.

Final Result

I deployed my code onto the Microsoft Azure platform, and you can view it here: http://youtube.azurewebsites.net/

YouTube Live Player

Adding a Custom Domain from Namecheap to Microsoft Azure

To add a custom domain name to a website hosted on Microsoft Azure (like this website), you must first go into the Azure’s SCALE configuration page and make sure the web hosting plan mode is anything above Free mode. For me, I picked Shared mode.

Web hosting plan mode

You’ll then see a “Manage Domains” button at the bottom of Azure. Click that and you will see “THE IP ADDRESS TO USE WHEN YOU CONFIGURE A RECORDS”. Keep note of the IP address.

First, we have to go into Namecheap. Click on the domain you want to use, and click “All Host Records” on the left side under Host Management.

We need to create one “A (Address)” and three “CNAME (Alias)” entries. Create the entries as shown below:

Save the settings and you should be done with Namecheap.

Namecheap host records

Now lastly, go back into Azure’s “Manage Custom Domains” and type in the domain name that you’ve just configured. The input box should show a green checkmark indicating that Azure was able to communicate with your domain name on Namecheap.

Azure manage custom domains

Click the checkmark and you can now access your Azure website with your custom domain!

Becoming a Software Engineer

hackreactor

I quit my IT job 2 weeks ago so that I could attend a coding boot camp called Hack Reactor. It is a 12 week course (6 days a week, 11 hours a day) that started today. Based on the previous graduations, 99% of the students found a software engineering job within 3 months with an average starting salary of $105,000, as of this blog post date.

I’m super excited and can’t wait to see how much I will grow and where I will be after I graduate.

Samsung TV Overscan HDMI Fix

I connect my hackintosh to my Samsung TV to watch movies, but there’s a problem where the TV cuts off part of the edges.

tv1-edgecut

If I open up the OS X Displays Preferences, there’s a checkbox for “Overscan”. Unchecking it will show my entire desktop on the TV with nothing cut off, but now there’s a black border.

tv2-osx tv2-osxoverscanoff
So I’ve been using it like that for a few weeks now, but I decided to look for a fix to get rid of the black borders. I searched online and found this post from storageforum.net: How to Disable overscan on a Samsung “SMART” tv – solved

What the user Sol did was changing the “Edit Name” for the HDMI connection to “PC”. So I tried it out.

tv3-hdmi tv3-edit tv3-pc
Success?? No, not yet. It only made the black border even bigger for me!

tv4

I opened up the OS X Displays Preferences again, and turned Overscan back On, and voila! My whole desktop can be seen now, with no black borders on the Samsung TV!

tv5

P.S. My Samsung TV is new, and I haven’t removed the plastics and energy guide sticker yet. :)

TMS Cannot Upload New Software (404)

Our Cisco TelePresence endpoints are currently on TC6.0.1, while the latest being TC7.1.3. So I opened up TMS > Systems > System Upgrade > Software Manager; this is where we can upload the latest files to TMS, so the endpoints can download the updates easily.

Now the problem is when you click “Upload New Software”, select the TC7.1.3 pkg file, and hit the Upload button.

upload

Everything looks fine, right until the upload reaches 100%. At that point, it’ll show: “Server Error – 404 – File or directory not found.”

fail

The reason is that TMS only allows files that are ~300MB to be uploaded, and the TC7.1.3 pkg file is ~320MB… just a little bit over that upload limit.

The solution: Remote Desktop into the TMS server and edit the Web.config file, located at “C:\Program Files (x86)\TANDBERG\TMS\wwwTMS\”.

Find this line:

And replace it with this line:

Then find this line:

And replace it with this line:

This will increase the upload file size limit to ~500MB. Now restart IIS and the new limit will be used.

Tip: Copy the Web.config file to the Desktop and edit that copy. When finished editing, save it, drag it back into the wwwTMS folder, and click Replace file.

Migrating TMS Agent to TMS Provisioning Extension

One of the things that must be done first before upgrading Cisco TelePresence Management Suite from v13 to v14, is to migrate TMS Agent to TMS Provisioning Extension. My company’s TMS was on v13.1, and the latest version was v14.3.2. After reading through the upgrade guides, I figured out that my steps had to be done in this order:

  1. Upgrade TMS from 13.1 to 13.2.x.
  2. Install TMSPE 1.0.
  3. Upgrade TMS from 13.2.x to 14.3.2.
  4. Upgrade TMSPE from 1.0 to 1.1.
  5. And finally upgrade TMSPE from 1.1 to 1.2.

This should have been pretty easy and straight forward, but we wanted to migrate TMS from its physical box to a virtual machine. So what I did was installed a fresh copy of TMS v13.1 on a brand new Windows Server 2008 R2. I did a SQL backup from the physical box and restored it to our SQL server that we wanted to use. Then I upgraded TMS v13.1 to v13.2.

This is the point where I thought all the data was restored, upgraded, and ready to be migrated. I installed TMSPE and ran the migration tool, but for some reason it said Migration Successful with 0 users migrated.

finish1

I was confused, so I had to do a bit of googling. Thankfully, I was able to find this post written by cfiestas on the Cisco forums: How to transfer OpenDS/Provisioning VCS data to TMS for recovery purposes. It turns out that the TMS Agent database isn’t stored in the SQL database; the TMS Agent data is stored in C:\Program Files\TANDBERG\TMS\Provisioning\OpenDS-2.0\db\userRoot. So I went to that path on the physical TMS box, and copied the folder over to the new virtualized TMS.

opends

This time when I reran the TMSPE migration tool, 37 users were migrated successfully! Now I am able to upgrade TMS v13.2 to v14.3.2, and upgrade TMSPE to v1.2.

finish2

TMSPE Installation – SQL Authentication Error

Upgrading an old version of the Cisco TelePresence Management Suite (v13.1) to a newer version (v13.2+) needs TMS Agent to be migrated to TMS Provisioning Extension.

When running the TMSPE installer (TMSProvisioningExtension.exe), it will ask you for the SQL server that you want to use.

tmspe1

After entering the correct information, you may encounter this error message:

“A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 – Error Locating Server/Instance Specified)”

tmspe2

The solution is very simple; the installer needs to know what the port number of the SQL Server is. All you have to do is change <SQL Server Name> to <SQL Server Name>:<port number>! In my case, the SQL Server was using the default port (1433). So I just had to change “sql-06” to “sql-06:1433”.

tmspe3