2

My objective is to design a small site with auto-completion of a field which would take suggestions from server's database (Node.js + MySQL).

What methods I could use to achieve this client-server communication as user types value into a field. I am looking for something greater than writing own AJAX requests, but easier than frameworks like Angular.

2 Answers 2

3

I use the same NodeJS + MySQL setup.

If you already have your app going, I suggest installing socket.io. Otherwise, look into Express.IO which is a framework for NodeJS development with Socket.IO integrated into it.

Websockets make AJAX look silly!

In NodeJS, you listen for io.emit("someEvent", {object-data-here}); on the client side, and route it on the server side with app.io.route("someEvent", function(request,response) {});

Don't copy and paste this, but firing a socket for a keyUp event is easy peasy.

var input = document.getElementById("inputfield");
input.addEventListener("keyUp", function() {
    io.emit("aKeyWasPressed", {value:this.value}); //on key up, send input value to Node
}, false); //false for bubbling event
Sign up to request clarification or add additional context in comments.

2 Comments

+1 for alternate solution but be cautious that dealing with websockets means more users having problems due to proxy/firewalls (and socket.io doesn't work well enough to circumvent proxy limitations). I do use autocompletion using socket.io but only when my app already use websockets.
@dystroy Yes, I have noticed socket.io not working behind a corporate firewall. Good news, is they are not my target audience. And in addition, I will learn a little bit more of socket.io. Thank you for the detailed answer, I am already using Express, thanks for mentioning Express.IO
2

take a look at jQuery $.ajax() method. http://api.jquery.com/jquery.ajax/

You can use the jquery-ui autocomplete for your autocomplete functionality. http://jqueryui.com/autocomplete/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.