What Is Socket.IO? A Deep Dive into Features, Versions, and Cross‑Language Implementations
This article explains Socket.IO’s low‑latency, bi‑directional, event‑driven communication model, outlines version differences, lists server and client language implementations, clarifies common misconceptions, details core features such as fallback, reconnection and broadcasting, and provides a complete PHP server‑client example with custom events and workerStart handling.
What is Socket.IO
Socket.IO is a library that enables low‑latency, bi‑directional, event‑driven communication between client and server.
Official site:
https://socket.ioVersion differences
Since its inception Socket.IO has released major versions 1.x, 2.x, 3.x and 4.x, each adding new features and performance improvements.
1.x : initial release with basic real‑time communication.
2.x : better error handling, protocol improvements, more stable connections.
3.x : higher performance, lower latency, TypeScript support, better framework integration.
4.x : WebSocket protocol updated for HTTP/2, modern JavaScript, API changes, dropped support for old Node.js versions.
Language implementations
Server side
JavaScript (Node.js) – https://github.com/socketio/socket.io JavaScript (Deno) – https://github.com/socketio/socket.io-deno Java – https://github.com/mrniko/netty-socketio Java – https://github.com/trinopoty/socket.io-server-java Python – https://github.com/miguelgrinberg/python-socketio Golang – https://github.com/googollee/go-socket.io Rust – https://github.com/Totodore/socketioxide PHP –
https://github.com/walkor/phpsocket.ioClient side
JavaScript (browser, Node.js, React Native) – installation steps, API, source code.
JavaScript (WeChat Mini‑Programs) – https://github.com/weapp-socketio/weapp.socket.io Java – https://github.com/socketio/socket.io-client-java C++ – https://github.com/socketio/socket.io-client-cpp Swift – https://github.com/socketio/socket.io-client-swift Dart – https://github.com/rikulo/socket.io-client-dart Python – https://github.com/miguelgrinberg/python-socketio.Net – https://github.com/doghappy/socket.io-client-csharp Rust – https://github.com/1c3t3a/rust-socketio Kotlin – https://github.com/icerockdev/moko-socket-io PHP –
https://github.com/ElephantIO/elephant.ioWhat Socket.IO is not
Socket.IO is not a pure WebSocket implementation.
Although it falls back to WebSocket when possible, it adds extra metadata to each packet, making direct WebSocket‑to‑Socket.IO connections incompatible.
// WARNING: the client will NOT be able to connect!
const socket = io("ws://echo.websocket.org");Socket.IO is not suitable for background services in mobile apps.
The library keeps an open TCP connection, which can drain battery; use dedicated push services such as FCM instead.
Key features
HTTP long‑polling fallback
If a WebSocket connection cannot be established, Socket.IO automatically falls back to HTTP long‑polling, a feature still useful for users behind misconfigured proxies.
Automatic reconnection
Socket.IO includes a heartbeat mechanism that detects broken connections and automatically attempts reconnection with exponential back‑off.
Packet buffering
When the client disconnects, outgoing packets are buffered and sent once the connection is restored.
Acknowledgments (acks)
Events can be emitted with a callback that receives a response from the other side.
socket.emit("hello", "world", (response) => {
console.log(response); // "got it"
});A timeout can be added:
socket.timeout(5000).emit("hello", "world", (err, response) => {
if (err) {
// the other side did not acknowledge within the delay
} else {
console.log(response); // "got it"
}
});Broadcast
Servers can emit to all clients or to a specific room.
// to all connected clients
io.emit("hello");
// to all clients in the "news" room
io.to("news").emit("hello");Multiplexing (namespaces)
Namespaces let you split application logic over a single shared connection, e.g., an admin channel.
io.on("connection", (socket) => { /* classic users */ });
io.of("/admin").on("connection", (socket) => { /* admin users */ });Practical example – PHP server with PHPSocket.IO
Installation
composer require workerman/phpsocket.ioServer code (server.php)
<?php
declare(strict_types=1);
require_once '../vendor/autoload.php';
use Workerman\Worker;
use PHPSocketIO\SocketIO;
// create socket.io server listening on port 2024
$io = new SocketIO(2024);
$io->on('connection', function ($socket) use ($io) {
echo "[x] new connection coming".PHP_EOL;
});
Worker::runAll();Start the server:
php server.php startClient HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>phpsocket.io client</title>
</head>
<body>
<script src="https://cdn.bootcss.com/socket.io/2.0.3/socket.io.js"></script>
<script>
var socket = io('http://127.0.0.1:2024');
socket.on('connect', function() {
console.log('connect success');
});
</script>
</body>
</html>When the page loads, the server logs new connections.
Custom events
Beyond the built‑in connect, message, and disconnect events, developers can define their own events using emit and on.
<?php
require_once __DIR__.'/vendor/autoload.php';
use Workerman\Worker;
use PHPSocketIO\SocketIO;
$io = new SocketIO(2024);
$io->on('connection', function ($socket) use ($io) {
$socket->on('chat message', function ($msg) use ($io) {
$io->emit('chat message from server', $msg);
});
});
Worker::runAll();Client triggers the event:
<script src="//cdn.bootcss.com/socket.io/1.3.7/socket.io.js"></script>
<script>
var socket = io('http://127.0.0.1:2024');
socket.emit('chat message', 'this is the message...');
socket.on('chat message from server', function(msg){
console.log('get message:' + msg + ' from server');
});
</script>workerStart event
The workerStart callback runs once when the process starts, allowing you to set up additional workers, such as an HTTP endpoint for pushing messages.
require_once __DIR__.'/vendor/autoload.php';
use Workerman\Worker;
use PHPSocketIO\SocketIO;
$io = new SocketIO(9120);
$io->on('workerStart', function() use ($io) {
$inner_http_worker = new Worker('http://0.0.0.0:9191');
$inner_http_worker->onMessage = function($http_connection, $data) use ($io){
if(!isset($_GET['msg'])) {
return $http_connection->send('fail, $_GET["msg"] not found');
}
$io->emit('chat message', $_GET['msg']);
$http_connection->send('ok');
};
$inner_http_worker->listen();
});
$io->on('connection', function($socket) use ($io){
$socket->on('chat message', function($msg) use ($io){
$io->emit('chat message from server', $msg);
});
});
Worker::runAll();Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
