Where to storage a websocket connection?

My question is more about an alternative to store a ws connection. Initially I thought about storing in the session or localStorage, but ws objects are not supported, only primitive data types.

I am wondering what would be the alternatives?

What I am trying to achieve is once I reload the page, I don’t lose the ws connection because I have a event sending ws.send(), and after reloading the page it becomes undefined.

Code:

const startConn = () => {
        const ws = new WebSocket('wss://4z2ysa3nod.execute-api.ap-southeast-2.amazonaws.com/Test/'); // TODO: use ENV variable
        setSocket(ws);

        ws.onopen = () => {
            console.log('[onopen] WebSocket connected');
            setStreaming(true);
            console.log("ws", ws);
            ws.send(JSON.stringify({
                action: 'start-streaming-data',
                data: configuration
            }));
        };

        ws.onmessage = (event) => {
            // i am storing data here, removed because not relevant
        })

            setPlotData(prevData => {
                const dataArray = Array.isArray(prevData) ? prevData : [];
                const newData = [
                    ...dataArray,
                    { time: new Date().toLocaleTimeString(), data: data.graph }
                ];
                // Limit the number of data points to preserve interval
                if (newData.length > 10) {
                    newData.shift();
                }
            
                sessionStorage.setItem('plotData', JSON.stringify(newData));
                return newData; // return new array to trigger re-render
            });
        };

        ws.onclose = () => {
            setStreaming(false);
            // clearInterval(intervalId);
            console.log('[onclose] WebSocket disconnected:');
        };

        return () => {
            ws.close();
        };
    }

    useEffect(() => {
        // TODO: simulating receiving data from sensor
        let intervalId;

        if (streaming) {
            intervalId = setInterval(() => {
                if (socket !== null) {
                    socket.send(JSON.stringify({
                        action: 'start-streaming-data',
                        data: configuration
                    }));
                } else {
                    console.log("[wsSend] socket is null");
                }
            }, 1000);
        } else {
            clearInterval(intervalId);
        }

        return () => clearInterval(intervalId);
    }, [streaming, configuration]);

1

A websocket connection cannot be saved across page reloads by storing it somewhere. Trying to put it in localStorage, sessionStorage or in a database is pointless and doesn’t work.

You could either prevent page reloads from actually happening – the simplest solution.

Or – allowing page reload – you could keep the connection alive in a place different from your page’s environment, e.g. as deceze’s comment suggests in a Shared Web Worker.

Both solutions for keeping the socket connection alive might be overkill though. The most common tactic is to allow the connection to break and just re-establish it after page reload. If you face weird problems/errors on client or server side after implementing this, try gracefully closing the connection.

Why is your ws variable undefined after reload?

The page reload wipes the JavaScript environment clean (among other actions like building a new DOM). More precisely, the page’s JavaScript Execution Context gets replaced by a brand new one. That’s why the old variables, functions, objects, etc. are all gone.

To learn more about where JavaScript is or can be executed, see the 4-parts Chrome Developer Blog series “Inside look on modern web browser”.

0

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật