Skip to content

Instantly share code, notes, and snippets.

@totetmatt
Created November 3, 2015 12:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save totetmatt/49d2c5dd243918068320 to your computer and use it in GitHub Desktop.
Save totetmatt/49d2c5dd243918068320 to your computer and use it in GitHub Desktop.
Small demo of Graph Streaming in Gephi from a Html page
<html>
<head>
<script type="text/javascript">
window.onload = function () {
// Gephi Streaming Master should be launch in your computer
var websocket = new WebSocket("ws://localhost:8080/workspace0?action=updateGraph")
websocket.onopen = function (event) {
// Sending event following the API and message structure defined
websocket.send('{"an":{"a":{"label":"a"}}}')
websocket.send('{"an":{"b":{"label":"b"}}}')
websocket.send('{"ae":{"ab":{"source":"a","target":"b"}}}')
randomGenerate()
};
// A Quick & Dirty example to see the "real-time" graph
function randomGenerate(){
setTimeout(function(){
var test = (Math.floor( Math.random() * 50 ) + 1)%2 ;
for (i = 0; i < 10; i++) {
var id = Math.floor( Math.random() * 50 ) + 1 ;
websocket.send('{"an":{"'+id+'":{"label":"'+id+'"}}}')
}
for (i = 0; i < 10; i++) {
var source = Math.floor( Math.random() * 50 ) + 1 ;
var target = Math.floor( Math.random() * 50 ) + 1 ;
if(test===0) {
websocket.send('{"ae":{"'+source+'-'+target+'":{"source":"'+source+'","target":"'+target+'"}}}')
} else {
websocket.send('{"de":{"'+source+'-'+target+'":{"source":"'+source+'","target":"'+target+'"}}}')
}
}
randomGenerate()
}, 2000)
}
// Triggered when we received a message
// Here Gephi is actually propagating any changes to all the clients (including yourself)
// It can be usefull but can also be ignored
/*
websocket.onmessage = function(message){
console.log(message)
}
*/
}
</script>
<title>Websocket Gephi</title>
</head>
<body>
Run Gephi and Gephi Streaming Master, then refresh this page.
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment