1<!doctype html> 2<html lang="en" dir="ltr" class="docs-wrapper docs-doc-page docs-version-current plugin-docs plugin-id-default docs-doc-id-categories/Documentation/index" data-has-hydrated="false"> 3<head> 4<meta charset="UTF-8"> 5<meta name="generator" content="Docusaurus v2.4.3"> 6<title data-rh="true">Introduction | Socket.IO</title><meta data-rh="true" name="viewport" content="width=device-width,initial-scale=1"><meta data-rh="true" name="twitter:card" content="summary_large_image"><meta data-rh="true" property="og:url" content="https://socket.io/docs/v4/"><meta data-rh="true" name="docusaurus_locale" content="en"><meta data-rh="true" name="docsearch:language" content="en"><meta data-rh="true" name="docusaurus_version" content="current"><meta data-rh="true" name="docusaurus_tag" content="docs-default-current"><meta data-rh="true" name="docsearch:version" content="current"><meta data-rh="true" name="docsearch:docusaurus_tag" content="docs-default-current"><meta data-rh="true" property="og:title" content="Introduction | Socket.IO"><meta data-rh="true" name="description" content="If you are new to Socket.IO, we recommend checking out our tutorial."><meta data-rh="true" property="og:description" content="If you are new to Socket.IO, we recommend checking out our tutorial."><link data-rh="true" rel="icon" href="/images/favicon.png"><link data-rh="true" rel="canonical" href="https://socket.io/docs/v4/"><link data-rh="true" rel="alternate" href="https://socket.io/docs/v4/" hreflang="en"><link data-rh="true" rel="alternate" href="https://socket.io/es/docs/v4/" hreflang="es"><link data-rh="true" rel="alternate" href="https://socket.io/fr/docs/v4/" hreflang="fr"><link data-rh="true" rel="alternate" href="https://socket.io/pt-br/docs/v4/" hreflang="pt-br"><link data-rh="true" rel="alternate" href="https://socket.io/zh-CN/docs/v4/" hreflang="zh-CN"><link data-rh="true" rel="alternate" href="https://socket.io/docs/v4/" hreflang="x-default"><link data-rh="true" rel="preconnect" href="https://ZM7QMFKQCJ-dsn.algolia.net" crossorigin="anonymous"><link rel="alternate" type="application/rss+xml" href="/blog/rss.xml" title="Socket.IO RSS Feed"> 7<link rel="alternate" type="application/atom+xml" href="/blog/atom.xml" title="Socket.IO Atom Feed"> 8 9 10 11<link rel="search" type="application/opensearchdescription+xml" title="Socket.IO" href="/opensearch.xml"><link rel="stylesheet" href="/assets/css/styles.0c095250.css"> 12<link rel="preload" href="/assets/js/runtime~main.b93ab2a6.js" as="script"> 13<link rel="preload" href="/assets/js/main.a60ddce9.js" as="script"> 14</head> 15<body class="navigation-with-keyboard">
16<script>!function(){function t(t){document.documentElement.setAttribute("data-theme",t)}var e=function(){var t=null;try{t=new URLSearchParams(window.location.search).get("docusaurus-theme")}catch(t){}return t}()||function(){var t=null;try{t=localStorage.getItem("theme")}catch(t){}return t}();null!==e?t(e):window.matchMedia("(prefers-color-scheme: dark)").matches?t("dark"):(window.matchMedia("(prefers-color-scheme: light)").matches,t("light"))}(),document.documentElement.setAttribute("data-announcement-bar-initially-dismissed",function(){try{return"true"===localStorage.getItem("docusaurus.announcement.dismiss")}catch(t){}return!1}())</script>
16<div id="__docusaurus"> 17<div role="region" aria-label="Skip to main content"><a class="skipToContent_fXgn" href="#__docusaurus_skipToContent_fallback">Skip to main content</a></div><div class="announcementBar_mb4j" style="background-color:#25c2a0" role="banner"><div class="announcementBarPlaceholder_vyr4"></div><div class="content_knG7 announcementBarContent_xLdY">Latest blog post (July 25, 2024): <a href="/blog/npm-package-provenance/">npm package provenance</a>.</div><button type="button" aria-label="Close" class="clean-btn close closeButton_CVFx announcementBarClose_gvF7"><svg viewBox="0 0 15 15" width="14" height="14"><g stroke="currentColor" stroke-width="3.1"><path d="M.75.75l13.5 13.5M14.25.75L.75 14.25"></path></g></svg></button></div><nav aria-label="Main" class="navbar navbar--fixed-top navbarHideable_m1mJ"><div class="navbar__inner"><div class="navbar__items"><button aria-label="Toggle navigation bar" aria-expanded="false" class="navbar__toggle clean-btn" type="button"><svg width="30" height="30" viewBox="0 0 30 30" aria-hidden="true"><path stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="2" d="M4 7h22M4 15h22M4 23h22"></path></svg></button><a class="navbar__brand" href="/"><div class="navbar__logo"><img src="/images/logo.svg" alt="Socket.IO logo" class="themedImage_ToTc themedImage--light_HNdA"><img src="/images/logo-dark.svg" alt="Socket.IO logo" class="themedImage_ToTc themedImage--dark_i4oU"></div><b class="navbar__title text--truncate">Socket.IO</b></a><div class="navbar__item dropdown dropdown--hoverable"><a href="#" aria-haspopup="true" aria-expanded="false" role="button" class="navbar__link">Docs</a><ul class="dropdown__menu"><li><a aria-current="page" class="dropdown__link dropdown__link--active" href="/docs/v4/">Guide</a></li><li><a class="dropdown__link" href="/docs/v4/tutorial/introduction">Tutorial</a></li><li><a class="dropdown__link" href="/get-started/">Examples</a></li><li><a class="dropdown__link" href="/docs/v4/emit-cheatsheet/">Emit cheatsheet</a></li></ul></div><a class="navbar__item navbar__link" href="/docs/v4/server-api/">Server API</a><a class="navbar__item navbar__link" href="/docs/v4/client-api/">Client API</a><div class="navbar__item dropdown dropdown--hoverable"><a href="#" aria-haspopup="true" aria-expanded="false" role="button" class="navbar__link">Ecosystem</a><ul class="dropdown__menu"><li class="dropdown-category"><b>Help</b></li><li><a class="dropdown__link" href="/docs/v4/troubleshooting-connection-issues/">Troubleshooting</a></li><li><a href="https://stackoverflow.com/questions/tagged/socket.io" target="_blank" rel="noopener noreferrer" class="dropdown__link">Stack Overflow<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li><a href="https://github.com/socketio/socket.io/discussions" target="_blank" rel="noopener noreferrer" class="dropdown__link">GitHub Discussions<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li><a href="https://socketio-slackin.herokuapp.com/" target="_blank" rel="noopener noreferrer" class="dropdown__link">Slack<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li><hr class="dropdown-separator"></li><li class="dropdown-category"><b>News</b></li><li><a class="dropdown__link" href="/blog">Blog</a></li><li><a href="https://twitter.com/SocketIO" target="_blank" rel="noopener noreferrer" class="dropdown__link">Twitter<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU">
17<path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li><hr class="dropdown-separator"></li><li class="dropdown-category"><b>Tools</b></li><li><a href="https://cdn.socket.io" target="_blank" rel="noopener noreferrer" class="dropdown__link">CDN<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li><a href="https://admin.socket.io" target="_blank" rel="noopener noreferrer" class="dropdown__link">Admin UI<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li></ul></div><div class="navbar__item dropdown dropdown--hoverable"><a href="#" aria-haspopup="true" aria-expanded="false" role="button" class="navbar__link">About</a><ul class="dropdown__menu"><li><a class="dropdown__link" href="/docs/v4/faq/">FAQ</a></li><li><a class="dropdown__link" href="/docs/v4/changelog/">Changelog</a></li><li><a href="https://github.com/orgs/socketio/projects/3" target="_blank" rel="noopener noreferrer" class="dropdown__link">Roadmap<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li><a href="https://opencollective.com/socketio" target="_blank" rel="noopener noreferrer" class="dropdown__link">Become a sponsor<svg width="12" height="12" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li></ul></div></div><div class="navbar__items navbar__items--right"><div class="navbar__item dropdown dropdown--hoverable dropdown--right"><a aria-current="page" class="navbar__link active" aria-haspopup="true" aria-expanded="false" role="button" href="/docs/v4/">4.x</a><ul class="dropdown__menu"><li><a aria-current="page" class="dropdown__link dropdown__link--active" href="/docs/v4/">4.x</a></li><li><a class="dropdown__link" href="/docs/v3/">3.x</a></li><li><a class="dropdown__link" href="/docs/v2/">2.x</a></li><li><hr class="dropdown-separator"></li><li><a class="dropdown__link" href="/docs/v4/changelog/">Changelog</a></li></ul></div><div class="navbar__item dropdown dropdown--hoverable dropdown--right"><a href="#" aria-haspopup="true" aria-expanded="false" role="button" class="navbar__link"><svg viewBox="0 0 24 24" width="20" height="20" aria-hidden="true" class="iconLanguage_nlXk"><path fill="currentColor" d="M12.87 15.07l-2.54-2.51.03-.03c1.74-1.94 2.98-4.17 3.71-6.53H17V4h-7V2H8v2H1v1.99h11.17C11.5 7.92 10.44 9.75 9 11.35 8.07 10.32 7.3 9.19 6.69 8h-2c.73 1.63 1.73 3.17 2.98 4.56l-5.09 5.02L4 19l5-5 3.11 3.11.76-2.04zM18.5 10h-2L12 22h2l1.12-3h4.75L21 22h2l-4.5-12zm-2.62 7l1.62-4.33L19.12 17h-3.24z"></path></svg>English</a><ul class="dropdown__menu"><li><a href="/docs/v4/" target="_self" rel="noopener noreferrer" class="dropdown__link dropdown__link--active" lang="en">English</a></li><li><a href="/es/docs/v4/" target="_self" rel="noopener noreferrer" class="dropdown__link" lang="es">Español</a></li><li><a href="/fr/docs/v4/" target="_self" rel="noopener noreferrer" class="dropdown__link" lang="fr">Français</a></li><li><a href="/pt-br/docs/v4/" target="_self" rel="noopener noreferrer" class="dropdown__link" lang="pt-br">Português (Brasil)</a></li><li><a href="/zh-CN/docs/v4/" target="_self" rel="noopener noreferrer" class="dropdown__link" lang="zh-CN">䏿ï¼ä¸å½ï¼</a></li></ul></div><a href="https://github.com/socketio/socket.io" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link header-github-link" aria-label="GitHub repository"></a><div class="toggle_vylO colorModeToggle_DEke"><button class="clean-btn toggleButton_gllP toggleButtonDisabled_aARS" type="button" disabled="" title="Switch between dark and light mode (currently light mode)" aria-label="Switch between dark and light mode (currently light mode)" aria-l
17ive="polite"><svg viewBox="0 0 24 24" width="24" height="24" class="lightToggleIcon_pyhR"><path fill="currentColor" d="M12,9c1.65,0,3,1.35,3,3s-1.35,3-3,3s-3-1.35-3-3S10.35,9,12,9 M12,7c-2.76,0-5,2.24-5,5s2.24,5,5,5s5-2.24,5-5 S14.76,7,12,7L12,7z M2,13l2,0c0.55,0,1-0.45,1-1s-0.45-1-1-1l-2,0c-0.55,0-1,0.45-1,1S1.45,13,2,13z M20,13l2,0c0.55,0,1-0.45,1-1 s-0.45-1-1-1l-2,0c-0.55,0-1,0.45-1,1S19.45,13,20,13z M11,2v2c0,0.55,0.45,1,1,1s1-0.45,1-1V2c0-0.55-0.45-1-1-1S11,1.45,11,2z M11,20v2c0,0.55,0.45,1,1,1s1-0.45,1-1v-2c0-0.55-0.45-1-1-1C11.45,19,11,19.45,11,20z M5.99,4.58c-0.39-0.39-1.03-0.39-1.41,0 c-0.39,0.39-0.39,1.03,0,1.41l1.06,1.06c0.39,0.39,1.03,0.39,1.41,0s0.39-1.03,0-1.41L5.99,4.58z M18.36,16.95 c-0.39-0.39-1.03-0.39-1.41,0c-0.39,0.39-0.39,1.03,0,1.41l1.06,1.06c0.39,0.39,1.03,0.39,1.41,0c0.39-0.39,0.39-1.03,0-1.41 L18.36,16.95z M19.42,5.99c0.39-0.39,0.39-1.03,0-1.41c-0.39-0.39-1.03-0.39-1.41,0l-1.06,1.06c-0.39,0.39-0.39,1.03,0,1.41 s1.03,0.39,1.41,0L19.42,5.99z M7.05,18.36c0.39-0.39,0.39-1.03,0-1.41c-0.39-0.39-1.03-0.39-1.41,0l-1.06,1.06 c-0.39,0.39-0.39,1.03,0,1.41s1.03,0.39,1.41,0L7.05,18.36z"></path></svg><svg viewBox="0 0 24 24" width="24" height="24" class="darkToggleIcon_wfgR"><path fill="currentColor" d="M9.37,5.51C9.19,6.15,9.1,6.82,9.1,7.5c0,4.08,3.32,7.4,7.4,7.4c0.68,0,1.35-0.09,1.99-0.27C17.45,17.19,14.93,19,12,19 c-3.86,0-7-3.14-7-7C5,9.07,6.81,6.55,9.37,5.51z M12,3c-4.97,0-9,4.03-9,9s4.03,9,9,9s9-4.03,9-9c0-0.46-0.04-0.92-0.1-1.36 c-0.98,1.37-2.58,2.26-4.4,2.26c-2.98,0-5.4-2.42-5.4-5.4c0-1.81,0.89-3.42,2.26-4.4C12.92,3.04,12.46,3,12,3L12,3z"></path></svg></button></div><div class="searchBox_ZlJk"><button type="button" class="DocSearch DocSearch-Button" aria-label="Search"><span class="DocSearch-Button-Container"><svg width="20" height="20" class="DocSearch-Search-Icon" viewBox="0 0 20 20"><path d="M14.386 14.386l4.0877 4.0877-4.0877-4.0877c-2.9418 2.9419-7.7115 2.9419-10.6533 0-2.9419-2.9418-2.9419-7.7115 0-10.6533 2.9418-2.9419 7.7115-2.9419 10.6533 0 2.9419 2.9418 2.9419 7.7115 0 10.6533z" stroke="currentColor" fill="none" fill-rule="evenodd" stroke-linecap="round" stroke-linejoin="round"></path></svg><span class="DocSearch-Button-Placeholder">Search</span></span><span class="DocSearch-Button-Keys"></span></button></div></div></div><div role="presentation" class="navbar-sidebar__backdrop"></div></nav><div id="__docusaurus_skipToContent_fallback" class="main-wrapper mainWrapper_z2l0 docsWrapper_BCFX"><button aria-label="Scroll back to top" class="clean-btn theme-back-to-top-button backToTopButton_sjWU" type="button"></button><div class="docPage__5DB"><aside class="theme-doc-sidebar-container docSidebarContainer_b6E3"><div class="sidebarViewport_Xe31"><div class="sidebar_njMd sidebarWithHideableNavbar_wUlq"><a tabindex="-1" class="sidebarLogo_isFc" href="/"><img src="/images/logo.svg" alt="Socket.IO logo" class="themedImage_ToTc themedImage--light_HNdA"><img src="/images/logo-dark.svg" alt="Socket.IO logo" class="themedImage_ToTc themedImage--dark_i4oU"><b>Socket.IO</b></a><nav aria-label="Docs sidebar" class="menu thin-scrollbar menu_SIkG menuWithAnnouncementBar_GW3s"><ul class="theme-doc-sidebar-menu menu__list"><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret menu__link--active" aria-expanded="true" href="/docs/v4/">Documentation</a></div><ul style="display:block;overflow:visible;height:auto" class="menu__list"><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link menu__link--active" aria-current="page" tabindex="0" href="/docs/v4/">Introduction</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/v4/how-it-works/">How it works</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/v4/delivery-guarantees">Delivery guarantees</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/v4/connection-state-recovery">Connection state recovery</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/v4/logging-and-debugging/">Logging and debugging</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/v4/testing/">Testing</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/v4/troubleshooting-connection-issues/">Troubleshooting</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/v4/typescript/">TypeScript</a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item"><a class="menu__link" tabindex="0" href="/docs/v4/memory-usage/">Memory usage</a></li></ul></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/v4/server-installation/">Server</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/v4/client-installation/">Client</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/v4/emitting-events/">Events</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/v4/adapter/">Adapters</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/v4/namespaces/">Advanced</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/v4/migrating-from-2-x-to-3-0/">Migrations</a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="menu__link menu__link--sublist menu__link--sublist-caret" aria-expanded="false" href="/docs/v4/faq/">Miscellaneous</a></div></li></ul></nav></div></div></aside><main class="docMainContainer_gTbr"><div class="container padding-top--md padding-bottom--lg"><div class="row"><div class="col docItemCol_VOVn"><div class="docItemContainer_Djhp"><article><nav class="theme-doc-breadcrumbs breadcrumbsContainer_Z_bl" aria-label="Breadcrumbs"><ul class="breadcrumbs" itemscope="" itemtype="https://schema.org/BreadcrumbList"><li class="breadcrumbs__item"><a aria-label="Home page" class="breadcrumbs__link" href="/"><svg viewBox="0 0 24 24" class="breadcrumbHomeIcon_YNFT"><path d="M10 19v-5h4v5c0 .55.45 1 1 1h3c.55 0 1-.45 1-1v-7h1.7c.46 0 .68-.57.33-.87L12.67 3.6c-.38-.34-.96-.34-1.34 0l-8.36 7.53c-.34.3-.13.87.33.87H5v7c0 .55.45 1 1 1h3c.55 0 1-.45 1-1z" fill="currentColor"></path></svg></a></li><li class="breadcrumbs__item"><span class="breadcrumbs__link">Documentation</span><meta itemprop="position" content="1"></li><li itemscope="" itemprop="itemListElement" itemtype="https://schema.org/ListItem" class="breadcrumbs__item breadcrumbs__item--active"><span class="breadcrumbs__link" itemprop="name">Introduction</span><meta itemprop="position" content="2"></li></ul></nav><span class="theme-doc-version-badge badge badge--secondary">Version: 4.x</span><div class="tocCollapsible_ETCw theme-doc-toc-mobile tocMobile_ITEo"><button type="button" class="clean-btn tocCollapsibleButton_TO0P">On this page</button></div><div class="theme-doc-markdown markdown"><header><h1>Introduction</h1></header><div class="theme-admonition theme-admonition-tip alert alert--success admonition_LlT9"><div class="admonitionHeading_tbUL"><span class="admonitionIcon_kALy"><svg viewBox="0 0 12 16"><path fill-rule="evenodd" d="M6.5 0C3.48 0 1 2.19 1 5c0 .92.55 2.25 1 3 1.34 2.25 1.78 2.78 2 4v1h5v-1c.22-1.22.66-1.75 2-4 .45-.75 1-2.08 1-3 0-2.81-2.48-5-5.5-5zm3.64 7.48c-.25.44-.47.8-.67 1.11-.86 1.41-1.25 2.06-1.45 3.23-.02.05-.02.11-.02.17H5c0-.06 0-.13-.02-.17-.2-1.17-.59-1.83-1.45-3.23-.2-.31-.42-.67-.67-1.11C2.44 6.78 2 5.65 2 5c0-2.2 2.02-4 4.5-4 1.22 0 2.36.42 3.22 1.19C10.55 2.94 11 3.94 11 5c0 .66-.44 1.78-.86 2.48zM4 14h5c-.23 1.14-1.3 2-2.5 2s-2.27-.86-2.5-2z"></path></svg></span>tip</div><div class="admonitionContent_S0QG"><p>If you are new to Socket.IO, we recommend checking out our <a href="/docs/v4/tutorial/introduction">
17tutorial</a>.</p></div></div><h2 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="what-socketio-is">What Socket.IO is<a href="#what-socketio-is" class="hash-link" aria-label="Direct link to What Socket.IO is" title="Direct link to What Socket.IO is">â</a></h2><p>Socket.IO is a library that enables <strong>low-latency</strong>, <strong>bidirectional</strong> and <strong>event-based</strong> communication between a client and a server.</p><img src="/images/bidirectional-communication2.png" alt="Diagram of a communication between a server and a client" class="themedImage_ToTc themedImage--light_HNdA"><img src="/images/bidirectional-communication2-dark.png" alt="Diagram of a communication between a server and a client" class="themedImage_ToTc themedImage--dark_i4oU"><p>The Socket.IO connection can be established with different low-level transports:</p><ul><li>HTTP long-polling</li><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API" target="_blank" rel="noopener noreferrer">WebSocket</a></li><li><a href="https://developer.mozilla.org/en-US/docs/Web/API/WebTransport_API" target="_blank" rel="noopener noreferrer">WebTransport</a></li></ul><p>Socket.IO will automatically pick the best available option, depending on:</p><ul><li>the capabilities of the browser (see <a href="https://caniuse.com/websockets" target="_blank" rel="noopener noreferrer">here</a> and <a href="https://caniuse.com/webtransport" target="_blank" rel="noopener noreferrer">here</a>)</li><li>the network (some networks block WebSocket and/or WebTransport connections)</li></ul><p>You can find more details about that in the <a href="/docs/v4/how-it-works/">"How it works" section</a>.</p><h3 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="server-implementations">Server implementations<a href="#server-implementations" class="hash-link" aria-label="Direct link to Server implementations" title="Direct link to Server implementations">â</a></h3><table><thead><tr><th>Language</th><th>Website</th></tr></thead><tbody><tr><td>JavaScript (Node.js)</td><td>- <a href="/docs/v4/server-installation/">Installation steps</a><br>- <a href="/docs/v4/server-api/">API</a><br>- <a href="https://github.com/socketio/socket.io" target="_blank" rel="noopener noreferrer">Source code</a></td></tr><tr><td>JavaScript (Deno)</td><td><a href="https://github.com/socketio/socket.io-deno" target="_blank" rel="noopener noreferrer">https://github.com/socketio/socket.io-deno</a></td></tr><tr><td>Java (Active development)</td><td><a href="https://github.com/socketio4j/netty-socketio" target="_blank" rel="noopener noreferrer">https://github.com/socketio4j/netty-socketio</a></td></tr><tr><td>Java</td><td><a href="https://github.com/mrniko/netty-socketio" target="_blank" rel="noopener noreferrer">https://github.com/mrniko/netty-socketio</a></td></tr><tr><td>Java</td><td><a href="https://github.com/trinopoty/socket.io-server-java" target="_blank" rel="noopener noreferrer">https://github.com/trinopoty/socket.io-server-java</a></td></tr><tr><td>Python</td><td><a href="https://github.com/miguelgrinberg/python-socketio" target="_blank" rel="noopener noreferrer">https://github.com/miguelgrinberg/python-socketio</a></td></tr><tr><td>Golang</td><td><a href="https://github.com/googollee/go-socket.io" target="_blank" rel="noopener noreferrer">https://github.com/googollee/go-socket.io</a></td></tr><tr><td>Rust</td><td><a href="https://github.com/Totodore/socketioxide" target="_blank" rel="noopener noreferrer">https://github.com/Totodore/socketioxide</a></td></tr></tbody></table><h3 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="client-implementations">Client implementations<a href="#client-implementations" class="hash-link" aria-label="Direct link to Client implementations" title="Direct link to Client implementations">â</a></h3><table><thead><tr><th>Language</th><th>Website</th></tr></thead><tbody><tr><td>JavaScript (browser, Node.js or React Native)</td><td>- <a href="/docs/v4/client-installation/">Installation steps</a><br>- <a href="/docs/v4/client-api/">API</a><br>- <a href="https://github.com/socketio/socket.io-client" target="_blank" rel="noopener noreferrer">Source code</a></td></tr><tr><td>JavaScript (for WeChat Mini-Programs)</td><td><a href="https://github.com/weapp-socketio/weapp.socket.io" target="_blank" rel="noopener noreferrer">https://github.com/weapp-socketio/weapp.socket.io</a></td></tr><tr><td>Java</td><td><a href="https://github.com/socketio/socket.io-client-java" target="_blank" rel="noopener noreferrer">https://github.com/socketio/socket.io-client-java</a></td></tr><tr><td>C++</td><td><a href="https://github.com/socketio/socket.io-client-cpp" target="_blank" rel="noopener noreferrer">https://github.com/socketio/socket.io-client-cpp</a></td></tr><tr><td>Swift</td><td><a href="https://github.com/socketio/socket.io-client-swift" target="_blank" rel="noopener noreferrer">https://github.com/socketio/socket.io-client-swift</a></td></tr><tr><td>Dart</td><td><a href="https://github.com/rikulo/socket.io-client-dart" target="_blank" rel="noopener noreferrer">https://github.com/rikulo/socket.io-client-dart</a></td></tr><tr><td>Python</td><td><a href="https://github.com/miguelgrinberg/python-socketio" target="_blank" rel="noopener noreferrer">https://github.com/miguelgrinberg/python-socketio</a></td></tr><tr><td>.Net</td><td><a href="https://github.com/doghappy/socket.io-client-csharp" target="_blank" rel="noopener noreferrer">https://github.com/doghappy/socket.io-client-csharp</a></td></tr><tr><td>Rust</td><td><a href="https://github.com/1c3t3a/rust-socketio" target="_blank" rel="noopener noreferrer">https://github.com/1c3t3a/rust-socketio</a></td></tr><tr><td>Kotlin</td><td><a href="https://github.com/icerockdev/moko-socket-io" target="_blank" rel="noopener noreferrer">https://github.com/icerockdev/moko-socket-io</a></td></tr><tr><td>PHP</td><td><a href="https://github.com/ElephantIO/elephant.io" target="_blank" rel="noopener noreferrer">https://github.com/ElephantIO/elephant.io</a></td></tr><tr><td>Golang</td><td><a href="https://github.com/maldikhan/go.socket.io" target="_blank" rel="noopener noreferrer">https://github.com/maldikhan/go.socket.io</a></td></tr></tbody></table><h2 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="what-socketio-is-not">What Socket.IO is not<a href="#what-socketio-is-not" class="hash-l
17ink" aria-label="Direct link to What Socket.IO is not" title="Direct link to What Socket.IO is not">â</a></h2><div class="theme-admonition theme-admonition-caution alert alert--warning admonition_LlT9"><div class="admonitionHeading_tbUL"><span class="admonitionIcon_kALy"><svg viewBox="0 0 16 16"><path fill-rule="evenodd" d="M8.893 1.5c-.183-.31-.52-.5-.887-.5s-.703.19-.886.5L.138 13.499a.98.98 0 0 0 0 1.001c.193.31.53.501.886.501h13.964c.367 0 .704-.19.877-.5a1.03 1.03 0 0 0 .01-1.002L8.893 1.5zm.133 11.497H6.987v-2.003h2.039v2.003zm0-3.004H6.987V5.987h2.039v4.006z"></path></svg></span>caution</div><div class="admonitionContent_S0QG"><p>Socket.IO is <strong>NOT</strong> a WebSocket implementation.</p></div></div><p>Although Socket.IO indeed uses WebSocket for transport when possible, it adds additional metadata to each packet. That is why a WebSocket client will not be able to successfully connect to a Socket.IO server, and a Socket.IO client will not be able to connect to a plain WebSocket server either.</p><div class="language-js codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-js codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><span class="token comment" style="color:#999988;font-style:italic">// WARNING: the client will NOT be able to connect!</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token keyword" style="color:#00009f">const</span><span class="token plain"> socket </span><span class="token operator" style="color:#393A34">=</span><span class="token plain"> </span><span class="token function" style="color:#d73a49">io</span><span class="token punctuation" style="color:#393A34">(</span><span class="token string" style="color:#e3116c">"ws://echo.websocket.org"</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_y97N"><path fill="currentColor" d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg viewBox="0 0 24 24" class="copyButtonSuccessIcon_LjdS"><path fill="currentColor" d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><p>If you are looking for a plain WebSocket server, please take a look at <a href="https://github.com/websockets/ws" target="_blank" rel="noopener noreferrer">ws</a> or <a href="https://github.com/uNetworking/uWebSockets.js" target="_blank" rel="noopener noreferrer">µWebSockets.js</a>.</p><p>There are also <a href="https://github.com/nodejs/node/issues/19308" target="_blank" rel="noopener noreferrer">discussions</a> for including a WebSocket server in the Node.js core.</p><p>On the client-side, you might be interested in the <a href="https://github.com/nathanboktae/robust-websocket" target="_blank" rel="noopener noreferrer">robust-websocket</a> package.</p><div class="theme-admonition theme-admonition-caution alert alert--warning admonition_LlT9"><div class="admonitionHeading_tbUL"><span class="admonitionIcon_kALy"><svg viewBox="0 0 16 16"><path fill-rule="evenodd" d="M8.893 1.5c-.183-.31-.52-.5-.887-.5s-.703.19-.886.5L.138 13.499a.98.98 0 0 0 0 1.001c.193.31.53.501.886.501h13.964c.367 0 .704-.19.877-.5a1.03 1.03 0 0 0 .01-1.002L8.893 1.5zm.133 11.497H6.987v-2.003h2.039v2.003zm0-3.004H6.987V5.987h2.039v4.006z"></path></svg></span>caution</div><div class="admonitionContent_S0QG"><p>Socket.IO is not meant to be used in a background service for mobile applications.</p></div></div><p>The Socket.IO library keeps an open TCP connection to the server, which may result in a high battery drain for your users. Please use a dedicated messaging platform like <a href="https://firebase.google.com/docs/cloud-messaging" target="_blank" rel="noopener noreferrer">FCM</a> for this use case.</p><h2 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="features">
17Features<a href="#features" class="hash-link" aria-label="Direct link to Features" title="Direct link to Features">â</a></h2><p>Here are the features provided by Socket.IO over plain WebSockets:</p><h3 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="http-long-polling-fallback">HTTP long-polling fallback<a href="#http-long-polling-fallback" class="hash-link" aria-label="Direct link to HTTP long-polling fallback" title="Direct link to HTTP long-polling fallback">â</a></h3><p>The connection will fall back to HTTP long-polling in case the WebSocket connection cannot be established.</p><p>This feature was the #1 reason people used Socket.IO when the project was created more than ten years ago (!), as the browser support for WebSockets was still in its infancy.</p><p>Even if most browsers now support WebSockets (more than <a href="https://caniuse.com/mdn-api_websocket" target="_blank" rel="noopener noreferrer">97%</a>), it is still a great feature as we still receive reports from users that cannot establish a WebSocket connection because they are behind some misconfigured proxy.</p><h3 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="automatic-reconnection">Automatic reconnection<a href="#automatic-reconnection" class="hash-link" aria-label="Direct link to Automatic reconnection" title="Direct link to Automatic reconnection">â</a></h3><p>Under some particular conditions, the WebSocket connection between the server and the client can be interrupted with both sides being unaware of the broken state of the link.</p><p>That's why Socket.IO includes a heartbeat mechanism, which periodically checks the status of the connection.</p><p>And when the client eventually gets disconnected, it automatically reconnects with an exponential back-off delay, in order not to overwhelm the server.</p><h3 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="packet-buffering">Packet buffering<a href="#packet-buffering" class="hash-link" aria-label="Direct link to Packet buffering" title="Direct link to Packet buffering">â</a></h3><p>The packets are automatically buffered when the client is disconnected, and will be sent upon reconnection.</p><p>More information <a href="/docs/v4/client-offline-behavior/#buffered-events">here</a>.</p><h3 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="acknowledgements">Acknowledgements<a href="#acknowledgements" class="hash-link" aria-label="Direct link to Acknowledgements" title="Direct link to Acknowledgements">â</a></h3><p>Socket.IO provides a convenient way to send an event and receive a response:</p><p><em>Sender</em></p><div class="language-js codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-js codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><span class="token plain">socket</span><span class="token punctuation" style="color:#393A34">.</span><span class="token method function property-access" style="color:#d73a49">emit</span><span class="token punctuation" style="color:#393A34">(</span><span class="token string" style="color:#e3116c">"hello"</span><span class="token punctuation" style="color:#393A34">,</span><span class="token plain"> </span><span class="token string" style="color:#e3116c">"world"</span><span class="token punctuation" style="color:#393A34">,</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token parameter">response</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><span class="token arrow operator" style="color:#393A34">=></span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token console class-name">console</span><span class="token punctuation" style="color:#393A34">.</span><span class="token method function property-access" style="color:#d73a49">log</span><span class="token punctuation" style="color:#393A34">(</span><span class="token plain">response</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">// "got it"</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">}</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_y97N">
17<path fill="currentColor" d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg viewBox="0 0 24 24" class="copyButtonSuccessIcon_LjdS"><path fill="currentColor" d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><p><em>Receiver</em></p><div class="language-js codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-js codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><span class="token plain">socket</span><span class="token punctuation" style="color:#393A34">.</span><span class="token method function property-access" style="color:#d73a49">on</span><span class="token punctuation" style="color:#393A34">(</span><span class="token string" style="color:#e3116c">"hello"</span><span class="token punctuation" style="color:#393A34">,</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token parameter">arg</span><span class="token parameter punctuation" style="color:#393A34">,</span><span class="token parameter"> callback</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><span class="token arrow operator" style="color:#393A34">=></span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token console class-name">console</span><span class="token punctuation" style="color:#393A34">.</span><span class="token method function property-access" style="color:#d73a49">log</span><span class="token punctuation" style="color:#393A34">(</span><span class="token plain">arg</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">// "world"</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token function" style="color:#d73a49">callback</span><span class="token punctuation" style="color:#393A34">(</span><span class="token string" style="color:#e3116c">"got it"</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">}</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_y97N"><path fill="currentColor" d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg viewBox="0 0 24 24" class="copyButtonSuccessIcon_LjdS"><path fill="currentColor" d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><p>You can also add a timeout:</p><div class="language-js codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-js codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><span class="token plain">socket</span><span class="token punctuation" style="color:#393A34">.</span><span class="token method function property-access" style="color:#d73a49">timeout</span><span class="token punctuation" style="color:#393A34">(</span><span class="token number" style="color:#36acaa">5000</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">.</span><span class="token method function property-access" style="color:#d73a49">emit</span><span class="token punctuation" style="color:#393A34">(</span><span class="token string" style="color:#e3116c">"hello"</span><span class="token punctuation" style="color:#393A34">,</span><span class="token plain"> </span><span class="token string" style="color:#e3116c">"world"</span><span class="token punctuation" style="color:#393A34">,</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token parameter">err</span><span class="token parameter punctuation" style="color:#393A34">,</span><span class="token parameter"> response</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><span class="token arrow operator" style="color:#393A34">=></span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">
17{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token keyword control-flow" style="color:#00009f">if</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token plain">err</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">// the other side did not acknowledge the event in the given delay</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">}</span><span class="token plain"> </span><span class="token keyword control-flow" style="color:#00009f">else</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token console class-name">console</span><span class="token punctuation" style="color:#393A34">.</span><span class="token method function property-access" style="color:#d73a49">log</span><span class="token punctuation" style="color:#393A34">(</span><span class="token plain">response</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">// "got it"</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">}</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">}</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_y97N"><path fill="currentColor" d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg viewBox="0 0 24 24" class="copyButtonSuccessIcon_LjdS"><path fill="currentColor" d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><h3 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="broadcasting">Broadcasting<a href="#broadcasting" class="hash-link" aria-label="Direct link to Broadcasting" title="Direct link to Broadcasting">â</a></h3><p>On the server-side, you can send an event to <a href="/docs/v4/broadcasting-events/">all connected clients</a> or <a href="/docs/v4/rooms/">to a subset of clients</a>:</p><div class="language-js codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-js codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><span class="token comment" style="color:#999988;font-style:italic">// to all connected clients</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">io</span><span class="token punctuation" style="color:#393A34">.</span><span class="token method function property-access" style="color:#d73a49">emit</span><span class="token punctuation" style="color:#393A34">(</span><span class="token string" style="color:#e3116c">"hello"</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token comment" style="color:#999988;font-style:italic">// to all connected clients in the "news" room</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">io</span><span class="token punctuation" style="color:#393A34">.</span><span class="token method function property-access" style="color:#d73a49">to</span><span class="token punctuation" style="color:#393A34">(</span><span class="token string" style="color:#e3116c">"news"</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">.</span><span class="token method function property-access" style="color:#d73a49">emit</span><span class="token punctuation" style="color:#393A34">(</span><span class="token string" style="color:#e3116c">"hello"</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_y97N">
17<path fill="currentColor" d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg viewBox="0 0 24 24" class="copyButtonSuccessIcon_LjdS"><path fill="currentColor" d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><p>This also works when <a href="/docs/v4/using-multiple-nodes/">scaling to multiple nodes</a>.</p><h3 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="multiplexing">Multiplexing<a href="#multiplexing" class="hash-link" aria-label="Direct link to Multiplexing" title="Direct link to Multiplexing">â</a></h3><p>Namespaces allow you to split the logic of your application over a single shared connection. This can be useful for example if you want to create an "admin" channel that only authorized users can join.</p><div class="language-js codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:#393A34;--prism-background-color:#f6f8fa"><div class="codeBlockContent_biex"><pre tabindex="0" class="prism-code language-js codeBlock_bY9V thin-scrollbar"><code class="codeBlockLines_e6Vv"><span class="token-line" style="color:#393A34"><span class="token plain">io</span><span class="token punctuation" style="color:#393A34">.</span><span class="token method function property-access" style="color:#d73a49">on</span><span class="token punctuation" style="color:#393A34">(</span><span class="token string" style="color:#e3116c">"connection"</span><span class="token punctuation" style="color:#393A34">,</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token parameter">socket</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><span class="token arrow operator" style="color:#393A34">=></span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">// classic users</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">}</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain" style="display:inline-block"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain">io</span><span class="token punctuation" style="color:#393A34">.</span><span class="token method function property-access" style="color:#d73a49">of</span><span class="token punctuation" style="color:#393A34">(</span><span class="token string" style="color:#e3116c">"/admin"</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">.</span><span class="token method function property-access" style="color:#d73a49">on</span><span class="token punctuation" style="color:#393A34">(</span><span class="token string" style="color:#e3116c">"connection"</span><span class="token punctuation" style="color:#393A34">,</span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">(</span><span class="token parameter">socket</span><span class="token punctuation" style="color:#393A34">)</span><span class="token plain"> </span><span class="token arrow operator" style="color:#393A34">=></span><span class="token plain"> </span><span class="token punctuation" style="color:#393A34">{</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"> </span><span class="token comment" style="color:#999988;font-style:italic">// admin users</span><span class="token plain"></span><br></span><span class="token-line" style="color:#393A34"><span class="token plain"></span><span class="token punctuation" style="color:#393A34">}</span><span class="token punctuation" style="color:#393A34">)</span><span class="token punctuation" style="color:#393A34">;</span><br></span></code></pre><div class="buttonGroup__atx"><button type="button" aria-label="Copy code to clipboard" title="Copy" class="clean-btn"><span class="copyButtonIcons_eSgA" aria-hidden="true"><svg viewBox="0 0 24 24" class="copyButtonIcon_y97N">
17<path fill="currentColor" d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svg viewBox="0 0 24 24" class="copyButtonSuccessIcon_LjdS"><path fill="currentColor" d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div><p>More on that <a href="/docs/v4/namespaces/">here</a>.</p><h2 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="common-questions">Common questions<a href="#common-questions" class="hash-link" aria-label="Direct link to Common questions" title="Direct link to Common questions">â</a></h2><h3 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="is-socketio-still-needed-today">Is Socket.IO still needed today?<a href="#is-socketio-still-needed-today" class="hash-link" aria-label="Direct link to Is Socket.IO still needed today?" title="Direct link to Is Socket.IO still needed today?">â</a></h3><p>That's a fair question, since WebSockets are supported <a href="https://caniuse.com/mdn-api_websocket" target="_blank" rel="noopener noreferrer">almost everywhere</a> now.</p><p>That being said, we believe that, if you use plain WebSockets for your application, you will eventually need to implement most of the features that are already included (and battle-tested) in Socket.IO, like <a href="#automatic-reconnection">reconnection</a>, <a href="#acknowledgements">acknowledgements</a> or <a href="#broadcasting">broadcasting</a>.</p><h3 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="what-is-the-overhead-of-the-socketio-protocol">What is the overhead of the Socket.IO protocol?<a href="#what-is-the-overhead-of-the-socketio-protocol" class="hash-link" aria-label="Direct link to What is the overhead of the Socket.IO protocol?" title="Direct link to What is the overhead of the Socket.IO protocol?">â</a></h3><p><code>socket.emit("hello", "world")</code> will be sent as a single WebSocket frame containing <code>42["hello","world"]</code> with:</p><ul><li><code>4</code> being Engine.IO "message" packet type</li><li><code>2</code> being Socket.IO "message" packet type</li><li><code>["hello","world"]</code> being the <code>JSON.stringify()</code>-ed version of the arguments array</li></ul><p>So, a few additional bytes for each message, which can be further reduced by the usage of a <a href="/docs/v4/custom-parser/">custom parser</a>.</p><div class="theme-admonition theme-admonition-info alert alert--info admonition_LlT9"><div class="admonitionHeading_tbUL"><span class="admonitionIcon_kALy"><svg viewBox="0 0 14 16"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg></span>info</div><div class="admonitionContent_S0QG"><p>The size of the browser bundle itself is <a href="https://bundlephobia.com/package/socket.io-client" target="_blank" rel="noopener noreferrer"><code>10.4 kB</code></a> (minified and gzipped).</p></div></div><p>You can find the details of the Socket.IO protocol <a href="/docs/v4/socket-io-protocol/">here</a>.</p><h3 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="something-does-not-work-properly-please-help">Something does not work properly, please help?<a href="#something-does-not-work-properly-please-help" class="hash-link" aria-label="Direct link to Something does not work properly, please help?" title="Direct link to Something does not work properly, please help?">â</a></h3><p>Please check our <a href="/docs/v4/troubleshooting-connection-issues/">Troubleshooting guide</a>.</p><h2 class="anchor anchorWithHideOnScrollNavbar_WYt5" id="next-steps">Next steps<a href="#next-steps" class="hash-link" aria-label="Direct link to Next steps" title="Direct link to Next steps">â</a></h2><ul><li><a href="/get-started/chat">Get started example</a></li><li><a href="/docs/v4/server-installation/">Server installation</a></li><li><a href="/docs/v4/client-installation/">Client installation</a></li></ul></div><footer class="theme-doc-footer docusaurus-mt-lg"><div class="theme-doc-footer-edit-meta-row row"><div class="col"><a href="https://github.com/socketio/socket.io-website/edit/main/docs/categories/01-Documentation/index.md" target="_blank" rel="noreferrer noopener" class="theme-edit-this-page"><svg fill="currentColor" height="20" width="20" viewBox="0 0 40 40" class="iconEdit_Z9Sw" aria-hidden="true"><g><path d="m34.5 11.7l-3 3.1-6.3-6.3 3.1-3q0.5-0.5 1.2-0.5t1.1 0.5l3.9 3.9q0.5 0.4 0.5 1.1t-0.5 1.2z m-29.5 17.1l18.4-18.5 6.3 6.3-18.4 18.4h-6.3v-6.2z"></path></g></svg>Edit this page</a></div><div class="col lastUpdated_vwxv"><span class="theme-last-updated">Last updated<!-- --> on <b><time datetime="2026-07-21T14:24:54.000Z">Jul 21, 2026</time></b></span></div></div></footer></article><nav class="pagination-nav docusaurus-mt-lg" aria-label="Docs pages"><a class="pagination-nav__link pagination-nav__link--next" href="/docs/v4/how-it-works/"><div class="pagination-nav__sublabel">Next</div><div class="pagination-nav__label">How it works</div></a></nav></div></div><div class="col col--3"><div class="tableOfContents_bqdL thin-scrollbar theme-doc-toc-desktop"><ul class="table-of-contents table-of-contents__left-border"><li><a href="#what-socketio-is" class="table-of-contents__link toc-highlight">What Socket.IO is</a><ul><li><a href="#server-implementations" class="table-of-contents__link toc-highlight">Server implementations</a></li><li><a href="#client-implementations" class="table-of-contents__link toc-highlight">Client implementations</a></li></ul></li><li><a href="#what-socketio-is-not" class="table-of-contents__link toc-highlight">What Socket.IO is not</a></li><li><a href="#features" class="table-of-contents__link toc-highlight">Features</a><ul><li><a href="#http-long-polling-fallback" class="table-of-contents__link toc-highlight">HTTP long-polling fallback</a></li><li><a href="#automatic-reconnection" class="table-of-contents__link toc-highlight">Automatic reconnection</a></li><li><a href="#packet-buffering" class="table-of-contents__link toc-highlight">Packet buffering</a></li><li><a href="#acknowledgements" class="table-of-contents__link toc-highlight">Acknowledgements</a></li><li><a href="#broadcasting" class="table-of-contents__link toc-highlight">Broadcasting</a></li><li><a href="#multiplexing" class="table-of-contents__link toc-highlight">Multiplexing</a></li></ul></li><li><a href="#common-questions" class="table-of-contents__link toc-highlight">Common questions</a><ul><li><a href="#is-socketio-still-needed-today" class="table-of-contents__link toc-highlight">Is Socket.IO still needed today?</a></li><li><a href="#what-is-the-overhead-of-the-socketio-protocol" class="table-of-contents__link toc-highlight">What is the overhead of the Socket.IO protocol?</a></li><li><a href="#something-does-not-work-properly-please-help" class="table-of-contents__link toc-highlight">Something does not work properly, please help?</a></li></ul></li><li><a href="#next-steps" class="table-of-contents__link toc-highlight">Next steps</a></li></ul></div></div></div></div></main></div></div><footer class="footer footer--dark"><div class="container container-fluid"><div class="row footer__links">
17<div class="col footer__col"><div class="footer__title">Documentation</div><ul class="footer__items clean-list"><li class="footer__item"><a class="footer__link-item" href="/docs/v4/">Guide</a></li><li class="footer__item"><a class="footer__link-item" href="/docs/v4/tutorial/introduction">Tutorial</a></li><li class="footer__item"><a class="footer__link-item" href="/get-started/">Examples</a></li><li class="footer__item"><a class="footer__link-item" href="/docs/v4/server-api/">Server API</a></li><li class="footer__item"><a class="footer__link-item" href="/docs/v4/client-api/">Client API</a></li></ul></div><div class="col footer__col"><div class="footer__title">Help</div><ul class="footer__items clean-list"><li class="footer__item"><a class="footer__link-item" href="/docs/v4/troubleshooting-connection-issues/">Troubleshooting</a></li><li class="footer__item"><a href="https://stackoverflow.com/questions/tagged/socket.io" target="_blank" rel="noopener noreferrer" class="footer__link-item">Stack Overflow<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li class="footer__item"><a href="https://github.com/socketio/socket.io/discussions" target="_blank" rel="noopener noreferrer" class="footer__link-item">GitHub Discussions<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li class="footer__item"><a href="https://socketio-slackin.herokuapp.com/" target="_blank" rel="noopener noreferrer" class="footer__link-item">Slack<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li></ul></div><div class="col footer__col"><div class="footer__title">News</div><ul class="footer__items clean-list"><li class="footer__item"><a class="footer__link-item" href="/blog">Blog</a></li><li class="footer__item"><a href="https://twitter.com/SocketIO" target="_blank" rel="noopener noreferrer" class="footer__link-item">Twitter<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li></ul></div><div class="col footer__col"><div class="footer__title">Tools</div><ul class="footer__items clean-list"><li class="footer__item"><a href="https://cdn.socket.io" target="_blank" rel="noopener noreferrer" class="footer__link-item">CDN<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li class="footer__item"><a href="https://admin.socket.io" target="_blank" rel="noopener noreferrer" class="footer__link-item">Admin UI<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li></ul></div><div class="col footer__col"><div class="footer__title">About</div><ul class="footer__items clean-list"><li class="footer__item"><a class="footer__link-item" href="/docs/v4/faq/">FAQ</a></li><li class="footer__item"><a class="footer__link-item" href="/docs/v4/changelog/">Changelog</a></li><li class="footer__item"><a href="https://github.com/orgs/socketio/projects/3" target="_blank" rel="noopener noreferrer" class="footer__link-item">Roadmap<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU">
17<path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li><li class="footer__item"><a href="https://opencollective.com/socketio" target="_blank" rel="noopener noreferrer" class="footer__link-item">Become a sponsor<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg></a></li></ul></div></div><div class="footer__bottom text--center"><div class="footer__copyright">Copyright © 2026 Socket.IO</div></div></div></footer></div>
18<script src="/assets/js/runtime~main.b93ab2a6.js"></script>
vendor: 1 bytes, line 18
18
19<script src="/assets/js/main.a60ddce9.js"></script>
19 20</body> 21</html>
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.