leolabs / ableton-js

Control Ableton Live with Node.js
MIT License
368 stars 46 forks source link

Jbflow/feature/tcp socket #119

Open jbflow opened 5 months ago

jbflow commented 5 months ago

I think I have removed all breaking changes so this should work as before. I have set it back to using the UDP socket with an optional variable you can change in the init file to have it run the TCP socket, and also implemented a socket interface and a base class for the main script so we can test and compare the two sockets and this should help to optimise the code without breaking anything else.

Confirmation that this method does work from postman below.

Screenshot 2024-05-21 at 18 30 36

What:

To test TCP socket

To do:

what-the-diff[bot] commented 5 months ago

PR Summary

The overall changes aim to improve the organization of code, increase efficiency, and make way for new, improved classes to handle TCP and UDP sockets.

leolabs commented 5 months ago

That's awesome, thank you so much for this PR and the work you put into making this work with WebSockets! Let's check if it would be possible to make this into a package that can be used on NodeJS, Deno, and in the browser.

There's a build tool called DNT that allows us to write TypeScript focused on the browser and that takes care of creating versions that can be used on NodeJS by, for example, shimming the WebSocket implementation automatically.

For the client side, something like PartySocket might be interesting to look into as it nicely automatically handles reconnects when the connection drops for some reason.

If you're happy with the Python script's side of the PR, we could merge it into a separate branch and I can take care of the TypeScript side. What do you think?

I'm also curious about the latency of WebSockets running in a separate thread compared to the current UDP approach of polling for data every few milliseconds. Have you done any tests to compare the two in that regard?

jbflow commented 5 months ago

No problem, it's great to find other devs working with the Live API. Thanks for sharing theses resources. 🎆

Not heard of Deno or DNT before so will have a read.

👀 PartySocket - as I am currently handling that functionality manually.

I have a few tweaks to make to the Python script that I have made in my own project so I will reflect those here, so yeah merging to a new branch sounds like a good plan, will push my changes over the next 24 hours for you to review.

Haven't looked at testing yet but it's something that needs doing. when I update this PR I will roll back some of the breaking changes so we can look at running both implementations side by side to compare.

jbflow commented 5 months ago

@leolabs PR ready for review, breaking changes and clutter removed and working from node as before or you can switch it to TCP to connect from a websocket. :)

leolabs commented 5 months ago

Thank you, I'll have a look at this now! I think once we get the WebSockets implementation working properly on the JS side, we can safely remove the UDP implementation and just release this as a major version with potential breaking changes to keep the code simple.

Regarding the port situation, I think you asked about it in one of the other threads, we could use an approach where the Python plugin chooses a free port out of a list of, say, 8 predefined ones (e.g. 39031, 39032, 39033, 39034, 39035, 39036, 39037, 39038). This should avoid conflicts where the OS is already using a port, and the client library can just try to connect to each port and if that isn't available, use the next one.

Usually, the first port in the list is free and can be used so this approach shouldn't have a huge performance impact for establishing a connection. What do you think?

jbflow commented 5 months ago

Awesome, the approach I've taken should allow for both to run side by side until we're confident in this approach, the limited testing I did last night it doesn't seem to have broken anything for running in Node.

With regards to ports, that was the approach I was thinking of taking, id agree that it's unlikely we're going to encounter another bound port, the most likely scenario is if live crashes or our own socket fails to close and unbind properly and then it fails to reconnect after starting because it's still open and bound to the previous instance, in which case we can just move to one of the next predefined ports.

Another note on this PR which I forgot leave in my comments, I started looking at chunking for large message sizes but had to remove it because there was an issue with framing, I'm handling this in my code by batching the messages, combined with the queue it seems to work ok, might also be possible to limit the queue size and have it send once it hits a certain size to keep the package size below the limit. What are your thoughts on this?

jbflow commented 5 months ago

I removed the chunking approach in commit dee9906

leolabs commented 4 months ago

Hey @jbflow, please excuse my super late reply on this! I haven't found the time to thoroughly test this PR yet, but will hopefully get to it next week.