DEV Community

admirableyetti
admirableyetti

Posted on

Beginners socket.io code without cors error XD

extensions required: live server that's all.

I am going to assume that you already have previous programming knowledge and that you have tried to use socket.io and for any reason it has not worked for you, so I will share you the code that I used to make my first socket and that it worked especially for the problem of cors.

to our blank project create the following files
files to create our first websocket

launch a new project with npm init -y command it creates a file package.json then run npm i nodemon socket.io and add line 9 on your package.json file that will use nodemon with server.js
package.json file

add the following code to server.js file, usually live server use 5500 port,line 4 is where you give permission to connect...
server.js file

else it'll deny you access and will throw cors error

cors error

index.js
index.js file

html file
html file

once you have all we seen run npm run serve and console will show init server, then on html file open live server open console and will show...
console will show init server

now that it works you have the basic to begin on websockets, finally you have to search more and better info to make upgrade your code

feel free to feedback

and there you go, my first basic beginners socket.io post

Top comments (0)