1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
6 * This js establishes a websockets connection with the API Server.
9 /* Subscribe to websockets event log. Do nothing if already connected. */
10 function subscribeToEventLog () {
11 // if websockets are not supported by browser, do not subscribe for events
12 websocketsSupported = ('WebSocket' in window);
13 if (websocketsSupported == false) {
17 // check if websocket connection is already stored on the window
18 event_log_disp = $(window).data("arv-websocket");
19 if (event_log_disp == null) {
20 // need to create new websocket and event log dispatcher
21 websocket_url = $('meta[name=arv-websocket-url]').attr("content");
22 if (websocket_url == null)
25 event_log_disp = new WebSocket(websocket_url);
27 event_log_disp.onopen = onEventLogDispatcherOpen;
28 event_log_disp.onmessage = onEventLogDispatcherMessage;
30 // store websocket in window to allow reuse when multiple divs subscribe for events
31 $(window).data("arv-websocket", event_log_disp);
35 /* Send subscribe message to the websockets server. Without any filters
36 arguments, this subscribes to all events */
37 function onEventLogDispatcherOpen(event) {
38 this.send('{"method":"subscribe"}');
41 /* Trigger event for all applicable elements waiting for this event */
42 function onEventLogDispatcherMessage(event) {
43 parsedData = JSON.parse(event.data);
44 object_uuid = parsedData.object_uuid;
50 // if there are any listeners for this object uuid or "all", trigger the event
51 matches = ".arv-log-event-listener[data-object-uuid=\"" + object_uuid + "\"],.arv-log-event-listener[data-object-uuids~=\"" + object_uuid + "\"],.arv-log-event-listener[data-object-uuid=\"all\"],.arv-log-event-listener[data-object-kind=\"" + parsedData.object_kind + "\"]";
52 $(matches).trigger('arv-log-event', parsedData);
55 /* Automatically connect if there are any elements on the page that want to
56 receive event log events. */
57 $(document).on('ajax:complete ready', function() {
58 var a = $('.arv-log-event-listener');
60 subscribeToEventLog();