How do i make multiple Dnode connections?

402 Views Asked by At

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

2

There are 2 best solutions below

1
On BEST ANSWER

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)
});
0
On

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: