Beginning the Initial Connection

Dear Sciaku Learner you are not logged in or not enrolled in this course.

Please Click on login or enroll now button.

If you have any query feel free to chat us!

Happy Coding! Happy Learning!

Lecture 394:- Beginning the Initial Connection

To begin the initial connection with Socket.io, we first need to set up the server-side code. We will create a new file index.js inside the sockets folder and set up the basic Socket.io connection.

First, we need to install the socket.io package using NPM:

luaCopy code

npm install socket.io

Then, in our index.js file, we can set up the basic server-side code as follows:

javascriptCopy code

const io = require('socket.io')(server); io.on('connection', (socket) => {  console.log('A user connected!'); });

Here, we are initializing a new instance of Socket.io and passing in our server instance as an argument. We then attach a listener to the connection event, which fires whenever a client connects to our server. In this case, we simply log a message to the console to confirm that a user has connected.

We can now run the server and test the connection by opening a client-side Socket.io connection. In our index.ejs file, we can add the following script tag to establish a client-side connection:

htmlCopy code

<script src="/socket.io/socket.io.js"></script> <script>  const socket = io();  socket.on('connect', () => {    console.log('Connected to server!');  }); </script>

Here, we are including the Socket.io client-side script, then initializing a new instance of Socket.io and attaching a listener to the connect event. When this event fires, we log a message to the console to confirm that we have successfully connected to the server.

Now, when we run our application and open it in the browser, we should see two messages in the console: "A user connected!" (from the server-side code) and "Connected to server!" (from the client-side code), confirming that our Socket.io connection is working properly.

46. Chatting Engine

Comments: 2

profile
@niteshguptav63
17-Nov-2024, 01:39 PM

I am not able to access videos from second class and further. I have already completed first class

profile
@niteshguptav63
16-Nov-2024, 10:56 AM

When will I get my course?

profile
@admin79
17-Nov-2024, 01:29 PM

Now, Your query was resolved.

Frequently Asked Questions (FAQs)

How do I register on Sciaku.com?
How can I enroll in a course on Sciaku.com?
Are there free courses available on Sciaku.com?
How do I purchase a paid course on Sciaku.com?
What payment methods are accepted on Sciaku.com?
How will I access the course content after purchasing a course?
How long do I have access to a purchased course on Sciaku.com?
How do I contact the admin for assistance or support?
Can I get a refund for a course I've purchased?
How does the admin grant access to a course after payment?