Edit in JSFiddle

var socket = new WebSocket("ws://echo.websocket.org/"),
    pubsub = amplify,
    player = { name: "George Lucas" };

socket.onopen = function() {
    pubsub.publish("chat/open")
};

socket.onclose = function() {
    pubsub.publish("chat/close")
};
        
socket.onerror = function() {
    pubsub.publish("chat/error")
};
    
socket.onmessage = function(data) {
    pubsub.publish(data.Type + "/" + data.SubType, data);
};

pubsub.subscribe("chat/message/send", function(data) {
    socket.send(data);
});
    
pubsub.subscribe("chat/message/receive", function(data){
    console.log(data);        
});

pubsub.subscribe("chat/open", function() {
    console.log("Socket opened.");
    pubsub.publish("chat/message/send", "Hi, server!");
});

pubsub.subscribe("chat/message/normal", function(data){   
    console.log(data.From +":" + data.Message); 
});

pubsub.subscribe("chat/message/normal", function(data){ 
    if(data.From == player.Name){
        console.log("You were mentioned in a message!"); 
    } 
});

              
            
          
            
              
                

              
            
          
            
              

External resources loaded into this fiddle: