From 6e6d874f3aa75a8d1fc25a79617a658a2c6773d7 Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Fri, 2 Mar 2018 00:14:40 +0100
Subject: [PATCH] Websocket opening logic

---
 front/src/App.vue | 24 ++++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/front/src/App.vue b/front/src/App.vue
index d60f82febe..b26959fe70 100644
--- a/front/src/App.vue
+++ b/front/src/App.vue
@@ -33,6 +33,9 @@
 </template>
 
 <script>
+import { WebSocketBridge } from 'django-channels'
+
+import logger from '@/logging'
 import Sidebar from '@/components/Sidebar'
 import Raven from '@/components/Raven'
 
@@ -44,10 +47,31 @@ export default {
   },
   created () {
     this.$store.dispatch('instance/fetchSettings')
+    this.openWebsocket()
+    let self = this
     setInterval(() => {
       // used to redraw ago dates every minute
       self.$store.commit('ui/computeLastDate')
     }, 1000 * 60)
+  },
+  methods: {
+    openWebsocket () {
+      let self = this
+      let token = this.$store.state.auth.token
+      // let token = 'test'
+      const bridge = new WebSocketBridge()
+      bridge.connect(
+        `/api/v1/instance/activity?token=${token}`,
+        null,
+        {reconnectInterval: 5000})
+      bridge.listen(function (event) {
+        logger.default.info('Received timeline update', event)
+        self.$store.commit('instance/event', event)
+      })
+      bridge.socket.addEventListener('open', function () {
+        console.log('Connected to WebSocket')
+      })
+    }
   }
 }
 </script>
-- 
GitLab