Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Socket.Io Docs by Removing createServer #5003

Open
priyankarpal opened this issue Apr 22, 2024 · 1 comment
Open

Simplify Socket.Io Docs by Removing createServer #5003

priyankarpal opened this issue Apr 22, 2024 · 1 comment
Labels
question Further information is requested

Comments

@priyankarpal
Copy link

In the Socket.IO documentation, consider removing references to createServer() and adding the use of app.listen() for server setup. This adjustment would streamline the documentation and make it more accessible, particularly for users who are new to Socket.IO and Express

Before

import express from 'express';
import { createServer } from 'node:http';

const app = express();
const server = createServer(app);

app.get('/', (req, res) => {
  res.send('<h1>Hello world</h1>');
});

server.listen(3000, () => {
  console.log('server running at http://localhost:3000');
});

After

import express from 'express';

const app = express();

app.get('/', (req, res) => {
  res.send('<h1>Hello world</h1>');
});

app.listen(3000, () => {
  console.log('server running at http://localhost:3000');
});
@priyankarpal priyankarpal added the to triage Waiting to be triaged by a member of the team label Apr 22, 2024
@priyankarpal priyankarpal changed the title Clarify in Socket.IO Docs that app.listen() Can Be Used Directly for Server Setup Simplify Socket.Io Docs by Removing createServer Apr 22, 2024
@darrachequesne
Copy link
Member

Hi! I'm not sure that this is a good idea, as the current way indicates that the Node.js HTTP server can be shared between Socket.IO and Express:

Current version (explicit):

import express from 'express';
import { createServer } from 'node:http';
import { io } from 'socket.io';

const app = express();
const httpServer = createServer(app);

app.get('/', (req, res) => {
  res.send('<h1>Hello world</h1>');
});

httpServer.listen(3000, () => {
  console.log('server running at http://localhost:3000');
});

const io = new Server(httpServer);

Suggested version (implicit):

import express from 'express';
import { io } from 'socket.io';

const app = express();

app.get('/', (req, res) => {
  res.send('<h1>Hello world</h1>');
});

const httpServer = app.listen(3000, () => {
  console.log('server running at http://localhost:3000');
});

const io = new Server(httpServer);

There is also the risk that people mix the two:

import express from 'express';
import { createServer } from 'node:http';
import { io } from 'socket.io';

const app = express();
const httpServer = createServer(app);

app.get('/', (req, res) => {
  res.send('<h1>Hello world</h1>');
});

aop.listen(3000, () => {
  console.log('server running at http://localhost:3000');
});

const io = new Server(httpServer);

Which does not work because app.listen() creates another HTTP server.

Does that make sense?

@darrachequesne darrachequesne added question Further information is requested and removed to triage Waiting to be triaged by a member of the team labels Apr 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants