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 us…


This content originally appeared on DEV Community and was authored by Abhishek shah

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.


This content originally appeared on DEV Community and was authored by Abhishek shah


Print Share Comment Cite Upload Translate Updates
APA

Abhishek shah | Sciencx (2021-09-20T09:34:06+00:00) Using Socket.io in express-generator.. Retrieved from https://www.scien.cx/2021/09/20/using-socket-io-in-express-generator/

MLA
" » Using Socket.io in express-generator.." Abhishek shah | Sciencx - Monday September 20, 2021, https://www.scien.cx/2021/09/20/using-socket-io-in-express-generator/
HARVARD
Abhishek shah | Sciencx Monday September 20, 2021 » Using Socket.io in express-generator.., viewed ,<https://www.scien.cx/2021/09/20/using-socket-io-in-express-generator/>
VANCOUVER
Abhishek shah | Sciencx - » Using Socket.io in express-generator.. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/09/20/using-socket-io-in-express-generator/
CHICAGO
" » Using Socket.io in express-generator.." Abhishek shah | Sciencx - Accessed . https://www.scien.cx/2021/09/20/using-socket-io-in-express-generator/
IEEE
" » Using Socket.io in express-generator.." Abhishek shah | Sciencx [Online]. Available: https://www.scien.cx/2021/09/20/using-socket-io-in-express-generator/. [Accessed: ]
rf:citation
» Using Socket.io in express-generator. | Abhishek shah | Sciencx | https://www.scien.cx/2021/09/20/using-socket-io-in-express-generator/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.