New websocket on different instance of site

I have a remote control for 4 Devices running on a node server.

Each device has its own page (/1, /2, /3, /4), but they are all generated from the same html/js.

the only difference is the ip for each device, loaded from a json on the server depending on the url path.

This all works, but the problem is: i have 3 obviously wrong IPs entered for testing purposes, and one correct one. Now if i open the correct one, go back to the parent page and open the page of a device with a wrong IP, it is still shown as online and can be controlled.

I understand this like: the socket stays open across the pages and is actually not built new on every site.

how can i make sure that each subpage generates a new socket?

right now, i just have

socket = new io.connect();

in the browser.js,

ioServer.on('connection', function (socket) {
    //etc.
}

in the app.js and it works for ONE device.

Am I right to assume that I need some kind of "destroy socket if page is changed"-function?

Thanks for the help

By the looks of it, you only want to instantiate and boot a device when the device's client connects for the first time and emits 'startup'. If this is not the case, I'd probably instantiate and boot each device when the server starts.

Going with the first case, I'd store your devices in a key-value object and reuse them when needed.

var devices = {};

ioServer.on('connection', function (socket) {

    var client_ip_address = socket.request.connection.remoteAddress;
    console.log("New Connection from: " + client_ip_address);

    var device;

    socket.on('startup', function (data) {

        var deviceId = data.device;

        console.log("[INFO] startup: device " + deviceId);

        //If this device is already in devices, resuse it    
        if (deviceId in devices) {

            //Get the device from your devices object
            device = devices[deviceId];

        //Otherwise, create a new device and store it in devices
        } else {

            device = new HyperDeck(deviceId);

            //Store new device in devices object
            devices[deviceId] = device;

            device.boot();
        }

        console.log(device.id);
        console.log(device.ip);
    });

    ...