1"use strict";(self.webpackChunksample_website=self.webpackChunksample_website||[]).push([[6967],{3905:(e,t,n)=>{n.d(t,{Zo:()=>d,kt:()=>g});var a=n(7294);function o(e,t,n){return t in e?Object.defineProperty(e,t,{value:n,enumerable:!0,configurable:!0,writable:!0}):e[t]=n,e}function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var a=Object.getOwnPropertySymbols(e);t&&(a=a.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,a)}return n}function s(e){for(var t=1;t<arguments.length;t++){var n=null!=arguments[t]?arguments[t]:{};t%2?r(Object(n),!0).forEach((function(t){o(e,t,n[t])})):Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(n)):r(Object(n)).forEach((function(t){Object.defineProperty(e,t,Object.getOwnPropertyDescriptor(n,t))}))}return e}function i(e,t){if(null==e)return{};var n,a,o=function(e,t){if(null==e)return{};var n,a,o={},r=Object.keys(e);for(a=0;a<r.length;a++)n=r[a],t.indexOf(n)>=0||(o[n]=e[n]);return o}(e,t);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);for(a=0;a<r.length;a++)n=r[a],t.indexOf(n)>=0||Object.prototype.propertyIsEnumerable.call(e,n)&&(o[n]=e[n])}return o}var c=a.createContext({}),l=function(e){var t=a.useContext(c),n=t;return e&&(n="function"==typeof e?e(t):s(s({},t),e)),n},d=function(e){var t=l(e.components);return a.createElement(c.Provider,{value:t},e.children)},p="mdxType",m={inlineCode:"code",wrapper:function(e){var t=e.children;return a.createElement(a.Fragment,{},t)}},u=a.forwardRef((function(e,t){var n=e.components,o=e.mdxType,r=e.originalType,c=e.parentName,d=i(e,["components","mdxType","originalType","parentName"]),p=l(n),u=o,g=p["".concat(c,".").concat(u)]||p[u]||m[u]||r;return n?a.createElement(g,s(s({ref:t},d),{},{components:n})):a.createElement(g,s({ref:t},d))}));function g(e,t){var n=arguments,o=t&&t.mdxType;if("string"==typeof e||o){var r=n.length,s=new Array(r);s[0]=u;var i={};for(var c in t)hasOwnProperty.call(t,c)&&(i[c]=t[c]);i.originalType=e,i[p]="string"==typeof e?e:o,s[1]=i;for(var l=2;l<r;l++)s[l]=n[l];return a.createElement.apply(null,s)}return a.createElement.apply(null,n)}u.displayName="MDXCreateElement"},8440:(e,t,n)=>{n.r(t),n.d(t,{assets:()=>d,contentTitle:()=>c,default:()=>g,frontMatter:()=>i,metadata:()=>l,toc:()=>p});var a=n(3117),o=(n(7294),n(3905)),r=n(941),s=n(4996);const i={title:"Broadcasting events",sidebar_position:3,slug:"/broadcasting-events/"},c=void 0,l={unversionedId:"categories/Events/broadcasting-events",id:"version-3.x/categories/Events/broadcasting-events",title:"Broadcasting events",description:"Socket.IO makes it easy to send events to all the connected clients.",source:"@site/versioned_docs/version-3.x/categories/04-Events/broadcasting-events.md",sourceDirName:"categories/04-Events",slug:"/broadcasting-events/",permalink:"/docs/v3/broadcasting-events/",draft:!1,editUrl:"https://github.com/socketio/socket.io-website/edit/main/versioned_docs/version-3.x/categories/04-Events/broadcasting-events.md",tags:[],version:"3.x",lastUpdatedAt:1784117104,formattedLastUpdatedAt:"Jul 15, 2026",sidebarPosition:3,frontMatter:{title:"Broadcasting events",sidebar_position:3,slug:"/broadcasting-events/"},sidebar:"version-3.x/sidebar",previous:{title:"Listening to events",permalink:"/docs/v3/listening-to-events/"},next:{title:"Rooms",permalink:"/docs/v3/rooms/"}},d={},p=[{value:"To all connected clients",id:"to-all-connected-clients",level:2},{value:"To all connected clients except the sender",id:"to-all-connected-clients-except-the-sender",level:2},{value:"With multiple Socket.IO servers",id:"with-multiple-socketio-servers",level:2}],m={toc:p},u="wrapper";function g(e){let{components:t,...n}=e;return(0,o.kt)(u,(0,a.Z)({},m,n,{components:t,mdxType:"MDXLayout"}),(0,o.kt)("p",null,"Socket.IO makes it easy to send events to all the connected clients."),(0,o.kt)("admonition",{type:"info"},(0,o.kt)("p",{parentName:"admonition"},"Please note that broadcasting is a ",(0,o.kt)("strong",{parentName:"p"},"server-only")," feature.")),(0,o.kt)("h2",{id:"to-all-connected-clients"},"To all connected clients"),(0,o.kt)(r.Z,{alt:"Broadcasting to all connected clients",sources:{light:(0,s.Z)("/images/broadcasting.png"),dark:(0,s.Z)("/images/broadcasting-dark.png")},mdxType:"ThemedImage"}),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre",className:"language-js"},'io.emit("hello", "world");\n')),(0,o.kt)("admonition",{type:"caution"},(0,o.kt)("p",{parentName:"admonition"},"Clients that are currently disconnected (or in the process of reconnecting) won't receive the event. Storing this event somewhere (in a database, for example) is up to you, depending on your use case.")),(0,o.kt)("h2",{id:"to-all-connected-clients-except-the-sender"},"To all connected clients except the sender"),(0,o.kt)(r.Z,{alt:"Broadcasting to all connected clients excepting the sender",sources:{light:(0,s.Z)("/images/broadcasting2.png"),dark:(0,s.Z)("/images/broadcasting2-dark.png")},mdxType:"ThemedImage"}),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre",className:"language-js"},'io.on("connection", (socket) => {\n socket.broadcast.emit("hello", "world");\n});\n')),(0,o.kt)("admonition",{type:"note"},(0,o.kt)("p",{parentName:"admonition"},"In the example above, using ",(0,o.kt)("inlineCode",{parentName:"p"}
1,'socket.emit("hello", "world")')," (without ",(0,o.kt)("inlineCode",{parentName:"p"},"broadcast"),' flag) would send the event to "client A". You can find the list of all the ways to send an event in the ',(0,o.kt)("a",{parentName:"p",href:"/docs/v3/emit-cheatsheet/"},"cheatsheet"),".")),(0,o.kt)("h2",{id:"with-multiple-socketio-servers"},"With multiple Socket.IO servers"),(0,o.kt)("p",null,"Broadcasting also works with multiple Socket.IO servers."),(0,o.kt)("p",null,"You just need to replace the default ",(0,o.kt)("a",{parentName:"p",href:"/docs/v3/glossary/#Adapter"},"Adapter")," by the Redis Adapter. More information about it ",(0,o.kt)("a",{parentName:"p",href:"/docs/v3/using-multiple-nodes/#Passing-events-between-nodes"},"here"),"."),(0,o.kt)(r.Z,{alt:"Broadcasting with Redis",sources:{light:(0,s.Z)("/images/broadcasting-redis.png"),dark:(0,s.Z)("/images/broadcasting-redis-dark.png")},mdxType:"ThemedImage"}),(0,o.kt)("p",null,"In certain cases, you may want to only broadcast to clients that are connected to the current server. You can achieve this with the ",(0,o.kt)("inlineCode",{parentName:"p"},"local")," flag:"),(0,o.kt)("pre",null,(0,o.kt)("code",{parentName:"pre",className:"language-js"},'io.local.emit("hello", "world");\n')),(0,o.kt)(r.Z,{alt:"Broadcasting with Redis but local",sources:{light:(0,s.Z)("/images/broadcasting-redis-local.png"),dark:(0,s.Z)("/images/broadcasting-redis-local-dark.png")},mdxType:"ThemedImage"}),(0,o.kt)("p",null,"In order to target specific clients when broadcasting, please see the documentation about ",(0,o.kt)("a",{parentName:"p",href:"/docs/v3/rooms/"},"Rooms"),"."))}g.isMDXComponent=!0}}]);
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.