Last active
August 29, 2015 14:02
-
-
Save FWeinb/75e169dbd0886adf7fec to your computer and use it in GitHub Desktop.
Preview of socket.io Web Components using poylmer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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