Archive for March, 2012

NodeJS + Socket.io Random Freezing

Mar 30

So I recently found this out the hard way… if you are running a socket.io service on a non-standard port and have a firewall (which you most likely do lol) then you may be experiencing random freezing. TO MY KNOWLEDGE, this is caused by the UDP port being blocked by the firewall.

I was experiencing this issue and I unblocked that port in the firewall yesterday and I have not seen my app freeze yet! 😀

YAY!

Points for super short blog entry lol but just in case anyone can benefit from this 😛

Filed Under: Dev

NodeJS – Execute a Shell Script / Command

Mar 10

So on my continued quest to build an awesome shoutbox in NodeJS + socket.io, I have discovered how to run shell scripts from NodeJS. This was a request from one of the staff members on DarkUmbra.Net to be able to restart the server if something messes up. What resulted was a shell script that restarts NodeJS and whatever server you are running.

This is the NodeJS code to do this:

You can also choose to output something back to the client if need be.

Filed Under: Dev

Awesome Linux Process Management

Mar 9

I recently discovered a neat trick with linux processes that allow you to a) run them in the background and/or b) keep them running after you disconnect from SSH or your connection, however that may be.

Running processes in the background is quite common so if you’re familiar with linux then you’ll know that appending an & to the end of the command will throw it in the background out of view and allow you to keep working, while it runs.

The issue with this is that when you disconnect from SSH, that script will cease to run. The way you can prevent that is you can redirect output to the nohup command which will continue to run after closing your connection.

You can also combine the two to run it in the background AND keep running if you drop connection.

Now the neat thing about nohup is it stores any output from the command in ~/nohup.out and can be viewed using any method (tail, nano, etc). But what if you wanted to view the output in realtime as if you were viewing the command running? You can use the tail -f command to do so. This will load the last few lines of the file as well as print out any new lines that are posted to the file while the command is running.

So when I run my nodeJS server for example, I run it as follows so it’s in the background, keeps running if I drop connection, AND I load the log file into tail -f so I can view the output as normal. This prevents the server from stopping if I lose connection (like when I’m working from the bus :P) as well as any errors 😀

Yay for awesome process management in unix/linux!

Filed Under: Dev

NodeJS – “CURL”

Mar 9

If you ever find yourself in the same situation I did where I needed nodeJS to pull data from a remote “API”, you’ll possibly know that it’s not as straightforward as it can be 😛

Since this took me a while to find I think it’s only fair that I post it in case anyone else can make use of it. To get data from an external server you can use the following function.

Hopefully that’s helpful 🙂

Filed Under: Dev

NodeJS + Socket.io + PHP

Mar 9

Over the past few days, I’ve been writing a custom Chat in an attempt to optimize resource consumption and make use of AWESOME web sockets. I stumbled across the idea of being able to “push” events that happen on the site such as receiving new messages, or new posts on topics you follow to the shoutbox. As a result I went out looking for a way of PHP connecting to a nodeJS server and being able to broadcast to the socket from there.

The result is a sort of hack which involves creating a secondary HTTP server and listening for connections. When a connection is received, the URL is parsed with the parameters and action can be taken accordingly 🙂

Here’s the code:

To push to it from PHP just fire a file_get_contents() or curl request to it. For example the one I use is as follows:

Hopefully this can help someone else with the same issue I had 😀

Filed Under: Dev