-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathws.provider.tsx
55 lines (48 loc) · 1.32 KB
/
ws.provider.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"use client";
import { WebSocketProvider } from "next-ws/client";
import { ReactNode } from "react";
import { env } from "~/env";
import { EmitEvent } from "~/lib/ws/events.ws";
/**
* Log to console when ws message validation fails
*/
function onInvalidWSMessage(payload: string, message?: string): null {
console.error(
`Invalid ws message payload${message ? ` ${message}` : ""} :`,
payload,
);
return null;
}
/**
* Validate that incoming ws message event's payload is in the format we want it be.
*
* @param e incoming message event
*/
export function validateWSMessage(e: MessageEvent<string>): null | EmitEvent {
try {
const { event, data } = JSON.parse(e.data);
if (!event || !data) {
return onInvalidWSMessage(
e.data,
"missing required keys 'event' || 'data'",
);
}
//TODO: zod validation?
return { event, data };
} catch (err) {
if (err instanceof SyntaxError) {
return onInvalidWSMessage(e.data, "invalid JSON");
}
return onInvalidWSMessage(e.data);
}
}
/**
* Provides a websocket instance to children for access anywhere within the app.
*/
export function WSProvider({ children }: { children: ReactNode }) {
return (
<WebSocketProvider url={`ws://localhost:${env.PORT}/api/ws`}>
<div>{children}</div>
</WebSocketProvider>
);
}