简体   繁体   中英

Best practices in structuring Node.js & Socket.io app?

Currently I'm developing a node server which uses websockets for communication. I'm used to applying MVC (or MC) pattern in my apps. I'd like to structure my socket.io in similiar way and my question is how to do this in the best way?

For now I have something like this:

utils/socket.ts:

type IO = null | SocketIO.Server;
let io: IO;

export function init(ioServer: IO) {
  io = ioServer;
}

export function getIO(): IO {
  return io;
}

app.ts:

import express from 'express';
...

import { init } from './utils/socket';
import startSockets from './controllers';

const app = express();


...

const server = app.listen(process.env.PORT || 5000);

const io = socketio.listen(server);

if (io) {
  init(io);
  io.on('connect', startSockets);
}

controllers/index.ts:

import { onConnect } from './connect';
import { getIO } from '../utils/socket';

export default function (socket: SocketIO.Socket) {
  const io = getIO();
  socket.on('connect-player', onConnect);
}

controllers/connect.ts:

 import Player from '../models/Player';

  export const onConnect = async function (this: SocketIO.Socket, name: string) {
  const player = await Player.create({ name });

  this.broadcast.emit('player-connected', `Player ${name} joined the game!`);
  this.emit(
    'player-connected',
    `Congratulations ${name}, you successfully joined our game!`,
    player,
  );

  this.on('disconnect', onDisconnect.bind(this, player.id));
};

const onDisconnect = async function (this: SocketIO.Socket, playerId: string) {
  const player = await Player.findById(playerId);
  await player?.remove();
  this.broadcast.emit(
    'player-connected',
    `Player ${player?.name} left our game!`,
  );
};

I'm not sure about using 'this' in my controller and about that singleton pattern in utils/socket.ts. Is that proper? The most important for me is to keep the app clear and extensible.

I'm using express and typescript. Player.ts is my moongoose schema of player.

I had been struggling with socket.io + express.js a while back, specially I am as well use to apply MVC patterns.
While searching, googling and stack overflowing, these links helped me with the project back than.
https://blueanana.github.io/2017/03/18/Socket-io-Express-4/
https://github.com/onedesign/express-socketio-tutorial
https://gist.github.com/laterbreh/5d7bdf03258152c95b8d
Using socket.io in Express 4 and express-generator's /bin/www

I wont say what is the best way or not, I dont even like that expression. But I will say it is a way, which helped to keep it clear, extensible, organized and very well modularized.
Specially that each project has its own intrinsic requirements and needs.
Hope these can get to help you as well as it did it for me, get the feeling and understanding of how and what it needs to be done on the project.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM