{"id":15924720,"url":"https://github.com/harkal/websocket-demux","last_synced_at":"2025-04-03T12:47:25.331Z","repository":{"id":57143720,"uuid":"306977884","full_name":"harkal/websocket-demux","owner":"harkal","description":"Demultiplexes messages over a websocket in a straitforward request handling fashion","archived":false,"fork":false,"pushed_at":"2020-10-25T10:11:17.000Z","size":2,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-09T02:17:14.831Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/harkal.png","metadata":{"files":{"readme":"README.md","changelog":null,"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":"2020-10-24T21:51:22.000Z","updated_at":"2022-01-24T08:42:09.000Z","dependencies_parsed_at":"2022-09-06T00:11:08.624Z","dependency_job_id":null,"html_url":"https://github.com/harkal/websocket-demux","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harkal%2Fwebsocket-demux","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harkal%2Fwebsocket-demux/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harkal%2Fwebsocket-demux/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/harkal%2Fwebsocket-demux/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/harkal","download_url":"https://codeload.github.com/harkal/websocket-demux/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247005473,"owners_count":20868019,"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":[],"created_at":"2024-10-06T21:40:39.578Z","updated_at":"2025-04-03T12:47:25.284Z","avatar_url":"https://github.com/harkal.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\nWebsocket Demultiplexer\n=======================\n\nThis simple library handles the demultiplexing of requests/responces over a single websocket connection (or not).\n\nThe only prerequisite for the server is to be able to mark respoinces with a context value so the matchmaking can be performed. \n\nUsage\n---\n\nBelow is an example code of how this can be used:\n\n```javascript\nconst demux = require('websocket-demux')\nconst WebSocket = require('ws')\n\ndemux.init()\n\nconst ws = new WebSocket('wss://echo.websocket.org/', {\n  origin: 'https://websocket.org'\n})\n\nws.on('open', function open() {\n  console.log('connected')\n  demux.request(ws, { time: Date.now() }, (err, res)=\u003e{\n    if(err) {\n      console.log('Error: ', err)\n      return\n    }\n\n    console.log('Roundtrip: ', Date.now() - res.time)\n  })\n})\n\nws.on('close', function close() {\n  console.log('disconnected')\n  demux.socket_closed()\n})\n\nws.on('message', function incoming(data) {\n  if(demux.process_message(JSON.parse(data))){\n    // The message was handled return\n    return\n  }\n\n  // Do custom handling. Here we just print the message\n  console.log(data)\n})\n```\n\nThere is also a promisified version that you can use if you fancy:\n\n```javascript\nlet res = await demux.request_async(ws, { time: Date.now() })\nconsole.log(res)\n```\n\nInstallation\n---\n```\nnpm install --save websocket-demux\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fharkal%2Fwebsocket-demux","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fharkal%2Fwebsocket-demux","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fharkal%2Fwebsocket-demux/lists"}