An Interest In:
Web News this Week
- March 21, 2024
- March 20, 2024
- March 19, 2024
- March 18, 2024
- March 17, 2024
- March 16, 2024
- March 15, 2024
September 20, 2021 09:34 am GMT
Original Link: https://dev.to/shahiscoding/using-socket-io-in-express-generator-4pkp
Using Socket.io in express-generator.
Socket.io has documentation on how to use it with express but it a bit tricky to use socket.io with express-generator because the server is created in www file and the io is created in app or server but the use of io is in the router .So how can you use socket.io in express-generator ?
Its very simple you can create a new file as mysocket and create instance of io over there and export it.
In the mysocket file.
let socketIo = require('socket.io');let io = socketIo();let socketApi = {};socketApi.io = io;module.exports = socketApi;
In the www file
var server = http.createServer(app);socketAPI.io.attach(server);
So, you can import io from mysocket file into the router and use it.
If you have anything to add you can leave a comment.
Original Link: https://dev.to/shahiscoding/using-socket-io-in-express-generator-4pkp
Share this article:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To