blob: cf034f7aefb8d396806431b3ddda2537c6f43b3d [file] [log] [blame]
Yoshie Muranakace9a3ef2020-05-06 14:33:22 -07001import { debounce } from 'lodash';
2
Yoshie Muranakadc04feb2019-12-04 08:41:22 -08003/**
4 * WebSocketPlugin will allow us to get new data from the server
5 * without having to poll for changes on the frontend.
6 *
Yoshie Muranaka1ace1d92020-02-06 13:47:28 -08007 * This plugin is subscribed to host state property and logging
8 * changes, indicated in the app header Health and Power status.
Yoshie Muranakadc04feb2019-12-04 08:41:22 -08009 *
10 * https://github.com/openbmc/docs/blob/b41aff0fabe137cdb0cfff584b5fe4a41c0c8e77/rest-api.md#event-subscription-protocol
11 */
12const WebSocketPlugin = store => {
13 let ws;
14 const data = {
Yoshie Muranaka1ace1d92020-02-06 13:47:28 -080015 paths: ['/xyz/openbmc_project/state/host0', '/xyz/openbmc_project/logging'],
16 interfaces: [
17 'xyz.openbmc_project.State.Host',
18 'xyz.openbmc_project.Logging.Entry'
19 ]
Yoshie Muranakadc04feb2019-12-04 08:41:22 -080020 };
21
22 const initWebSocket = () => {
Mateusz Gapski33a8c532020-07-24 08:52:53 +020023 const socketDisabled =
24 process.env.VUE_APP_SUBSCRIBE_SOCKET_DISABLED === 'true' ? true : false;
25 if (socketDisabled) return;
Yoshie Muranaka23f227d2020-05-01 14:19:43 -070026 const token = store.getters['authentication/token'];
27 ws = new WebSocket(`wss://${window.location.host}/subscribe`, [token]);
Yoshie Muranakadc04feb2019-12-04 08:41:22 -080028 ws.onopen = () => {
29 ws.send(JSON.stringify(data));
30 };
Mateusz Gapski33a8c532020-07-24 08:52:53 +020031 ws.on('error', function(err) {
32 console.error('error!');
33 console.error(err.code);
34 });
Yoshie Muranakadc04feb2019-12-04 08:41:22 -080035 ws.onerror = event => {
36 console.error(event);
37 };
Yoshie Muranakace9a3ef2020-05-06 14:33:22 -070038 ws.onmessage = debounce(event => {
Yoshie Muranaka1ace1d92020-02-06 13:47:28 -080039 const data = JSON.parse(event.data);
40 const eventInterface = data.interface;
Yoshie Muranakace9a3ef2020-05-06 14:33:22 -070041 const path = data.path;
Yoshie Muranaka1ace1d92020-02-06 13:47:28 -080042
43 if (eventInterface === 'xyz.openbmc_project.State.Host') {
44 const { properties: { CurrentHostState } = {} } = data;
45 store.commit('global/setHostStatus', CurrentHostState);
Yoshie Muranakace9a3ef2020-05-06 14:33:22 -070046 } else if (path === '/xyz/openbmc_project/logging') {
47 store.dispatch('eventLog/getEventLogData');
Yoshie Muranaka1ace1d92020-02-06 13:47:28 -080048 }
Yoshie Muranakace9a3ef2020-05-06 14:33:22 -070049 // 2.5 sec debounce to avoid making multiple consecutive
50 // GET requests since log related server messages seem to
51 // come in clusters
52 }, 2500);
Yoshie Muranakadc04feb2019-12-04 08:41:22 -080053 };
54
55 store.subscribe(({ type }) => {
56 if (type === 'authentication/authSuccess') {
57 initWebSocket();
58 }
59 if (type === 'authentication/logout') {
60 if (ws) ws.close();
61 }
62 });
63
64 if (store.getters['authentication/isLoggedIn']) initWebSocket();
65};
66
67export default WebSocketPlugin;