SocketIO DAT emit method?

Up to which version of socket.io is supported by the new socketioDAT?
Are there any examples of how to use the emit() method? I’m unsure of how to properly format input parameters for it and am not clear from the reference docs.

Something like:

[code]data = {}
data[‘name’] = ‘host’
data[‘roomId’] = ‘tdHost’

op(‘socketio1’).emit(‘join’, data)[/code]

Yields:

Traceback (most recent call last): File "</project1/socketIO/text1:op('/project1/socketIO/text1').run()>", line 1 td.Error: File "/project1/socketIO/text1", line 5 td.Error: Invalid number or type of arguments. See help for details. Value:('join', {'name': 'host', 'roomId': 'tdHost'}) Type:<class 'tuple'>. Results of run operation resulted in exception. Value:type:textDAT path:/project1/socketIO/text1 Type:<class 'td.textDAT'>.

Any clarity would help, thanks!

The emit method takes the event name as a parameter, as well as an optional data keyword. It’s optional because not all events require any data to be sent.

You can see the method spec here: docs.derivative.ca/Experimental … oDAT_Class

In your example it looks like you haven’t properly set the data keyword.

op('socketio1').emit('join', data)

Should be:

op('socketio1').emit('join', data=data)

Thanks Eric. I had not been familiar with the way Python keywords work (versus default parameters in JavaScript and C++), and that small example is helpful.

Up to what version of socket.io is supported by the SocketIO DAT? For instance, the previous implementations of socket.io in TouchDesigner only seemed to support up to version 0.9.0, and the current version is 2.2.0. I don’t see version support listed in the reference docs but may be missing it.

EDIT:
I ask because I am no longer receiving any errors, but my server (socket.io=v2.2.0) is not registering any events besides an initial ‘connection’ event.

For instance, I tried:

op('socketio1').emit('test')

This same emit from other clients (e.g. JavaScript, Unreal Engine) registers as expected, and I’m certain my node.js server code is correct. The SocketIO DAT also does not seem to be receiving any events despite my server seeing the connection.

It should work with the most recent version of socket.io. For reference we are using socket.io’s C++ client API: github.com/socketio/socket.io-client-cpp

OK thanks. That’s the same implementation being used in Unreal Engine.

Turns out my server wasn’t actually seeing the ‘connection’ event after all, so I’ll have to figure out what is going wrong. If you could provide an example TOE with a properly set up SocketIO DAT (whatever you may have used for internal testing), I would appreciate it!

I successfully tested with the node.js chat example here: github.com/socketio/socket.io/t … mples/chat

Follow the steps to setup the chat server outlined there. With the server running then, you should be able to open your browser to localhost:3000 and be prompted to enter a name.

If you then open the attached toe file and run the script (highlighted in blue) the SocketIO DAT will be added to the same chat via the ‘add user’ event. You’ll also see an input table DAT, which is a list of all the events from the server that the SocketIO DAT is listening to.
SocketIOChatExample.toe (3.81 KB)

Thank you for providing this!

I have tried the example TOE on two separate machines with no luck. The node.js server runs fine, and I am able to connect to it from other clients using the same localhost:3000 URL.

The TOE you sent seems to be created on 2019.31010, and I am on Win64 2019.30790. I have checked my Windows Firewall settings and confirmed that TouchDesigner has full access. Is it possible this does not work on the currently available experimental build?

Ahh, yes, sorry about that. There is overly strict prefix filtering in 2019.30790. So if you don’t have “http://”, for example, then it won’t connect. To work around this for the time being you should change the URL to be “http://localhost:3000”.

Sorry, I should have specified in my previous message that I tried that on both machines, using the “http://” prefix after the default URL didn’t work. I am still unable to get the example TOE to connect or receive any messages.

Try making the node.js server HTTPS instead, with a self-signed certificate. Some details here:
nodejs.org/en/knowledge/HTTP/se … PS-server/

Then change the URL to “https://localhost:3000”.

Also, for debugging you can set the environment variable TOUCH_TEXT_CONSOLE to 1 to open the console when launching TD. The socketio API will output connection statuses/errors there.

Also disable Windows Firewall

I have already tried this to no effect.

I’ll create an HTTPS node.js server and let you know how that goes. That debugging tip is helpful too, thanks.

Hi Eric, I have turned on TOUCH_TEXT_CONSOLE and tried making the node.js server HTTPS per the instructions you included.

SocketIO DAT will connect to HTTPS server, but not with Verify Certificate (SSL) toggled on (this is probably because I’m using a self-signed certificate). I am able to receive messages as well.

SocketIO DAT will not connect to an HTTP server, however. The console simply prints [error] handle_transport_init received error: TLS handshake failed . Judging by this debug output, it seems it only looks for HTTPS connections by default. Is this an accurate assumption?

I’ll try adjusting the server for my project to use HTTPS and let you know if I run into any further issues. Thanks!

Yeah, that’s exactly right. If you had your certificate signed by a certificate authority then it should work when this parameter is toggled.

This is a result of us compiling the socket.io C++ client API with TLS enabled – it seems that it will always try to establish a secure connection with a TLS handshake. I think the solution here is for us to compile another version of the lib with TLS disabled and use the appropriate lib based on the connection requirements. In the meantime though, your server will need to be secure.

OK cool, thanks for the clarification. It would be great to have the ability to use both HTTP and HTTPS as its simpler to prototype with the former.

This will be fixed in the next experimental release. The connection requirement (secure/non-secure) will be determined implicitly from the URL prefix (eg. http:// for non-secure).

Hi all, bumping this thread. I’m connected and able to emit messages, but am having an issue with sending multiple values. The server I’m connecting to has some code like this:

server.on('event_name', function(arg1, arg2)) {
// do something with multiple input args
}

I’ve tried emitting those multiple args both as a tuple and a list, but neither seems to work:
dat.emit('event_name', data=('value1', 'value2'))
dat.emit('event_name', data=['value1', 'value2'])

In both cases, what the server receives is a string of ‘value1,value2’ for arg1, i.e. my two values concatenated with a comma, and arg2 is undefined. Is there a proper way to format this data? I realize that I can send a string of json data or handle this some other way, but I imagine actually sending multiple values should be supported, especially as I imagine a lot of server code is probably written this way.

Thanks!

1 Like

Looks like both tuples and lists are parsed as a single array message. However, tuples should be sent as separate arguments.

I’ve fixed tuples so that each item in the tuple will be sent as its own argument. This distinguishes it from a list, which will still be sent as an array message. This only applies to the outer tuple. If it’s a tuple of tuples, the inner tuples will also be sent as array messages (similar to a list).

These changes will be in the next experimental build, thanks for the report!

1 Like