Accessing a MySQL database from Node.JS

I just pushed node.dbslayer.js to GitHub, a very basic and easy-to-use interface to DBSlayer.

What is DBSlayer?

The DBacesslayer aka DBSlayer is a lightweight database abstraction layer suitable for high-load websites where you need the scalable advantages of connection pooling. Written in C for speed, DBSlayer talks to clients via JSON over HTTP, meaning it’s simple to monitor and can swiftly interoperate with any web framework you choose.

Developed by the New York Times to distribute the load of their MySQL servers, DBSlayer allows us to perform queries in a non-blocking way. Running a SQL query is as simple as:

connection.query("SELECT * FROM TABLE").addCallback(function(result){
	for (var i = 0, l = result.ROWS.length; i < l; i++){
		var row = result.ROWS[i];
		// do something with the data
	}
});

Have fun!

This post has 11 responses

Posted in Blog, Server side 3 months ago

The Google GO team is working on a WebSocket server

As part of the very comprehensive suite of packages Google GO is bundled with, a WebSocket server seems to be in the works:

http://code.google.com/p/go/source/browse/src/pkg/websocket/

Makefile
client.go
server.go
websocket.go
websocket_test.go

This is how you define a handler in GO’s WebSocket:

package main
 
import (
   "http"
   "io"
   "websocket"
)
 
// echo back the websocket.
func EchoServer(ws *websocket.Conn) {
     io.Copy(ws, ws);
}
 
func main() {
  http.Handle("/echo", websocket.Handler(EchoServer));
  err := http.ListenAndServe(":12345", nil);
  if err != nil {
      panic("ListenAndServe: ", err.String())
  }
}

Using node.websocket.js, it’d boil down to starting up the server:

$ node runserver.js --port="12345"

and defining the modules/echo.js handler:

this.onData = function(data, conn){
   conn.send(data);
}

I’d be interested in seeing how well GO performs with many simultaneous, long-lived clients.

This post has 1 response

Posted in Blog 3 months ago

Node.JS and the WebSocket protocol

After reading Simon Willison’s post on Node.JS, I decided I’d give it a try myself. Today I released node.websocket.js

After a couple of months I’ve been watching the progress on the WebSocket protocol, which gives JavaScript developers full-duplex communications channels in the browser. This is a very exciting alternative to the COMET techniques we’re used to seeing.

Node.JS is a framework for networked, event-driven applications, where JavaScript has a natural fit. This particular implementation is built on top of the excellent V8 engine.

Running the server is as simple as:

$ node runserver.js

Continue reading

This post has 18 responses

Posted in Blog 3 months ago

TextboxList 0.4 for jQuery is here!

Big update for my dear jQuery users. I’m rolling out TextboxList for jQuery 0.4 with these updates:

  • [FEATURE] Autocompletion with on-demand server querying
  • [ENHANCEMENT] All classes moved to $. to avoid global namespace pollution. Please make sure to prepend $. to TextboxList if you are upgrading from an older version:

       new $.TextboxList('#element');
  • [FEATURE] Easier jQuery-friendly initialization like this:

       $('#element').textboxlist({options});

    However, if you still need to access the TextboxList instance to call additional methods (like add), you’ll have to continue to use new $.TextboxList

  • [BUGFIX] GrowingInput now works in noConflict mode

  • [BUGFIX] Fix for GrowingInput to handle special characters and correctly calculate the input length.
  • [BUGFIX] Fix for support of multiple addKeys
  • [BUGFIX] Fix for focus problem when TextboxList gains focus through focusing an editable input
  • [BUGFIX] Autocomplete search term is now trimmed (thanks Mike Feng)
  • [BUGFIX] Fix for when the max option is used
  • [BUGFIX] Fix for unique = true and autocomplete.

As usual, head to the project page for download.

This post has 39 responses

Posted in Client side 4 months ago

Integrate Google Wave into your OS X system with Fluid

My Dock

Want to have Wave as a standalone application, with a badge that reflects the number of unread waves?
Follow these steps:

  1. Download the excellent Fluid.app
  2. Run it and create a new application like this:

    Launch Fluid.app and create it like this

    Download the icon I used here and make sure to select it in the appropriate dialog dropdown.

  3. Launch the application. When Wave loads for the first time, it’ll think you’re using an unsupported browser. This is not true, since Fluid.app is Webkit-based. Disregard this message and press continue. Google will remember this setting.
  4. To get the badge count functionality, I developed this userscript. Unzip it, go to the Google Wave applications, find the plugins menu and click Browser Usersripts Folder. Drop the file there and then make sure it’s enabled.

    Plugins menu

  5. One last useful setting for any Fluid.app you create is to make sure it doesn’t unload the website when you press ⌘ + W. Make sure only hides the window is checked:

    Fluid application settings

This post has 55 responses

Posted in Desktop 5 months ago

TextboxList 0.4: On-Demand suggestions

TextboxList 0.4 is out, with new features:

  • check option, which can allow you to specify a function to filter out new boxes that do not meet a requirement
  • encode now receives the complete values, not only the id or string of the bit. This gives you full control of what you want to send to the server.
  • Autocomplete now works with on-demand suggestions. As easy as this:

    new TextboxList('form_tags_input_4', {unique: true, plugins: {autocomplete: {
    	minLength: 3,
    	queryRemote: true,
    	remote: {url: 'autocomplete2.php'}
    }}});

And some bugfixes:

  • Fixed missing index when unique: false and autocomplete were used, which resulted in an error
  • Fixed problems with items with id 0, which incorrectly evaluated as false.

Head to the demo to see the new autocomplete in action, and as usual, report any bugs you might encounter.
Update: 0.5 is out already, which fixes a bug with the traditional use of Autocomplete. Sorry for the trouble!

This post has 43 responses

Posted in Client side 10 months ago

Sending email with Symfony 1.2 and Swift 4

If you had used Symfony 1.2 with Swift 3 before, you probably sent emails like this:

try
{
  $mailer = new Swift(new Swift_Connection_NativeMail());
  $message = new Swift_Message('The subject', 'The body <b>html</b>', 'text/html');
  $mailer->send($message, 'to@user.com', 'noreply@company.com');
  $mailer->disconnect();
}
catch (Exception $e)
{
  $mailer->disconnect();
}

However, due to API changes in Swift 4, that would have thrown an error:

Fatal error: Cannot instantiate abstract class Swift in /[...]/apps/frontend/modules/[...]/actions/actions.class.php on line 40

It turns out that now the class Swift is defined as abstract, which means it can’t be instantiated. Now, classes that you used to instantiate directly, like Swift_Message, have been added factory methods called newInstance

require_once('lib/vendor/swift/swift_init.php'); # needed due to symfony autoloader
$mailer = Swift_Mailer::newInstance(Swift_MailTransport::newInstance());
$message = Swift_Message::newInstance('The subject')
         ->setFrom(array('noreply@company.com' => 'Mailer Name'))
         ->setTo(array('email@email.com' => 'Name Lastname'))
         ->setBody('The body <b>html</b>', 'text/html');  
$mailer->send($message);

Remember that it’s good practise to get the body HTML from a partial, instead of hardcoding it into the action.

$mailBody = $this->getPartial('emailPartial', array(/* parameters */));
// ... 
->setBody($mailBody, 'text/html');

The code has become a lot more readable, easier to customize and write.
More information here and here.

This post has 8 responses

Posted in Server side 10 months ago

An IE6 post

Addressing an old subject again, misaddressed by many.

No matter how much we all hate IE6, we never seem to agree on what’s the best way to finally get rid of it. Web designers and developers alike have realized that investing too much time and effort in fixing its quirks is not viable from a business perspective, but they still want to reach that audience.

This ambivalence is what still drives people, like myself, to keep writing about the infamous browser.

Continue reading

This post has 21 responses

Posted in Client side 10 months ago

BarackSlideshow 0.3

Due to popular request, I’ve made some changes to BarackSlideshow:

  • New project page with documentation and how to use explanation
  • It includes the upcoming, updated and optimized Fx.MorphList
  • The download package has been fixed for a very easy deployment.

This post has 22 responses

Posted in Projects 10 months ago