Skip to content

Instantly share code, notes, and snippets.

View alex-hladun's full-sized avatar
🏠
Working from home

Alex Hladun alex-hladun

🏠
Working from home
  • Accolite Digital
  • Calgary
View GitHub Profile
could not requestRemote: {
"methodName": "query",
"params": [
{
"query": {
"selector": {
"borerShiftProductionId": {
"$eq": "9d2d0e6f-6acb-4b9c-96e7-ba4153fa887d"
}
},
@alex-hladun
alex-hladun / SessionStore.ts
Created October 7, 2021 02:55
Querying the online user list
getOnlineUsers = async () => {
const keys = new Set();
let cursor = "0";
do {
await new Promise((resolve, reject) =>
this.redisClient.scan(
cursor,
"MATCH",
"session:*",
"COUNT",
@alex-hladun
alex-hladun / SessionStore.ts
Last active November 1, 2021 23:27
Saving & retrieving sessions in Redis
// Save a session for a user and their online status
saveSession(id: string, { username, connected }: { [key: string]: string }) {
this.redisClient
.multi()
.hmset(`session:${id}`, "username", username, "connected", connected)
.expire(`session:${id}`, SESSION_TTL)
.exec();
}
@alex-hladun
alex-hladun / SocketManager.ts
Created October 7, 2021 02:45
Connecting to socket.io-redis-adapter
connectToHostedRedis = (redisEndpoint: string) => {
this.pubClient = new RedisClient({ host: redisEndpoint, port: 6379 });
const subClient = this.pubClient.duplicate();
this.io.adapter(createAdapter({ pubClient: this.pubClient, subClient }));
this.sessionStore = new RedisSessionStore(this.pubClient);
};
@alex-hladun
alex-hladun / Routes.ts
Created September 30, 2021 18:52
Route for authentication on login
import { Request, Response } from "express";
import { createUser } from "./DynamoPuts";
import { verifyLogin } from "./DynamoQueries";
const registerRoutes = (dynamoEnabled: boolean) => {
const express = require("express");
const router = express.Router();
router.post("/login", async (req: Request, res: Response) => {
if (dynamoEnabled) {
@alex-hladun
alex-hladun / SocketManager.ts
Last active November 1, 2021 23:22
Auth middleware for socket
class SocketManager {
io: SocketIO.Server;
pubClient: redis.RedisClient;
sessionStore: RedisSessionStore;
serverConfig: IServerConfig;
redisEnabled: boolean;
dynamoEnabled: boolean;
constructor(server: http.Server, config: IServerConfig) {
this.configureMiddleware();
@alex-hladun
alex-hladun / SocketManager.ts
Last active October 7, 2021 02:42
Setting up cors on the back-end
// On the back-end
this.io = new Server(server, {
cors: {
// Would change origin to eventual DNS for React app
origin: "http://localhost:3000",
methods: ["GET", "POST"],
credentials: true
},
allowEIO3: true, // Whether to enable compatibility with Socket.IO v2 clients.
maxHttpBufferSize: 1024, // max message payload size (prevents clients from sending gigabytes of data)
@alex-hladun
alex-hladun / SocketManager.ts
Last active November 1, 2021 23:12
Setting up the socket listeners
import { joinRoom, saveRoomMessage } from "./DynamoPuts";
class SocketManager {
io: SocketIO.Server;
dynamoEnabled: boolean;
redisEnabled: boolean;
dynamoEnabled: boolean;
constructor(server: http.Server, config: IServerConfig) {
this.generateSocketServer(server);
@alex-hladun
alex-hladun / SocketManager.ts
Last active November 1, 2021 23:10
Connecting Socket.io to our server
import { Server, Socket } from "socket.io";
Class SocketManager {
io: SocketIO.Server;
pubClient: redis.RedisClient;
sessionStore: RedisSessionStore;
serverConfig: IServerConfig;
redisEnabled: boolean;
dynamoEnabled: boolean;
@alex-hladun
alex-hladun / App.ts
Created September 30, 2021 17:50
Express server setup
import { createServer } from "http";
import express from "express";
import cors from "cors";
const app = express();
app.use(
cors({
origin: "http://localhost:3000",
methods: ["GET", "POST"],
credentials: true