{"id":22544118,"url":"https://github.com/samhuk/sock-state","last_synced_at":"2026-02-24T02:07:22.969Z","repository":{"id":65840812,"uuid":"599061437","full_name":"samhuk/sock-state","owner":"samhuk","description":"Redux-like state container over Web Sockets","archived":false,"fork":false,"pushed_at":"2023-08-12T12:44:39.000Z","size":171,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-04-09T23:50:33.997Z","etag":null,"topics":["redux","state-management","typescript","websocket"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/samhuk.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"contributing/development.md","funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null},"funding":{"github":["samhuk"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2023-02-08T11:23:24.000Z","updated_at":"2023-08-14T14:31:03.000Z","dependencies_parsed_at":"2024-01-18T00:30:49.363Z","dependency_job_id":"420c1ae5-1a45-44e4-9f52-0955ba4c0970","html_url":"https://github.com/samhuk/sock-state","commit_stats":{"total_commits":43,"total_committers":1,"mean_commits":43.0,"dds":0.0,"last_synced_commit":"5d0bdc9c620bcc91c671aa8052d43145fcece6b7"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":"samhuk/ts-npm-package-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samhuk%2Fsock-state","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samhuk%2Fsock-state/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samhuk%2Fsock-state/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/samhuk%2Fsock-state/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/samhuk","download_url":"https://codeload.github.com/samhuk/sock-state/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248131454,"owners_count":21052819,"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":["redux","state-management","typescript","websocket"],"created_at":"2024-12-07T14:06:13.428Z","updated_at":"2025-10-30T09:37:08.277Z","avatar_url":"https://github.com/samhuk.png","language":"TypeScript","funding_links":["https://github.com/sponsors/samhuk","https://www.buymeacoffee.com/samhuk"],"categories":[],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003eSock State\u003c/h1\u003e\n\u003cp align=\"center\"\u003e\n  \u003cem\u003eRedux-like state container over Web Sockets\u003c/em\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://github.com/samhuk/sock-state/actions/workflows/ci.yaml/badge.svg\" target=\"_blank\"\u003e\n    \u003cimg src=\"https://github.com/samhuk/sock-state/actions/workflows/ci.yaml/badge.svg\" alt=\"ci status\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://img.shields.io/badge/License-MIT-green.svg\" target=\"_blank\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/License-MIT-green.svg\" alt=\"license\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://badge.fury.io/js/sock-state.svg\" target=\"_blank\"\u003e\n    \u003cimg src=\"https://badge.fury.io/js/sock-state.svg\" alt=\"npm version\" /\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n## Overview\n\nSock State allows you to create [Redux](https://redux.js.org/)-like stores that can be updated and subscribed to over [Web Sockets](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API).\n\nThis enables distributed computing with Javascript without having to rely on workarounds such as shared stdout streams, HTTP long-polling, and so forth.\n\n## Usage Overview\n\nCreate a **Reducer** just like you do with Redux:\n\n```typescript\nexport const chatAppReducer = (state, action) =\u003e {\n  if (state == null)\n    return { messages: [] }\n\n  switch (action.type) {\n    case 'addMessage':\n      return { messages: state.messages.concat(action.payload) }\n    default:\n      return state\n  }\n}\n```\n\nCreate a **Store Server** with a **Topic** that uses the Reducer:\n\n```typescript\nimport { createStoreServer } from 'sock-state'\n\nconst server = createStoreServer({\n  host: 'localhost',\n  port: 4000,\n  topics: { chatApp: { reducer: chatAppReducer } },\n})\n```\n\nCreate **Store Clients** and connect them to the Store Server:\n\n```typescript\nimport { createNodeStoreClient } from 'sock-state/lib/client/node'\n\nconst addMessage = message =\u003e ({\n  type: 'addMessage',\n  payload: message,\n})\n\nconst client1 = createNodeStoreClient({ host: 'localhost', port: 4000 })\nconst client2 = createNodeStoreClient({ host: 'localhost', port: 4000 })\n\nclient1.connect()\nclient2.connect()\n```\n\nSubscribe Store Clients to Topics (using the Reducer), and listen for Topic events (e.g. state changes):\n\n```typescript\nconst client1Topic = storeClient.topic('chatApp')\nconst client2Topic = storeClient.topic('chatApp')\n\nclient1Topic.on('state-change', chatAppReducer, state =\u003e {\n  console.log('client 1 new state:', state.messages)\n})\nclient2Topic.on('state-change', chatAppReducer, state =\u003e {\n  console.log('client 2 new state:', state.messages)\n})\n```\n\nDispatch actions to Topics to update their state:\n\n```typescript\nconst addMessage = message =\u003e ({\n  type: 'addMessage',\n  payload: message,\n})\n\nclient2Topic.dispatch(addMessage('Hello Client 2'))\nclient2Topic.dispatch(addMessage('Hello Client 1'))\n```\n\n### Dynamic Topics\n\nTopics can be added to and removed from the server. Any clients subscribed to a topic that is deleted will be notified and automatically unsusbcribe from it. Basic example:\n\n```typescript\nconst server = createStoreServer({\n  host: 'localhost',\n  port: 4000,\n})\nserver.addTopic({ name: 'chatApp', reducer: ... })\nserver.deleteTopic('chatApp')\n```\n\n### Accepting/Rejecting Connections\n\nConnections to the server can be accepted or rejected conditionally. This can be used for basic authentication. Basic example:\n\n```typescript\nconst server = createStoreServer({\n  host: 'localhost',\n  port: 4000,\n  connectionAcceptor: (webSocket, request) =\u003e {\n    const isIpAllowed = checkIp(request.socket.remoteAddress)\n    return {\n      accepted: isIpAllowed,\n      reason: isIpAllowed ? undefined : 'IP Address banned',\n    }\n  }\n})\n```\n\n### Event Listening\n\nVarious events can be listened for. Basic example:\n\n```typescript\nconst storeServer = createStoreServer({\n  host: 'localhost',\n  port: 4000,\n  reporter: {\n    ...,\n    onClientAccepted: client =\u003e console.log(`Client ${client.shortUuid} connected (IP: ${client.req.socket.remoteAddress}).`),\n    ...\n  },\n})\n\nstoreServer.server.on('message', (msgData, client) =\u003e {\n  console.log(`Message recieved from client ${client.shortUuid}: ${msgData}`)\n})\n```\n\n## Examples\n\n[./examples/chat-app](./examples/chat-app) - A basic chat app. To run this, clone this repository and run `npm i` then `npm run chat-app` (requires Node.js and npm).\n\n## Development\n\nSee [./contributing/development.md](./contributing/development.md)\n\n---\n\nIf you would like to support the development of Sock State, feel free to [sponsor me on GitHub](https://github.com/sponsors/samhuk) ❤️ or [buy me a coffee](https://www.buymeacoffee.com/samhuk) ✨\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamhuk%2Fsock-state","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsamhuk%2Fsock-state","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsamhuk%2Fsock-state/lists"}