How to search for a device on an internal network using Javascript and/or Node js

Asked

Viewed 772 times

5

I’m creating an application that uses Websocket to connect two devices to an internal network, but for that to be done, it is necessary for the user to enter the address of the Node js server in a text field for it to be used to create a Websocket connection between the HTML5 application and the Node js Server.

But if it were possible, I would like to eliminate the need to type the address to create this connection, I thought it was impossible at first, in fact I didn’t even consider it at first until I got in touch with Apple’s Keynote application, in this application it is possible to connect two devices on the same network so you can control the slide show with your iPad while your Mac designs the presentation, for example.

In Keynote, to create this connection it is only necessary that the two devices are connected on the same network and using the same application at the same time, without the need to enter any IP, This intrigued me and gave me some hope that the same could be done with Javascript and Node js. I know they possibly use some technology or standard that I don’t have access to, but I’d like to at least know how this was done. Theories that might work theoretically are also welcome.

  • If there is no answer in two days, I will put a reward here.

2 answers

3

In Node JS, it should not be so difficult; if the idea is simply to eliminate the need for the user to deal with IP numbers, it is enough that there is a Node JS server that can access and return the recently connected IP numbers to the users. Obviously, this requires a server computer on the network (INSIDE it), and each client would still have to know a single indispensable IP number: that of this central machine.

In Javascript, things are not so sure. The JS suffers from severe restrictions, mainly for safety reasons, but there is already discussion of a API for network discovery. Until something tangible emerges from there, I recommend you take a look at the project mdns-browser to see what can be harnessed in a simple web project, or what you could change in your project to take advantage of Chrome’s features.

  • How much to the server on the network and need to know its IP: I think each client can make one broadcast when being initialized, asking if there is active server. The server, when capturing the request, can send a direct response to the client informing his IP. This eliminates the need to know the server IP beforehand.

  • So@Renan, as far as I know, the difficulty lies precisely in do a broadcast; but if there is a way to perform this task directly on the client, then it really gives to implement the improvement you said - or else to go straight to the automatic discovery of too clients, as originally planned.

  • 1

    I understand, I hadn’t thought about it. Alternatively the server can broadcast every 'n' second, I think it gets easier.

  • Super right. I had in mind that it was a service in the "chat" style, in which only participates who had intention to participate; it may not be the case. In this way, we can take advantage of the server to create Broadcasts in a regular time interval, or even on demand from the customer. Thus, JS clients running the application could respond 'passively' to Broadcasts to confirm that they are able to receive messages via socket from other hosts.

  • I think I understand your proposal, the problem is that I would like to be able to deal with possibilities in which the server’s IP is not known so that anyone can use in their internal network just by running the Nodejs server. What I want to build (and maybe I should have said from the beginning) would be an application to display slides built in HTML5 and that could be controlled at a distance as in the Keynote quoted above. I will search better on the API for network discovery and maybe if there was a way to secure an internal IP through the nodejs itself exclusive for this purpose...

0

The automatic discovery of network services you experienced in Keynote is probably implemented by Bonjour (mDNS). It is an open-source Apple project and has add-ons in several languages. For Node.js you will find in node_mdns: npm install mdns.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.