{"id":20187375,"url":"https://github.com/edivados/nitro-spa","last_synced_at":"2026-05-12T19:47:49.066Z","repository":{"id":219440256,"uuid":"748926033","full_name":"edivados/nitro-spa","owner":"edivados","description":"Vite SPA + Nitro Server with Socket.IO \u0026 WS in Node environment","archived":false,"fork":false,"pushed_at":"2024-12-23T21:06:39.000Z","size":461,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-10T07:37:11.810Z","etag":null,"topics":["nitro","socket-io","spa","unjs","websocket","ws"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","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/edivados.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"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,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-01-27T03:58:12.000Z","updated_at":"2024-12-23T21:06:42.000Z","dependencies_parsed_at":"2024-03-13T19:46:31.258Z","dependency_job_id":"c8c31cb2-6569-45eb-8bf8-f2c974cda0ce","html_url":"https://github.com/edivados/nitro-spa","commit_stats":null,"previous_names":["edivados/nitro-spa"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/edivados/nitro-spa","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edivados%2Fnitro-spa","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edivados%2Fnitro-spa/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edivados%2Fnitro-spa/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edivados%2Fnitro-spa/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/edivados","download_url":"https://codeload.github.com/edivados/nitro-spa/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edivados%2Fnitro-spa/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":27321029,"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","status":"online","status_checked_at":"2025-11-28T02:00:06.623Z","response_time":58,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["nitro","socket-io","spa","unjs","websocket","ws"],"created_at":"2024-11-14T03:23:20.283Z","updated_at":"2025-11-28T19:04:29.857Z","avatar_url":"https://github.com/edivados.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Vite SPA + Nitro Server with Socket.IO \u0026 WS in Node environment\n\nThis repository demonstrates how to set up a Single Page Application (SPA) using Vite, integrated with a Nitro server, and demonstrates how to use [Socket.IO](https://github.com/socketio/socket.io) and [WebSockets (WS)](https://github.com/websockets/ws) for real-time communication.\n\n## Overview\n\n### Websocket upgrade handling\n\nIn development, the project overwrites Nitros dev preset and uses a custom Node preset in production to enable passing upgrade requests to route handlers. The built in websocket support with [crossws](https://github.com/unjs/crossws) is disabled.\n\n#### Socket.IO with Nitro\n\nSocket.IO is a bit tricky because it expects to be passed an http server instance. For production, it would be possible to make the server accessible, but there doesn't seem to be a way to do that in development because of the way Nitro works under the hood, with a listhen instance and worker running the actual server code.\n\nA solution I settled with is to pass a Node HTTP server instance to Socket.IO without starting it, and manually emit the `request` and `upgrade` events on the server instance.\n\n```ts\nexport default lazyEventHandler(() =\u003e {\n  const { httpServer } = useSocketIOServer();\n  return eventHandler(event =\u003e {\n    event.headers.get(\"upgrade\") === \"websocket\"\n      ? httpServer.emit(\"upgrade\", event.node.req, event.node.req.socket, Buffer.alloc(0))\n      : httpServer.emit(\"request\", event.node.req, event.node.res);\n      event._handled = true;\n  });\n});\n\n```\n\n#### WS with Nitro\n\nNothing special here, using the `handleUpgrade` function of WS in the ws server route.\n\n### Request handling\n\nIn development, the Nitro renderer proxies requests with [httpxy](https://github.com/unjs/httpxy) to Vite, while in production, it returns the content of `index.html`.\n\n## Running the appplication\n\n### Installation\n\nClone the repository and install the necessary dependencies:\n```sh\ngit clone https://github.com/edivados/nitro-spa.git \ncd nitro-spa \nnpm install\n```\n\n### Usage\n\n#### Development\n\nTo start the development server, run the following command:\n\n```sh\nnpm run dev\n```\nThis will start the Vite server along with the Nitro server.\n\n#### Production build and preview\n\n```sh\nnpm run build\nnpm run preview\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedivados%2Fnitro-spa","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fedivados%2Fnitro-spa","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedivados%2Fnitro-spa/lists"}