chef 3745 Posted March 8, 2020 Share Posted March 8, 2020 (edited) Hi, When utilizing an ApiClient object in javascript, I would like to set a callback function for the onMessageReceived (or... 'onmessage') event for the websocket connection. I have been able to open the websocket and have also been successful in checking it's status, however I can't quiet figure out the event listener part. This throws no errors when it loads, but I'm unsure if it's correct? I add the event Listener to the websocket. ApiClient._webSocket.addEventListener('onmessage', (msg) => { console.log('My Apps websocket message: ' + msg); }); Thank you Edited March 8, 2020 by chef Link to comment Share on other sites More sharing options...
rechigo 293 Posted March 8, 2020 Share Posted March 8, 2020 (edited) Looking at the API Client code, you should do it like this: ApiClient._webSocket.onmessage = () => console.log("JHOOOASF"); Let me know how it works (tested it myself and it worked without issue) Edited March 8, 2020 by rechigo 1 Link to comment Share on other sites More sharing options...
Luke 37029 Posted March 8, 2020 Share Posted March 8, 2020 Looking at the API Client code, you should do it like this: ApiClient._webSocket.onmessage = () => console.log("JHOOOASF"); Let me know how it works (tested it myself and it worked without issue) That's not good. Not only will it blow away the core app's message listeners, but you'll have to keep setting it again everytime the connection is closed and re-opened. 1 Link to comment Share on other sites More sharing options...
Luke 37029 Posted March 8, 2020 Share Posted March 8, 2020 Hi, When utilizing an ApiClient object in javascript, I would like to set a callback function for the onMessageReceived (or... 'onmessage') event for the websocket connection. I have been able to open the websocket and have also been successful in checking it's status, however I can't quiet figure out the event listener part. This throws no errors when it loads, but I'm unsure if it's correct? I add the event Listener to the websocket. ApiClient._webSocket.addEventListener('onmessage', (e) => { console.log('My Apps websocket message: ' + msg); }); Thank you Better, but will only last as long as the connection lasts, and will have to be re-applied whenever it is closed and re-opened. 1 Link to comment Share on other sites More sharing options...
chef 3745 Posted March 8, 2020 Author Share Posted March 8, 2020 Do you mean calling 'onmessage' with the annonamous arrow function? Or listening in on Websocket messages with 'addEventListener', or both? Thanks. Link to comment Share on other sites More sharing options...
chef 3745 Posted March 8, 2020 Author Share Posted March 8, 2020 (edited) Thanks, you answered when I was typing my question. So check the websocket is open. I suppose there should be a regular check of the state of the websocket then. Edited March 8, 2020 by chef Link to comment Share on other sites More sharing options...
chef 3745 Posted March 8, 2020 Author Share Posted March 8, 2020 (edited) My goal was to get some more feed back from a scheduled task. By reading the web socket info I can get grab task data in the message and look to see if my task is running. Then I suppose some kind of work arounds could be made to give the feed back I looking to get. But I'm wondering if maybe the scheduled tasks themselves could maybe have an extra parameter added to access this data in the core. We can use progress to get data into the dashboard. But some of my tasks run a long time and they could run consecutively. Currently I don't have any way to now which file they are working on without going into the system files to locate a marker file, or opening a plugin page and wait for it to update a list. The ability to add a browser notification would be amazing. Something that could show me some extra info on what the task is taking care of at that moment, either on start or finish. In order to do this currently it involves some high level JavaScript added to my own version of the dashboard-ui, which doesn't really work for everyone else. Edited March 8, 2020 by chef Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now