How to store data in sessionStorage?

370 Views Asked by At

I want to store the a string in the sessionStorage inside the chrome dev tools. I specifically want to store the socket.id from socket.io inside the sessionStorage in order to use the same socket.id in my multi-paged web app; is this possible?

1

There are 1 best solutions below

1
Ragavan Rajan On

You can store anything in sessionStorage. Session Storage acts like a localstorage. The only difference is the session storage will disapper if you close the tab or browser.

Example code:

public setSession(authResult): void {
    // Set the time that the access token will expire at
    const expiresAt = JSON.stringify((authResult.expiresIn * 1000) + new Date().getTime());
    sessionStorage.setItem('access_token', authResult.accessToken);
    sessionStorage.setItem('socketId', socket.id); // Save your socket id here 
    sessionStorage.setItem('expires_at', expiresAt);
  }

Pass the socket in constructor and access it in any of your method like above (setSession)