Skip to main content

User Session

This package contains utilities to work with a user's session.

Broadcasting Session Information

The current user's session ID can be broadcast using sendNewUserSessionGuid and listened for using channel which is an instance of BroadcastChannel.

import { getAuthContext } from '@rhapsody/auth-context'
import { channel, sendNewUserSessionGuid } from '@shared/user-session'

// `channel` is potentially undefined if the browser does not support `BroadcastChannel`
channel?.addEventListener('message', (message) => {
console.log(message.userGuid)
})

const { user } = getAuthContext()
sendNewUserSessionGuid(user.guid)

Session Keep Alive

User activity can be listened for an a keep alive request be sent to Melody using keepSessionAliveOnActivity.

import { keepSessionAliveOnActivity } from '@shared/user-session'

const FIVE_MINUTES = 5 * 60 * 1000
keepSessionAliveOnActivity(FIVE_MINUTES)

NOTE: This function noops if the session_expiration feature flag is not enabled.