Skip to content

Commit

Permalink
initial socket io admin client
Browse files Browse the repository at this point in the history
  • Loading branch information
huytran17 committed May 28, 2024
1 parent aea2599 commit 2530171
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
34 changes: 34 additions & 0 deletions core/admin-dashboard/config/socket.io/private-admin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { io, Socket } from "socket.io-client";
import {
SOCKET_LISTEN_EVENT,
SOCKETIO_EMIT_EVENT,
SOCKETIO_NSP,
} from "~/constants";

interface IUserPayload {
user_id: string;
}

interface ServerToClientEvents {
[SOCKET_LISTEN_EVENT.ONLINE]: ({ user_id }: IUserPayload) => void;
[SOCKET_LISTEN_EVENT.OFFLINE]: ({ user_id }: IUserPayload) => void;
}

interface ClientToServerEvents {
[SOCKETIO_EMIT_EVENT.ONLINE]: ({ user_id }: IUserPayload) => void;
}

export default function initialPrivateSocketIO() {
try {
const socket: Socket<ServerToClientEvents, ClientToServerEvents> = io(
`${process.env.SERVER_URL}/${SOCKETIO_NSP.PRIVATE_ADMIN}`,
{
withCredentials: true,
}
);

return socket;
} catch (error) {
console.error(error);
}
}
25 changes: 24 additions & 1 deletion core/admin-dashboard/constants/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,33 @@ const LOGIN_FAILED = {
MAX: 5,
};

const SOCKETIO_NSP = {
PRIVATE_ADMIN: "private-admin",
};

enum SOCKETIO_EMIT_EVENT {
ONLINE = "online",
}

enum SOCKET_LISTEN_EVENT {
ONLINE = "online",
OFFLINE = "offline",
}

export default Object.freeze({
ADMIN_TYPES,
HTTP_STATUS_CODE,
LOGIN_FAILED,
SOCKETIO_NSP,
SOCKETIO_EMIT_EVENT,
SOCKET_LISTEN_EVENT,
});

export { ADMIN_TYPES, HTTP_STATUS_CODE, LOGIN_FAILED };
export {
ADMIN_TYPES,
HTTP_STATUS_CODE,
LOGIN_FAILED,
SOCKETIO_NSP,
SOCKETIO_EMIT_EVENT,
SOCKET_LISTEN_EVENT,
};
16 changes: 14 additions & 2 deletions core/admin-dashboard/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@
</template>

<script>
import { mapActions, mapGetters } from "vuex";
import BaseDashboardAnalysis from "@/components/dashboard/BaseDashboardAnalysis";
import BaseAnalysisToggler from "@/components/dashboard/BaseAnalysisToggler";
import BaseDashboardAnalysis from "@/components/dashboard/BaseDashboardAnalysis";
import initialPrivateSocketIO from "@/config/socket.io/private-admin";
import { mapActions, mapGetters } from "vuex";
import { SOCKETIO_EMIT_EVENT } from "~/constants";
export default {
name: "AdminDashboard",
Expand All @@ -20,6 +22,7 @@ export default {
computed: {
...mapGetters({
analysis: "analysis",
me: "auth/me",
}),
},
Expand All @@ -44,6 +47,15 @@ export default {
this.GET_MOST_POPULAR_POSTS_ANALYTICS({ ...this.analysis }),
this.GET_CATEGORY_ANALYTICS({ ...this.analysis }),
]);
const user_id = this.me?._id;
if (!user_id) {
return;
}
const socket = initialPrivateSocketIO();
socket.emit(SOCKETIO_EMIT_EVENT.ONLINE, { user_id });
} catch (error) {
console.error(error);
}
Expand Down

0 comments on commit 2530171

Please sign in to comment.