Categories
JavaScript Answers

How to connect to TCP Socket from browser using JavaScript?

Spread the love

To connect to TCP Socket from browser using JavaScript, we call the socket.create method.

For instance, we write

chrome.experimental.socket.create("tcp", "127.0.0.1", 8080, (socketInfo) => {
  chrome.experimental.socket.connect(socketInfo.socketId, (result) => {
    chrome.experimental.socket.write(socketInfo.socketId, "Hello, world!");
  });
});

to call the socket.create method to create a socket to 127.0.0.1 port 8080.

We then call socket.connect method to connect to the socket with the socketInfo.

And then we call write to send data to the socket.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *