Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

draft: track web sockets like we track fetch #1613

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion playground/nextjs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"eslint": "^8.57.1",
"hls.js": "^1.5.15",
"next": "14.2.20",
"posthog-js": "1.200.1",
"posthog-js": "file:.yalc/posthog-js",
"react": "18.3.1",
"react-dom": "18.3.1",
"socket.io": "^4.8.1",
Expand Down
7 changes: 0 additions & 7 deletions playground/nextjs/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,6 @@ export default function App({ Component, pageProps }: AppProps) {
}
}, [])

useEffect(() => {
// make sure we initialize the WebSocket server
// we don't need to support IE11 here
// eslint-disable-next-line compat/compat
fetch('/api/socket')
}, [])

const localhostDomain = process.env.NEXT_PUBLIC_CROSSDOMAIN
? 'https://localhost:8000'
: process.env.NEXT_PUBLIC_POSTHOG_HOST
Expand Down
36 changes: 28 additions & 8 deletions playground/nextjs/pages/chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,28 @@ const Chat = () => {
const [socket, setSocket] = useState<Socket<DefaultEventsMap, DefaultEventsMap> | null>(null)

useEffect(() => {
// make sure we initialize the WebSocket server
// we don't need to support IE11 here
// eslint-disable-next-line compat/compat
fetch('/api/socket')

// Clean up the socket connection on unmount
return () => {
socket?.disconnect()
}
}, [])

const connect = () => {
// Create a socket connection
const createdSocket = io()

console.log('connecting', createdSocket)
// Listen for incoming messages
createdSocket.on('message', (message) => {
setMessages((prevMessages) => [...prevMessages, message])
})

setSocket(createdSocket)

// Clean up the socket connection on unmount
return () => {
createdSocket.disconnect()
}
}, [])
}

const sendMessage = () => {
if (!socket) {
Expand All @@ -39,6 +46,13 @@ const Chat = () => {

return (
<div className={'w-full min-h-96 flex-col space-y-2'}>
<button
disabled={!!socket}
onClick={connect}
className={'disabled:cursor-not-allowed bg-amber-400 disabled:bg-gray-600'}
>
Connect chat
</button>
<div className="flex flex-row justify-between items-center border border-gray-300 rounded p-2 space-x-2">
<input
className={'flex-1 border rounded px-2 py-1'}
Expand All @@ -47,7 +61,13 @@ const Chat = () => {
onChange={(e) => setCurrentMessage(e.target.value)}
/>

<button onClick={sendMessage}>Send</button>
<button
disabled={!socket}
onClick={sendMessage}
className={'disabled:cursor-not-allowed bg-amber-400 disabled:bg-gray-600'}
>
Send
</button>
</div>

<div className="flex flex-col border rounded px-2 py-1">
Expand Down
31 changes: 16 additions & 15 deletions playground/nextjs/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion playground/nextjs/src/posthog.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ if (typeof window !== 'undefined') {
person_profiles: PERSON_PROCESSING_MODE === 'never' ? 'identified_only' : PERSON_PROCESSING_MODE,
persistence_name: `${process.env.NEXT_PUBLIC_POSTHOG_KEY}_nextjs`,
opt_in_site_apps: true,
__preview_remote_config: true,
__preview_remote_config: false,
...configForConsent(),
})
// Help with debugging
Expand Down
24 changes: 24 additions & 0 deletions src/entrypoints/recorder-next.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { assignableWindow } from '../utils/globals'
import { getRecordConsolePlugin } from '@rrweb/rrweb-plugin-console-record'
import { record as rrwebRecord } from '@rrweb/record'
import { getRecordNetworkPlugin } from '../extensions/replay/external/network-recorder.plugin'
import { getRecordWebSocketPlugin } from '../extensions/replay/external/websocket-recorder.plugin'

assignableWindow.__PosthogExtensions__ = assignableWindow.__PosthogExtensions__ || {}
assignableWindow.__PosthogExtensions__.rrwebPlugins = {
getRecordConsolePlugin,
getRecordNetworkPlugin,
getRecordWebSocketPlugin,
}
assignableWindow.__PosthogExtensions__.rrweb = { record: rrwebRecord, version: 'v2' }

// we used to put all of these items directly on window, and now we put it on __PosthogExtensions__
// but that means that old clients which lazily load this extension are looking in the wrong place
// yuck,
// so we also put them directly on the window
// when 1.161.1 is the oldest version seen in production we can remove this
assignableWindow.rrweb = { record: rrwebRecord, version: 'v2' }
assignableWindow.rrwebConsoleRecord = { getRecordConsolePlugin }
assignableWindow.getRecordNetworkPlugin = getRecordNetworkPlugin

export default rrwebRecord
Loading
Loading