socket.io-client
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
@socket.io/component-emitter | 3.1.2 | 20.72 kB | MIT | prod | |
debug | 4.3.4 | 12.94 kB | MIT | prod | |
engine.io-client | 6.5.3 | 151.75 kB | MIT | prod | |
socket.io-parser | 4.2.4 | 7.48 kB | MIT | prod |
Socket.io-client is a client-side library that allows for real-time, bidirectional, and event-based communication between your web browser and a server. It facilitates the creation and handling of web socket connections, which are fundamental to real time web applications. Socket.io-client is particularly known for its reliable and efficient data transmission, and its ability to automatically degrade to polling if WebSockets are not available in the user's environment.
To use the Socket.io-client in your project, you would install it as a dependency via the Node Package Manager (npm) by running the command npm install socket.io-client
in your console. After successful installation, you can import it into your JavaScript file with the require method as such:
const io = require("socket.io-client");
let socket = io("http://localhost");
Replace "http://localhost"
with the URL of your server.
Once the connection is created, you can listen for and emit events with the on
and emit
functions respectively. For example:
socket.on("hello", (arg) => {
console.log(arg); // prints "Hello World"
});
socket.emit("hello", "Hello World");
This example listens for the event named "hello" and prints its argument, and then emits the same event with the argument "Hello World".
The comprehensive and up-to-date documentation for the socket.io-client library can be found on the official website at https://socket.io/docs/v4/client-initialization/. The source code for the site, where you can contribute if you wish, is on GitHub at https://github.com/socketio/socket.io-website.