Skip to content

Instantly share code, notes, and snippets.

@joshhunt
Last active August 29, 2017 07:45
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save joshhunt/ff0f549f6f5e89ba040a to your computer and use it in GitHub Desktop.
Save joshhunt/ff0f549f6f5e89ba040a to your computer and use it in GitHub Desktop.
Socket.io live reload for Apple TV TVML app
import 'babel-polyfill';
import firstView from 'views/first';
import * as liveReload from 'lib/liveReload';
App.onLaunch((launchOptions) => {
firstView();
liveReload.connect(launchOptions);
});
import io from 'socket.io-client';
import * as router from 'lib/router';
function resume({lastLocation}) {
if (!lastLocation) { return; }
router.goTo(lastLocation);
}
export function connect(launchOptions = {}) {
const socket = io(launchOptions.BASE_URL);
socket.on('connect', () => console.debug('Live reload: connected') );
socket.on('compile', () => console.debug('Live reload: compiling, prepare for reload') );
socket.on('live-reload', () => {
App.reload({when: 'now'}, {lastLocation: router.getLocation()});
});
if (launchOptions.reloadData) {
resume(launchOptions.reloadData || {});
}
}
io = require('socket.io')(server);
io.serveClient(false);
io.on('connection', (socket) => {
console.log('socket.io connection');
socket.on('event', (data) => {
console.log('socket.io data:', data);
});
socket.on('disconnect', () => {
console.log('socket.io disconnect');
});
});
config = {
module: {
loaders: [
{
test: /socket\.io\-client/,
// socket.io-client requires the window object, and navigator.userAgent to be present.
// use webpack to shim these into socket.io
loader: `imports?window=>{},navigator=>{userAgent: 'tvos'}`,
},
]
},
plugins: [
function() {
this.plugin('compile', function() {
io.emit('compile');
});
this.plugin('done', function() {
io.emit('live-reload');
});
},
],
}
@jedi4ever
Copy link

@josh ; thanks for the gist!

socket.io
I'm a bit struggling to get the socket.io-client working.

I think I successfully loaded it with the import, but once I start using it I'm hitting more 'unknown' elements:

  • location seems to be missing
  • document seems to be missing

did you experience the same issues? or where the window & navigator the only shims needed

@jedi4ever
Copy link

nevermind , it seems I was hitting my web-pack-dev-server and that one does not have a socket.io client.
I changed the port and all is well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment