2

I'm doing an autocomplete input form and i want to send what my user types to a remote database for suggestions.

I use dnode for it now, and i do not want to do a new remote connect each time my user types so i made the remote function global like this

dnode.connect(5050, function (remote) {
    window.remote = remote

});

So each time i want to check my mongodb i just use the window.remote.function and dont have to reconnect. Are there a better way?

Thanks

Ludvig
  • 637
  • 6
  • 18

2 Answers2

2

I suggest using Socket.IO directly for this, which is actually used by DNode under the hood for exchanging information between the server and the browser. Find more information about Socket.io on the following sites:

alessioalex
  • 62,577
  • 16
  • 155
  • 122
1

Bind your autocomplete listeners inside of the scope of the dnode connection instead of exposing the connection to the outside.

Instead of doing:

dnode.connect(5050, function (remote) {
    window.remote = remote
});    
autoCompleteLogic(window.remote)

do this instead:

dnode.connect(5050, function (remote) {
    autoCompleteLogic(remote)
});
substack
  • 3,346
  • 24
  • 14