Created 10-21-2019 07:36 AM
Dear NiFi enthousiasts,
I'm configuring a flow where I make a connection with several websockets. Each ws-url contains a unique authentication token obtained via a seperate http-request and is now manually added to the ws-url. Whenever the connection is lost for more than 20 min, my token becomes invalid and I have to manually request new tokes and update the JettyWebSocketClient for each of the websocket connections. this is time consuming and error-prone. To avoid this, I would like to automate this.
I was thinking I could store the token in an attribute and pass it to the controller but apparently you cannot pass flow-file attributes to a controller. And on top of that you have to disable before you can update.
So, the question is, how can I update the ws-url in the JettyWebSocketClient with new tokens?
In order to get the tokens, I need to do a http GET request with x-www-form-urlencoded headers, a signIn command, username and password. This returns a JSON with the token. I replaced the real token with "token".
{ "data": { "clientToken": "token" } }
websocket-url:
ws://{url:port}/api/v1/......X-AUTHENTICATION-TOKEN={token}
flow
Created 10-21-2019 08:02 AM
The JettyWebSocketClient NiFi controller does support NiFi EL (variable registry only) in the WebSocketURI property. This means that you can means you could use for example ${token} in the URL you set in this Controller service. Variables can be set on the process group.
So you have two option here and both involve using the NiFi Rest-API.
Both start with obtaining the new token (perhaps via a rest-api call using the invokeHTTP processor?
1. Use NiFi's rest-API to stop, modify, and start theJettyWebSocketClient
2. Use NiFi's rest-API to modify a NiFi variable set on a process group. Advantage here is the token variable can be used by multiple processors with the process group (or sub-process groups) where it was defined. Allowing you to make only one update.
So consider building a flow that upon failure you trigger an invokeHTTP to get a new token and then another invokeHTTP to update the variable via NiFi's rest_API. Then loop failed FlowFile back to processor that failed to try again.
When working with NiFi's rest-api, it is helps to use your browser's developer tools to capture the rest api calls as you manually perform them via the NiFi UI. NiFi's rest-api documentation is also available within the embedded docs of your NiFi installation (global menu --> Help).
Hope this helps,
Matt
Created 10-22-2019 02:16 AM
Hey Matt,
Thanks for your reply. This really helps a lot.
If this leads to a solution, I will post the results here and accept as solution.
Best,
Tine
Created on 05-01-2020 06:39 AM - edited 05-01-2020 06:41 AM
Dear @Tine,
I have exactly the same use case. Have you implemented the proposed solutions from above? I would really appreciate it if you could share your experience!
Thanks in advance!
Valentina