Kod pelayan.js sebelah pelayan
var express=require("express");
var http=require("http");
var sio=require("socket.io");
var app=express();
var server=http.createServer(app);
var fs=require("fs");
app.get("/", fungsi (req,res) {
res.sendfile(__dirname "/index.html");
});
server.listen(1337);
var socket=sio.listen(server);
socket.on("sambungan", fungsi (soket) {
socket.emit("berita",{hello:"Hello"});
socket.on("otherEvent", fungsi (data) {
console.log("Data diterima pelayan: %j",data);
})
});
Kod index.html pelanggan
var socket=io.connect();
socket.on("berita", fungsi (data) {
console.log(data.hello);
socket.emit("otherEvent",{my:"data"});
});