Questions about opening (and closing) a connection

Supporting Browser-based Javascript and Node.js
Post Reply
DrSiemer
Fresh meat
Posts: 2
Joined: Thu Apr 15, 2021 6:19 am
Contact:

Questions about opening (and closing) a connection

Post by DrSiemer »

Hi!

Phidget beginner here, hopefully someone can help me with a few questions that I have not yet found an answer to:

First of all, what does the "5000" in “ch.open(5000)” mean? I assume that's a timeout? This is never explained in the tutorial.

Another thing that has puzzled me: why is it important to close the connection? We will use phidgets to control some simple servos and LEDs, but as long as the session is running I don't see a reason to ever close a connection... Does that have any disadvantages I am not aware of?

Is it possible to check if a connection is already open? The examples I used always do this:

Code: Select all

ch.open(5000).then(function() { ch.setDutyCycle(1); });
But I can imagine something like this would also be possible if we can keep all connections open:

Code: Select all

if (ch.isOpen) { ch.setDutyCycle(1); });
And finally, is it possible to close any open connections or overtake those connections after a complete refresh of the code? Our app runs on node and to make sure all of the data is properly reset we simply reset the server. But that does mean we will lose all connection variables as well. If this is not possible that's ok; I will simply close all open connections before I reboot the server.
User avatar
mparadis
Site Admin
Posts: 660
Joined: Fri Oct 28, 2011 12:17 pm
Contact:

Re: Questions about opening (and closing) a connection

Post by mparadis »

1) The "5000" in open is indeed the timeout in milliseconds. This means your program will attempt to open ch for five seconds, and then give up and throw a "timeout" error event. You can read more in the "Phidget API" section of our API documentation (scroll down to the second "open()" section).

2) You're correct that some programs don't need to bother with closing the channel because the channel will naturally close if your program ends. Here are some reasons why you might want to make use of close():
- You want to free up the channel for another program or thread to use, especially if this program needs to keep running for a long time
- You have some code in the "onDetach" event that you want to run before ending your program (once close() completes, the channel will throw a detach event)
But generally most programs don't need to use it at all.

3) Yes, there is a way to check if a channel is open. The function getAttached() will return 'true' after open has successfully completed. You can read more about this in the same place in the API documentation linked above.

4) There is no built-in command to close all open connections. They will all close when the code terminates, or you can keep track of all of your connections and call close on each one if you need to do it while the code is still running.
DrSiemer
Fresh meat
Posts: 2
Joined: Thu Apr 15, 2021 6:19 am
Contact:

Re: Questions about opening (and closing) a connection

Post by DrSiemer »

Excellent, thank you for the quick response!
Looks like I was on the right track and I can just continue with what I was doing :D
Post Reply

Who is online

Users browsing this forum: No registered users and 9 guests