Giter Site home page Giter Site logo

Comments (4)

Sartori-RIA avatar Sartori-RIA commented on August 14, 2024 6

the way I managed to make the socket work in conjunction with the overnight is as follows:

export class AppServer extends Server {
    private readonly logger: Logger;
    private close: http.Server;

    constructor() {
        super();
        this.logger = new Logger();
        this.logger.rmTimestamp = true;
        this.app.use(cors());
        this.app.use(compression());
        this.app.use(helmet());
        this.app.use(bodyParser.json());
        this.app.use(bodyParser.urlencoded({extended: true}));
        this.app.use((req: Request, res: Response, next: NextFunction) => {
            res.setHeader('Content-type', 'application/json');
            res.setHeader('Accept', 'application/json');
            res.setHeader('Access-Control-Allow-Origin', '*');
            res.setHeader('Access-Control-Allow-Credentials', 'true');
            next();
        });
        this.mongoConnection();
        this.setupControllers();
    }

    stop() {
        this.close.close();
    }

    start() {
        const port = 4000;
        this.close = this.app.listen(port, () => {
            this.logger.info('Server listening on port: ' + port);
        });
    }

    initSocket() {
        this.logger.info('initializing socket');
        const io = require('socket.io')(this.close);
        io.on('connection', (socket: Socket) => {
            this.logger.info('user connected');
            socket.on('chat', (data: any) => {
                io.emit('chat', data);
            });
        });
    }

    private setupControllers() {
        this.logger.rmTimestamp = true;
        this.logger.info('\nRegistering controllers');
        super.addControllers([
            new ChatController(),
        ]);
    }

    private mongoConnection() {
        const url = `${process.env.MONGODB_URL}:${process.env.MONGO_PORT}`;
        connect(process.env.MONGODB_URI || url,
            {
                useNewUrlParser: true,
                useFindAndModify: false,
                useCreateIndex: true,
                dbName: process.env.MONGO_DB_NAME,
                user: process.env.MONGO_USER,
                pass: process.env.MONGO_PASSWORD,
            },
        ).catch((err) => {
            throw err;
        });
    }
}

from overnight.

seanpmaxwell avatar seanpmaxwell commented on August 14, 2024

Haven't used socket.io so can't say. Just checked the docs here, https://socket.io/docs/#Using-with-Express. I don't see how OvernightJS would make any difference.

from overnight.

Sartori-RIA avatar Sartori-RIA commented on August 14, 2024

and my startup server

const server = new AppServer();
server.start();
server.initSocket();

from overnight.

seanpmaxwell avatar seanpmaxwell commented on August 14, 2024

Thanks for posting that. I'm sure some will find it useful.

from overnight.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.