{"id":16182269,"url":"https://github.com/avraammavridis/react-window-communication-hook","last_synced_at":"2025-03-16T10:31:41.666Z","repository":{"id":55480904,"uuid":"155087980","full_name":"AvraamMavridis/react-window-communication-hook","owner":"AvraamMavridis","description":"React hook to communicate among browser context (tabs, windows, iframes)","archived":false,"fork":false,"pushed_at":"2020-12-28T06:01:29.000Z","size":2496,"stargazers_count":92,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-03-16T01:32:38.605Z","etag":null,"topics":["communication","hooks","iframe","react","tab","window"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AvraamMavridis.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-10-28T15:50:48.000Z","updated_at":"2025-03-04T09:27:11.000Z","dependencies_parsed_at":"2022-08-15T01:20:25.504Z","dependency_job_id":null,"html_url":"https://github.com/AvraamMavridis/react-window-communication-hook","commit_stats":null,"previous_names":["avraammavridis/react-context-communication-hook"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AvraamMavridis%2Freact-window-communication-hook","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AvraamMavridis%2Freact-window-communication-hook/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AvraamMavridis%2Freact-window-communication-hook/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AvraamMavridis%2Freact-window-communication-hook/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AvraamMavridis","download_url":"https://codeload.github.com/AvraamMavridis/react-window-communication-hook/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243858872,"owners_count":20359257,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["communication","hooks","iframe","react","tab","window"],"created_at":"2024-10-10T06:29:58.768Z","updated_at":"2025-03-16T10:31:41.643Z","avatar_url":"https://github.com/AvraamMavridis.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# react-window-communication-hook\r\n\r\nReact hook to communicate among browsers contexts (windows, tabs, iframes).\r\n\r\nExample use case: When the user presses logout in one tab, logout from every other tab\r\n\r\n\r\n## How to use it\r\n\r\nImport\r\n\r\n```\r\nimport useBrowserContextCommunication from 'react-window-communication-hook';\r\n```\r\n\r\npass a channel name\r\n\r\n```\r\nconst [communicationState, postMessage] = useBrowserContextCommunication(\"myGreatChannel\");\r\n```\r\n\r\n`communicationState` contains `lastMessage` and `messages` which is an array of the messages that where send from other tabs/windows to the current one.\r\n\r\nUse `postMessage` to send messages to the other browser contextes (windows, tabs, iframes)\r\n\r\n\r\n# Example\r\n\r\n```js\r\nimport useBrowserContextCommunication from 'react-window-communication-hook';\r\n\r\n\r\nfunction App() {\r\n  // state ({lastMessage,messages}) that comes from other browser context\r\n  const [communicationState, postMessage] = useBrowserContextCommunication(\"channel\");\r\n  // Tabs, Windows etc are not listening to there own messages so\r\n  // we need an extra local state\r\n  const [status, setStatus] = useState(\"login\");\r\n\r\n  function logout() {\r\n    setStatus(\"logout\");\r\n    postMessage(\"logout\");\r\n  }\r\n\r\n  const shouldLogout = [communicationState.lastMessage, status].includes('logout');\r\n\r\n  return (\r\n    \u003cdiv className=\"App\" \u003e\r\n      \u003ch1\u003e{shouldLogout ? 'Logged Out' : 'Logged In' }\u003c/h1\u003e\r\n      \u003cbutton onClick={logout}\u003eLogout\u003c/button\u003e\r\n    \u003c/div\u003e\r\n  );\r\n}\r\n```\r\n\r\n\u003cimg src=\"https://github.com/AvraamMavridis/react-window-communication-hook/blob/master/demo_gif.gif\" /\u003e\r\n\r\n\r\n# License\r\n\r\nMIT\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Favraammavridis%2Freact-window-communication-hook","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Favraammavridis%2Freact-window-communication-hook","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Favraammavridis%2Freact-window-communication-hook/lists"}