Weekend project: Building a web chat application with Node.js

screenshot

I’ve been playing around with Node.js recently and taking some of the neat online lessons available through Nodeschool. One of the lessons involved streaming realtime data between the client and server using Socket.io.

This sounded insanely powerful and fun, so I took a break from the rest of the lessons and started playing around with Socket.io. They have a tutorial that explains how to build a simple, realtime chat application using its library in about 20 minutes.

I’ve uploaded the results of this little project to Github and you can view it right here. This little web app is more or less a result of tutorial and I’ve added some additional stuff as I played around with it:

  • Usernames!
  • Number of active users!
  • Display a list of all connected users!
  • Check for identical usernames and prompt for different name
  • Timestamps on messages!
  • Sanitizing user input!
  • Properly autolink URLs!
  • Detect if a link to an image is pasted in and expand!
  • Display welcome message on connect

All in all, it’s been a really fun learning process and has given me some ideas on how to use Node.js for future realtime applications.

Note: You can see this in action on Heroku. You’ll probably be the only one only, but hey! https://davechat.herokuapp.com/

Baseball Twitter: Fun with the jQuery and Twitter’s API

screenshot_redsoxThis past weekend, I played around with jQuery, JavaScript, and Twitter’s API to create a web app that displays realtime search results for various baseball teams. I figure this is particularly important since pitchers and catchers report in about 10 days! 🙂

Anyway, the goal of this project was to rapidly build out a web app that could search and parse publicly available data (e.g., tweets!) for mentions of particular baseball teams while using various jQuery and JavaScript libraries. I could see this app having potentially wider uses beyond baseball such as returning the latest tweets from an event or hashtag. It also gave me a chance to use various libraries and frameworks as well. I’m pretty happy with how it turned out.

You can see it in action right here and the source code is available on Github.

 

GIFtv – Turn any display into a GIF picture frame

Our office has an empty TV that isn’t being properly utilized at the moment. It’s definitely a #FirstWorldProblem, but it basically boils down to the fact that we have multiple displays that act as dashboards for our various services and properties.

Anyway, we had a spare television available to show whatever we wanted. THE ENTIRE INTERNET WAS OUR OYSTER and what better way to put it to use than to display a random assortment of animated GIFs.

Our troubles arose when we realized we had to manually change out the animated GIFs, as well as zoom in in via the attached machine’s web browser so that it would properly display images.

Annoying, right?

The Solution

Cue superhero music

Some Javascript, CSS, and PHP came the the rescue!

We’re using CSS to set a background image (based on one of our animated GIFs) that fills up the entire display. Depending on the settings of your viewport, the image has the possibility of being severly cropped, but hey, whatever!

We’re using some Javascript calls (thank you so much, Stackoverlow) to display a new animated GIF every 20 seconds or so (or really, whatever time you want, but I’m using 20 seconds), as well as build an array of animated GIFs from our images folder.

Which brings us to PHP. We’re using PHP to scan the contents of the folder and then using some Jquery calls to periodically call our PHP script to rescan and rebuild the image array — this means you can drop new animated gifs into the /images folder (or remove them, but why would you even) and it will automagically add them to the Javascript array for random consideration.

Yes? Yes!

How to use

Upload this script to your favorite web server that supports PHP. Drop images (of the animated GIF variety) into the /images folder. Open up this page in your favorite browser on your favorite device. Cool? Cool!

Demo time

So, what the heck does this thing look like? You can see it in action right here. (Make sure to set your browser to full screen presentation mode so you get the right effect).

GIFtv: http://dave.ly/stuff/giftv/

To-do’s

  • Better error handling
  • Figure out how to get Dropbox integration working so the whole company can get in on the fun

Soft ‘g’ or hard ‘g’?

Hard G, dudes. It’s JIFF! I’m not wrong. You’re wrong.

Special Thanks

Thanks to a few people who helped me try and figure this out.

  • Conrad Muan (@conradmuan)
  • Lydia Katsamberis (@llkats)
  • Jon Ursenbach (@jonursenbach)

You can download the source code for GIFtv on Github.

Quick email generator for contests and giveaways using PHP

tl;dr: Quickly generate multiple emails that you can send from your favorite mail client using mailto links and parsing data from various text files. (email addresses, email template, and optional redemption codes)

Background

Recently, I had to contact 50 readers by email for a giveaway that we were running on our site. We’ve run similar sorts of events before and it’s always a tedious experience. It involves something like this:

  1. Get email address from the list of winners.
  2. Write up some sort of email template.
  3. Go through and copy paste template into new email, and then copy and past the email address from list of winners.
  4. Oh, don’t forget to copy and paste the subject line: “Dude, you’ve won FOO! Nice work!”
  5. Oh, this particular giveaway features a unique code for each winner. Make sure you copy and paste that, too!
  6. After all this copy and pasting, send the email.
  7. Repeat this process until you’ve gone through the list of winners. Look at a clock and realize this has taken all afternoon.
  8. “Oh, man,” you think to yourself, “there’s got to be a better way.”

The Better Way

There is! Just download this project to your favorite PHP compatible server (or run an app like MAMP on your machine) and open up the “make_email.php” script.

There are a few required files:

  • emails.txt – This is the list of email addresses you wish to send emails to. Each email address should be on a new line. No commas or funny stuff separating them, please. I didn’t build in any sort of fancy error checking.
  • body.txt – This is the meat and potatoes. This is the message you’re going to want to send over and over and over and over. Sweet! One particularly important note: If there is some sort of unique redemption code or URL that winners will need to go to, you can add in ‘%code%’ (without quotes) to your email. The script will then automatically fill in unique codes when it generates the email (should you choose that options).
  • codes.txt – If you have a whole list of redemption codes or URLs, this is where you put them. Like emails, each code or URL needs to be on a separate line.
    That’s it! You might be wondering how you set the subject line of the email. Right now, it’s a variable at the top of the script. Just change that and you’ll be good to go!

Once you have these text files filled out, run the script, choose whether you’re generating emails with or without redemption codes and then fire away!

You can now click through the list and start firing off emails like you’re your own spambot. Except you’re giving people legitimately cool things. And you’ll still be able to enjoy your afternoon.

If you’re interested in checking it out, you can download the source code on Github.

Rolling my own URL shortener

Thanks to the popularity of micro-blogging sites such as Twitter, URL shorteners are popping up everywhere. Bit.ly, one of the most popular shortening services, has raised over $2 million dollars in funding!

Despite their popularity, URL shortening sites are by no means a permanent fixture on the internet. A perfect example of this was tr.im, which announced last month that it was shutting down (and taking down all the links users had generated with it). Fortunately, tr.im decided to reverse its decision soon after. However, this shows how potentially vulnerable these services are.

For fun (and peace of mind) – I decided to experiment with creating my own URL shortener for personal purposes! This would allow me to periodically backup my database of links and also serve as a way to bookmark and share content that I find interesting. Plus, I was able to acquire a fun URL as well.

Say hello to dave.ly!

If you see links from it, you know it’s from me and is something I thought was interesting enough to share! Feel free to check it out and let me know what you think. I’d like to do some more things in the future as well (i.e., show a list of the most popular links).