Skip to content

Commit

Permalink
Load socket io library appending script. Add ability event socket lis…
Browse files Browse the repository at this point in the history
…tener
  • Loading branch information
javierbrea committed Feb 28, 2019
1 parent d4d3bcd commit d6e52aa
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 8 deletions.
1 change: 0 additions & 1 deletion public/index.html
Expand Up @@ -12,7 +12,6 @@

<body>
<div id="root"></div>
<script src="http://localhost:3100/socket.io/socket.io.js"></script>
<noscript>
You need to enable JavaScript to run this app.
</noscript>
Expand Down
5 changes: 5 additions & 0 deletions src/data-layer/services/ability/origins.js
@@ -1,6 +1,7 @@
import { origins } from "reactive-data-source";

import { authConfig } from "../../setup";
import { socket } from "../../socket";

import { byIdFilter } from "../../helpers";

Expand Down Expand Up @@ -46,3 +47,7 @@ export const abilityActions = new origins.Api(
abilityActions.addCustomFilter({
byId: byIdFilter
});

socket.addListener("ability:event", eventData => {
abilityStates.byId(eventData._id).clean();
});
20 changes: 13 additions & 7 deletions src/data-layer/socket.js
Expand Up @@ -95,14 +95,20 @@ class Socket {
}

setup(url) {
this._socket = window && window.io && window.io(url);
if (this._socket) {
this._handleDataSources();
this._addAuthListeners();
this._getCurrentAuth();
} else {
console.error("Sockets are not available");
const script = document.createElement("script");
script.type = "text/javascript"
script.onload = () => {
this._socket = window && window.io && window.io(url);
if (this._socket) {
this._handleDataSources();
this._addAuthListeners();
this._getCurrentAuth();
} else {
console.error("Sockets are not available");
}
}
script.src = `${url}/socket.io/socket.io.js`;
document.head.appendChild(script);
}
}

Expand Down

0 comments on commit d6e52aa

Please sign in to comment.