Skip to content

Commit

Permalink
fix: to fix (#289)
Browse files Browse the repository at this point in the history
* fix: to fix

* fix: some fix

* test: console.log websocket payload
  • Loading branch information
userMeh authored Jul 23, 2024
1 parent 9da04bb commit d1f8ca2
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 41 deletions.
28 changes: 1 addition & 27 deletions apps/api/src/handlers/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,7 @@ export const messages = new OpenAPIHono<{ Variables: Variables }>({
supabase
.channel('realtime_messages')
.on('postgres_changes', { event: '*', schema: 'public', table: 'MESSAGES' }, async (payload) => {
const dataMessage = {
new: {
...payload.new,
name: '',
},
old: {
...payload.old,
name: '',
},
event: payload.eventType,
};
if (payload.eventType !== 'DELETE') {
const { data, error } = await supabase
.from('USERS')
.select('username')
.eq('id', (payload.new as { id_sender: number }).id_sender)
.single();

if (error || !data) {
console.error(error);
return;
}

dataMessage.new.name = data.username;
}

io.emit('receivedMessage', dataMessage);
io.emit('receivedMessage', payload.new);
})
.subscribe();

Expand Down
6 changes: 4 additions & 2 deletions apps/client/app/(auth)/(members)/members/chat/page.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
'use client';
import type { Message, SocketMessage } from '@/app/lib/type/Messages';
import type { Message } from '@/app/lib/type/Messages';
import { deleteMessage, getMessages, sendMessage, updateMessage } from '@/app/lib/utils/messages';
import { zodResolver } from '@hookform/resolvers/zod';
import { Button } from '@repo/ui/components/ui/button';
Expand Down Expand Up @@ -44,11 +44,13 @@ export default function ChatView() {
fetchMessages();

const socket = io(process.env.NEXT_PUBLIC_SOCKET_ENDPOINT || '');
socket.on('receivedMessage', (payload: SocketMessage) => {
socket.on('receivedMessage', (payload) => {
console.log(payload);
fetchMessages();
});

return () => {
socket.off('receivedMessage');
socket.disconnect();
};
}, []);
Expand Down
12 changes: 0 additions & 12 deletions apps/client/app/lib/type/Messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,3 @@ export type Message = {
updated_at: string | null;
name: string;
};

export type SocketMessage = {
new: Message | undefined;
old:
| Message
| {
id: number;
name: string;
}
| undefined;
event: 'INSERT' | 'UPDATE' | 'DELETE';
};

0 comments on commit d1f8ca2

Please sign in to comment.