Sockets API Documentation

Discover and understand the Socket.js API.

Introduction

The Socket.js API is a powerful and flexible library for building network applications. It provides a robust and efficient way to handle network connections, data transfer, and asynchronous operations.

Key Concepts

Data Transfer Objects (DTOs): These represent the data being transferred during a connection. They encapsulate data and methods for handling the transfer.

Connection Handling: Socket.js offers a streamlined mechanism for establishing, maintaining, and closing connections.

Event Handling: The API utilizes events to provide clear and concise feedback on the state of the connection and operations.

API Endpoints

Here’s a breakdown of commonly used API endpoints:

  • `socket.connect(host, port)`: Connects to a socket.
  • `socket.send(data)`: Sends data to the socket.
  • `socket.receive(data)`: Receives data from the socket.
  • `socket.close()`: Closes the socket.
  • `socket.socketname()`: Get the socket name.
  • `socket.get(data)`: Get data.
  • `socket.set(data, callback)`: Set data.
  • `socket.get(socket_name, callback)`: Get data.

Example Code (JavaScript)


function connect(host, port) {
  const socket = socket.socket;
  socket.connect(host, port);
}

function sendData(data) {
  socket.send(data);
}

function receiveData(data) {
  socket.receive(data);
}

function closeSocket() {
    socket.close();
}

function getSocketName() {
    return socket.socketname();
}

function setSocketName(name) {
  socket.socketname(name);
}

function getSocket(socket_name) {
  return socket.socketname();
}

Error Handling