Refactor using socket.io