Skip to content

Instantly share code, notes, and snippets.

@FWeinb
Last active August 29, 2015 14:02
Show Gist options
  • Save FWeinb/75e169dbd0886adf7fec to your computer and use it in GitHub Desktop.
Save FWeinb/75e169dbd0886adf7fec to your computer and use it in GitHub Desktop.
Preview of socket.io Web Components using poylmer
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="socket-connection.html">
<link rel="import" href="socket-emitter.html">
<link rel="import" href="socket-receiver.html">
<polymer-element name="server-time">
<template>
<h6>Server time: {{currentTime | formatTime }}</h6>
<!-- connect to the /time namespace on the current origin -->
<socket-connection url="/time">
<!-- add a reciver to listen to `current time` event and store the result in `data` -->
<socket-receiver event="current time" data="{{currentTime}}"></socket-receiver>
</socket-connection>
</template>
<script>
Polymer('server-time', {
formatTime : function(value){
return new Date(parseInt(value, 10));
}
});
</script>
</polymer-element>
var express = require('express');
var cron = require('cron');
var app = express();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var time = io
.of('/time')
.on('connection', function (socket) {
console.log('Connected to Time');
var emitTime = function(){
socket.emit('current time', +new Date());
};
emitTime();
cron.job("*/1 * * * * *", emitTime ).start();
});
http.listen(80, function(){
console.log('listening on *:80');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment