Simple WebSocket & NodeJS Server Example

I’ve been searching for a NodeJS server for web sockets, but socket.io, required both the client and server to be run from within node. I wanted to connect to this server via a normal web page using WebSockets from HTML5. Here’s some code, hope you find it useful:

https://github.com/scottpreston/examples

Server Code

var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({port: 3000});

wss.on('connection', function (ws) {
    ws.on('message', function (message) {
        console.log('received: %s', message);
        ws.send(message + "!!!!");
    });
});

Client Code

    var websocket = new WebSocket('ws://localhost:3000');
    websocket.onopen = function (evt) {
        websocket.send("This Rocks!");
        websocket.close();
     }
    websocket.onmessage = function (evt) {
        console.log('RESPONSE: ' + evt.data);
    };

 

Home Grown Script Loader

I was wondering how RequireJS and AMD worked. So I created this snip. It might not be correct but it worked for me.

load('foo', function (foo){
        var f = new foo();
        f.a = 100;
        f.b = 200;
        alert(f.add());
    });

    function define(name, callback) {
        modules[name] = callback;
    }

    function load(file, callback) {
        var script = document.createElement('script');
        script.type = 'text/javascript';
        script.async = true;
        script.src = file + ".js";
        (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(script);
        script.onload = function() {
            callback(modules[file]);
        };
    }

    var modules = [];

// foo.js
define("foo", function() {
    var foo = {
        a: 1,
        b: 2,
        add: function () {
            return this.a + this.b;
        }
    };
    return foo;
});

Created A Chevelle Blog

I decided to create a blog for my chevelle because there was so many things I was learning about it and thought I should help out some other folks working on their car.

I’ve researched a lot on YouTub and the internet as well as Chevelles.Com, but making a post there about what I learned just didn’t seem right.

To learn more visit: scottschevelle.com.

Dead APC Back-Ups & Alternative Batteries

I recently had an UPS failure. My UPS and APC 650 ES, has a battery they call for is the RBC17. Which on Amazon.Com is $39.99. However when you peal the sticket off you get Vision UB1290. This battery (which is the same battery) cost around $15.00.

I imaging this happens a lot with companies looking to make a cheap buck. Hopefully this post catches someone search on “Alternative APC Battery” or something similar.

Chevelle Updates – Part 2

1) Throttle Cable: So I found that I needed a new throttle cable as the original one was 21.5″ long. For a Holley carb this was about an inch too short. I decided to get a Lokar 24″ universal cable with a new bracket. This should allow me to cut-to-fit the cable and get full throttle on it.

2) Rear Brakes: I wanted to get the car moving and rushed the rear brakes. Turns out the line does not fit as expected and I still need to install the emergency cable. I’ll take it out for a test run or two then put it up on blocks to fix this.

3) Alignment: I need to get this done after the brakes.

4) Pesky Oil/Fluid Leaks: My plan is to redo the engine this year, so I’m hoping to hold off for now on fixing these. The one near the distributer I need to keep a closer eye on. I’m not sure of the other leaks, I’m sure I’ll find out those soon enough.

5) Radio / Electronics: Hoping to get these sometime this summer, but only if I can drive it around more than have it on blocks.

1971 Chevelle Updates

To get my car running here’s what I have done to it thus far.

  • New Diff – New Moser 12 bolt, Tru Trac, 3.42 gears.
  • New Rear Suspension - New Hotchkis control arms, stabilizer springs and shocks)
  • Front Suspension - Powder coated stock control arms, new Hotchkis springs and stablizer and new shocks.
  • Brakes – New lines, power booster and master cylinder new drums in rear and new disc and spindles in front.
  • Body Pan – Sanded & Painted with rustoleum and put new sound absorbing padding in with original carpet just cleaned.
  • Starter Wiring – This was rigged up with a pushbutton switch vs. the ignition, had to correctly wire it to get working.
  • Alternator & Regulator & Wiring – This was again rigged up, the regulator failed, so the gen light was on all the time, I ended up replacing the alternator with a PowerMaster 47294, 140AMP. It has an internal regulator and the wiring was redone with 10 gauge and 8 gauge.
  • Wiper Motor – I purchased a switch but this did not work, so I had to rig up something.
  • Seat Belts – Probably the biggest pain, I purchased some RetroBelt, but the mounts don’t work well and the belt is marginal. This will need to get replaced.
  • Throttle Bracket & Cable – The cable was too long and the cable only pulled to about 1/2 throttle. I purchased a new bracket and cable from Amazon.
  • K&N Air Filter & Top – The old ones are black, need some new ones.

iPhone 5 Calendar Alerts

I’ve found out the different things you need to get your calendar alerts to work with Google Calendar.

  1. Go to Google Settings and Turn on Alerts
  2. Make sure the alert type is pop-up.
  3. Make sure it’s set to 5 minutes, then make sure your iPhone is also set to 5 minutes.
  4. Make sure you have a sound selected on your iPhone

That’s it. Happy alerting…

Google Maps, Not Showing, Grey Box?

I was working today on a bug with google maps not showing. It turns out it was because I had a css with overflow:hidden.

In order to fix this you’ll need to set the overflow:visible on any css elements for your #{MAP_ID} and subsequent <div> tags.

div#map,div#map div{ overflow:visible;}

A Simple MySQL Trigger Example

I was searching for this a few days back and I was struck at how Google had all these terrible explanations at the top of the results. Here’s a simple way to create and understand triggers in mysql.

CREATE TABLE IF NOT EXISTS `emails` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `emails` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

CREATE TABLE IF NOT EXISTS `emails_triggers` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `emails` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

DELIMITER $$
CREATE TRIGGER trigger_test AFTER
INSERT ON emails
FOR EACH ROW
BEGIN
 INSERT INTO `emails_triggers` (email) values (NEW.email);
END$$
DELIMITER ;

insert into emails values('test@test.com');
select * from emails_trigger;

Just a note: the DELIMITER keyword is used via a tool like PhpMyAdmin or the MySQL Client. I could not get this keyword to be recognized by my SQL editor and had problems until I just pasted it via PhpMyAdmin.

The JavaScript Module Pattern

One thing that really bothers me is often how obtuse programmers explain relatively simple things. Take for example the JavaScript Module Pattern.

<script type="text/javascript">
    var module = (function () {
        var foo = 0;
        return {
            bar:function () {
                foo = foo + 1;
                alert(foo);
            }
        };
    }());

    module.bar();
    module.bar();
</script>

This is really just two things:

  1. An anonymous function which allows for immediate execution.
  2. Returning a function, which behave like “public variables/methods”, keeping the other functions/properties inside the module which behave like “private variables/methods”

That’s it, no need to explain this with 5 pages of explanation about encapsulation, etc.