Skip to content

Commit

Permalink
fixed websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
farhanW3 committed Jan 11, 2024
1 parent 5479cdf commit 9942842
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { withErrorHandler } from "./middleware/error";
import { withExpress } from "./middleware/express";
import { withRequestLogs } from "./middleware/logs";
import { withOpenApi } from "./middleware/open-api";
import { withWebSocket } from "./middleware/websocket";
import { withRoutes } from "./routes";
import { writeOpenApiToFile } from "./utils/openapi";

Expand Down Expand Up @@ -51,6 +52,7 @@ const main = async () => {
await withCors(server);
await withRequestLogs(server);
await withErrorHandler(server);
await withWebSocket(server);
await withAuth(server);
await withExpress(server);
await withOpenApi(server);
Expand Down
23 changes: 23 additions & 0 deletions src/server/middleware/websocket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import WebSocketPlugin from "@fastify/websocket";
import { FastifyInstance } from "fastify";
import { logger } from "../../utils/logger";

export const withWebSocket = async (server: FastifyInstance) => {
await server.register(WebSocketPlugin, {
errorHandler: function (
error,
conn /* SocketStream */,
req /* FastifyRequest */,
reply /* FastifyReply */,
) {
logger({
service: "websocket",
level: "error",
message: `Websocket error: ${error}`,
});
// Do stuff
// destroy/close connection
conn.destroy(error);
},
});
};
15 changes: 4 additions & 11 deletions src/server/routes/transaction/status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,18 +95,11 @@ export async function checkTxStatus(fastify: FastifyInstance) {
});
},
wsHandler: async (connection: SocketStream, request) => {
logger({
service: "server",
level: "info",
message: `Websocket route handler`,
data: request,
});

const { queueId } = request.params;
// const timeout = await wsTimeout(connection, queueId, request);

logger({
service: "server",
service: "websocket",
level: "info",
message: `Websocket connection established for ${queueId}`,
});
Expand All @@ -127,7 +120,7 @@ export async function checkTxStatus(fastify: FastifyInstance) {

connection.socket.on("error", (error) => {
logger({
service: "server",
service: "websocket",
level: "error",
message: `Websocket error`,
error,
Expand All @@ -138,7 +131,7 @@ export async function checkTxStatus(fastify: FastifyInstance) {

connection.socket.on("message", async (message, isBinary) => {
logger({
service: "server",
service: "websocket",
level: "info",
message: `Websocket message received`,
data: message,
Expand All @@ -149,7 +142,7 @@ export async function checkTxStatus(fastify: FastifyInstance) {

connection.socket.on("close", () => {
logger({
service: "server",
service: "websocket",
level: "info",
message: `Websocket connection closed`,
});
Expand Down
6 changes: 4 additions & 2 deletions src/utils/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ export const env = createEnv({
.default("debug"),
LOG_SERVICES: z
.string()
.default("server,worker,cache")
.default("server,worker,cache,websocket")
.transform((s) =>
z.array(z.enum(["server", "worker", "cache"])).parse(s.split(",")),
z
.array(z.enum(["server", "worker", "cache", "websocket"]))
.parse(s.split(",")),
),
THIRDWEB_API_SECRET_KEY: z.string().min(1),
ADMIN_WALLET_ADDRESS: z.string().min(1),
Expand Down

0 comments on commit 9942842

Please sign in to comment.