The ThingWorx EMS and SDK based applications follow a three step process when connecting to the Platform:
- Establish the physical websocket: The client opens a websocket to the Platform using the host and port that it has been configured to use. The websocket URL exposed at the Platform is /Thingworx/WS. TLS will be negotiated at this time as well.
- Authenticate: The client sends a AUTH message to the platform, containing either an App Key (recommended) or username/password. The AUTH message is part of the Thingworx AlwaysOn protocol. If the client attempts to send any other message before the AUTH, the server will disconnect it. The server will also disconnect the client if it does not receive an AUTH message within 15 seconds. This time is configurable in the WSCommunicationSubsystem Configuration tab and is named "Amount of time to wait for authentication message (secs)."
Once authenticated the SDK/EMS is able to interact with the Platform according to the permissions applied to its credentials. For the EMS, this means that any client making HTTP calls to its REST interface can access Platform functionality. For this reason, the EMS only listens for HTTP connections on localhost (this can be changed using the http_server.host setting in your config.json).
At this point, the client can make requests to the platform and interact with it, much like a HTTP client can interact with the Platform's REST interface. However, the Platform can still not direct requests to the edge. - Bind: A BIND message is another message type in the ThingWorx AlwaysOn protocol. A client can send a BIND message to the Platform containing one or more Thing names or identifiers. When the Platform receives the BIND message, it will associate those Things with the websocket it received the BIND message over. This will allow the Platform to send request messages to those Things, over the websocket. It will also update the isConnected and lastConnection time properties for the newly bound Things.
A client can also send an UNBIND request. This tells the Platform to remove the association between the Thing and the websocket. The Thing's isConnected property will then be updated to false.
For the EMS, edge applications can register using the /Thingworx/Things/LocalEms/Services/AddEdgeThing service (this is how the script resource registers Things). When a registration occurs, the EMS will send a BIND message to the Platform on behalf of that new resource. Edge applications can de-register (and have an UNBIND message sent) by calling /Thingworx/Things/LocalEms/RemoveEdgeThing.