The complete working project of chat with Sockets express generator
how to start the project.
- Clone the project.
- Install the dependencies by using: npm install --save
- Run the project by: npm start
Sockets are implemented in the Chat page click on the button to go to chat page.
Here you go! Project is available at URL http://localhost:3000/
Steps to follow to configure sockets.io with express-genertor
Install Socket.io with the following command:
-
npm install --save socket.io
-
Add the following to app.js:
var sockIO = require('socket.io')(); app.sockIO = sockIO;
-
In bin/www, after var server = http.createServer(app), add the following:
var sockIO = app.sockIO; sockIO.listen(server);
-
To test functionality, in app.js, you can add the line:
sockIO.on('connection', function(socket){ console.log('A client connection occurred!'); });
-
Now in layout.hbs add the following snippet before the body closing tag < /body >:
<script src="/socket.io/socket.io.js"></script> <script> var socket = io(); </script>