{"_id":"@999-cs/websocket-it","_rev":"5-5b38c9cfe764bc7a25be6822ae891430","name":"@999-cs/websocket-it","dist-tags":{"latest":"2.0.1"},"versions":{"1.0.0":{"name":"@999-cs/websocket-it","version":"1.0.0","author":{"name":"zaayd_999"},"license":"ISC","_id":"@999-cs/websocket-it@1.0.0","maintainers":[{"name":"999-cs","email":"zayplaysissad@gmail.com"}],"dist":{"shasum":"c766a784862643ce04b6f8716a190c4a0de23d57","tarball":"https://registry.npmjs.org/@999-cs/websocket-it/-/websocket-it-1.0.0.tgz","fileCount":3,"integrity":"sha512-ZTvmzJQf5PlR8dPFj39IFtF0LT9G4txb5V+zh+L9KMokIQiUEJoMOKQejoGmeX5JqGRYPaqWIG2kUkhBzL90Kw==","signatures":[{"sig":"MEQCIHQ59YsRLMH4pp6hnysa6tlLI272ANkBQw1MGwt8EgtZAiAz8gesCSUa8dXVbo+XD6XL+6MoDnb7nxfM0vPwFz0YrQ==","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":2167},"main":"main.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"_npmUser":{"name":"999-cs","email":"zayplaysissad@gmail.com"},"_npmVersion":"10.5.1","directories":{},"_nodeVersion":"20.12.0","dependencies":{"fs":"^0.0.1-security","ws":"^8.18.0","express":"^4.19.2"},"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/websocket-it_1.0.0_1721757001222_0.23615760454514878","host":"s3://npm-registry-packages"}},"1.1.0":{"name":"@999-cs/websocket-it","version":"1.1.0","author":{"name":"zaayd_999"},"license":"ISC","_id":"@999-cs/websocket-it@1.1.0","maintainers":[{"name":"999-cs","email":"zayplaysissad@gmail.com"}],"dist":{"shasum":"bd834a58a2fde1b6669e698120b464f37b5d19c0","tarball":"https://registry.npmjs.org/@999-cs/websocket-it/-/websocket-it-1.1.0.tgz","fileCount":4,"integrity":"sha512-5SNVCB6ZqftVafSDhrQvyDsz6It1CYOzRwtUwE9X+MJvu5SVDXCFy/PNLBQggAD6IcxRuowcD0IX9PZK9j259Q==","signatures":[{"sig":"MEUCIQD5Anhh+9dux2W4RDVVO+1s1ulpXET5jNDF7AB0BSkO7gIgB2vcCDoUxxKjxpjB+MJsUves4bY8w1zOWwmev9Fte+A=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":7510},"main":"main.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"_npmUser":{"name":"999-cs","email":"zayplaysissad@gmail.com"},"_npmVersion":"10.5.1","description":"A simple and flexible WebSocket server library that extends `EventEmitter` to handle custom events. This library allows you to easily create and manage WebSocket servers, either by providing a port number or using an existing `WebSocketServer` instance.","directories":{},"_nodeVersion":"20.12.0","dependencies":{"fs":"^0.0.1-security","ws":"^8.18.0","express":"^4.19.2"},"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/websocket-it_1.1.0_1721825810490_0.10051749836065849","host":"s3://npm-registry-packages"}},"1.1.1":{"name":"@999-cs/websocket-it","version":"1.1.1","author":{"name":"zaayd_999"},"license":"ISC","_id":"@999-cs/websocket-it@1.1.1","maintainers":[{"name":"999-cs","email":"zayplaysissad@gmail.com"}],"dist":{"shasum":"013e411c5b776e836464d81558f0cc08737d4b5c","tarball":"https://registry.npmjs.org/@999-cs/websocket-it/-/websocket-it-1.1.1.tgz","fileCount":4,"integrity":"sha512-VxxzGf7P04dqKzPVX0njlTxCGAnYSrNCL117VhNvXhmXLOGIkkzFUi0N+FfbuHT2kn0/WRhRkne8y0nLZuVVBw==","signatures":[{"sig":"MEYCIQCXUuz/WoUSh2wZfIUBDNmHo9279kkXjq9768KGSnIV3QIhAPcA3QY0e38KiZTGKrJrBKp/bBADZffxhjy8JftwP8IL","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":7514},"main":"main.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"_npmUser":{"name":"999-cs","email":"zayplaysissad@gmail.com"},"_npmVersion":"10.5.1","description":"A simple and flexible WebSocket server library that extends `EventEmitter` to handle custom events. This library allows you to easily create and manage WebSocket servers, either by providing a port number or using an existing `WebSocketServer` instance.","directories":{},"_nodeVersion":"20.12.0","dependencies":{"fs":"^0.0.1-security","ws":"^8.18.0","express":"^4.19.2"},"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/websocket-it_1.1.1_1721839752177_0.06668356996351066","host":"s3://npm-registry-packages"}},"1.1.2":{"name":"@999-cs/websocket-it","version":"1.1.2","author":{"name":"zaayd_999"},"license":"ISC","_id":"@999-cs/websocket-it@1.1.2","maintainers":[{"name":"999-cs","email":"zayplaysissad@gmail.com"}],"dist":{"shasum":"955de3d56f182868ba157773865ef8d892e2eeff","tarball":"https://registry.npmjs.org/@999-cs/websocket-it/-/websocket-it-1.1.2.tgz","fileCount":5,"integrity":"sha512-ab+0eKDVBYilyYxIPA5cm9XwlPyXMyFdq9GndxOqFZtG0Smvwztmvvn0cvNsei7QHrOaQTSSYG5wxyp1XIDhqg==","signatures":[{"sig":"MEYCIQCrqGHaVHN8UmeJukCOXrS2vlsoc1rXgctsFaKjs/OguwIhAK9YprFnfhh/SDyYO1j9Sq00xdFoqfzgT6Egru5c4nqf","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":7546},"main":"main.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"_npmUser":{"name":"999-cs","email":"zayplaysissad@gmail.com"},"_npmVersion":"10.5.1","description":"A simple and flexible WebSocket server library that extends `EventEmitter` to handle custom events. This library allows you to easily create and manage WebSocket servers, either by providing a port number or using an existing `WebSocketServer` instance.","directories":{},"_nodeVersion":"20.12.0","dependencies":{"fs":"^0.0.1-security","ws":"^8.18.0","express":"^4.19.2"},"_hasShrinkwrap":false,"_npmOperationalInternal":{"tmp":"tmp/websocket-it_1.1.2_1721851154294_0.861189282426573","host":"s3://npm-registry-packages"}},"2.0.0":{"name":"@999-cs/websocket-it","version":"2.0.0","author":{"name":"zaayd_999"},"license":"ISC","_id":"@999-cs/websocket-it@2.0.0","maintainers":[{"name":"999-cs","email":"zayplaysissad@gmail.com"}],"dist":{"shasum":"4351c01442debf1bf0945845ad081ba288ac8b81","tarball":"https://registry.npmjs.org/@999-cs/websocket-it/-/websocket-it-2.0.0.tgz","fileCount":4,"integrity":"sha512-ipwm8W5Ja2sPqFpm6WDRmvqT4h2Um5NrgPCezBk3PxS+rNdX9O6NW445rSzT+RF6QcLKI0rjz6hwXgT/HFcKlQ==","signatures":[{"sig":"MEUCIQDswVW/wMjYPWz8TJDQOjno7pb8iZu2oefJSy3AEX01zAIgKL9WCV1o7OJncAV0KZFrsd0OLR1m22+vN2ZH0ZmEYi0=","keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA"}],"unpackedSize":7984},"main":"main.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"_npmUser":{"name":"999-cs","email":"zayplaysissad@gmail.com"},"_npmVersion":"10.8.2","description":"A simple and flexible WebSocket server library that extends `EventEmitter` to handle custom events. This library allows you to easily create and manage WebSocket servers, either by providing a port number or using an existing `WebSocketServer` instance.","directories":{},"_nodeVersion":"20.17.0","dependencies":{"fs":"^0.0.1-security","ws":"^8.18.0","express":"^4.19.2"},"_hasShrinkwrap":false,"devDependencies":{},"_npmOperationalInternal":{"tmp":"tmp/websocket-it_2.0.0_1725574830551_0.07302407018015411","host":"s3://npm-registry-packages"}},"2.0.1":{"name":"@999-cs/websocket-it","version":"2.0.1","main":"main.js","scripts":{"test":"echo \"Error: no test specified\" && exit 1"},"author":{"name":"zaayd_999"},"license":"ISC","description":"A simple and flexible WebSocket server library that extends `EventEmitter` to handle custom events. This library allows you to easily create and manage WebSocket servers, either by providing a port number or using an existing `WebSocketServer` instance.","dependencies":{"express":"^4.19.2","fs":"^0.0.1-security","ws":"^8.18.0"},"devDependencies":{},"_id":"@999-cs/websocket-it@2.0.1","_nodeVersion":"20.17.0","_npmVersion":"10.8.2","dist":{"integrity":"sha512-c2q8W6rhMScQ3EKVMNoNvZrjTaV+cXUTIJeGt6dwkl6ZjCANbDZge+Gbg2Uy4e0+n/Vs3kmdLWh8I9isKKUGYw==","shasum":"672ed7455a473133a23cccd44a13259309bafb4b","tarball":"https://registry.npmjs.org/@999-cs/websocket-it/-/websocket-it-2.0.1.tgz","fileCount":4,"unpackedSize":8110,"signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEQCIG67WRwa0nfNdRCfN7UK4l0rZMC3WIcbEKDxENUw5M14AiBRM5RHNOPHJpM7BL/h6M1zXIrEDho1TU1hiW1coWlOmA=="}]},"_npmUser":{"name":"999-cs","email":"zayplaysissad@gmail.com"},"directories":{},"maintainers":[{"name":"999-cs","email":"zayplaysissad@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/websocket-it_2.0.1_1725575020752_0.17448418906664465"},"_hasShrinkwrap":false}},"time":{"created":"2024-07-23T17:50:01.134Z","modified":"2024-09-05T22:23:41.195Z","1.0.0":"2024-07-23T17:50:01.398Z","1.1.0":"2024-07-24T12:56:50.631Z","1.1.1":"2024-07-24T16:49:12.308Z","1.1.2":"2024-07-24T19:59:14.461Z","2.0.0":"2024-09-05T22:20:30.690Z","2.0.1":"2024-09-05T22:23:41.001Z"},"author":{"name":"zaayd_999"},"license":"ISC","description":"A simple and flexible WebSocket server library that extends `EventEmitter` to handle custom events. This library allows you to easily create and manage WebSocket servers, either by providing a port number or using an existing `WebSocketServer` instance.","maintainers":[{"name":"999-cs","email":"zayplaysissad@gmail.com"}],"readme":"# @999-cs/websocket-it Library\r\n\r\nA simple and flexible WebSocket server library that extends `EventEmitter` to handle custom events. This library allows you to easily create and manage WebSocket servers, either by providing a port number or using an existing `WebSocketServer` instance.\r\n\r\n## Installation\r\n\r\nYou can install this library using npm:\r\n\r\n```bash\r\nnpm install @999-cs/websocket-it\r\n```\r\n\r\n## Usage\r\n\r\n### Creating a New WebSocket Server\r\n\r\nYou can create a new WebSocket server by providing a port number:\r\n\r\n```javascript\r\nconst { websocketIt } = require(\"@999-cs/websocket-it\");\r\n\r\nconst port = 8080;\r\nconst server = websocketIt.createServer({ port });\r\n\r\nserver.on(\"connection\", (ws) => {\r\n    console.log(\"A new client has connected\");\r\n});\r\n\r\nserver.on(\"messageCreate\", (data) => {\r\n    console.log(\"messageCreate event received with data:\", data);\r\n    //server.wss.clients -> Get all clients\r\n    //server.WebSocket -> try it by yourself and discover what this property do\r\n});\r\n\r\nserver.on(\"close\", () => {\r\n    console.log(\"A client has disconnected\");\r\n});\r\n\r\nserver.on(\"error\", (error) => {\r\n    console.error(`An error occurred: ${error}`);\r\n});\r\n\r\nconsole.log(`WebSocket server started on port ${port}`);\r\n```\r\n\r\n### Using an Existing WebSocket Server\r\n\r\nYou can also use an existing `WebSocketServer` instance:\r\n\r\n```javascript\r\nconst { WebSocketServer } = require(\"ws\");\r\nconst { websocketIt } = require(\"@999-cs/websocket-it\");\r\n\r\nconst existingWss = new WebSocketServer({ port: 8081 });\r\nconst server = websocketIt.createServer({ wss: existingWss });\r\n\r\nserver.on(\"connection\", (ws) => {\r\n    console.log(\"A new client has connected\");\r\n});\r\n\r\nserver.on(\"messageCreate\", (data) => {\r\n    console.log(\"messageCreate event received with data:\", data);\r\n});\r\n\r\nserver.on(\"close\", () => {\r\n    console.log(\"A client has disconnected\");\r\n});\r\n\r\nserver.on(\"error\", (error) => {\r\n    console.error(`An error occurred: ${error}`);\r\n});\r\n\r\nconsole.log(`WebSocket server is using an existing instance on port 8081`);\r\n```\r\n\r\n### Usage from Frontend\r\n\r\nTo connect to the WebSocket server from the frontend, you can use the `WebSocket` API available in modern browsers. Below is an example of how to connect to the WebSocket server and handle events.\r\n\r\n```html\r\n<!DOCTYPE html>\r\n<html lang=\"en\">\r\n<head>\r\n    <meta charset=\"UTF-8\">\r\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\">\r\n    <title>WebSocket Test</title>\r\n</head>\r\n<body>\r\n    <h1>WebSocket Test</h1>\r\n    <input type=\"text\" id=\"messageInput\" placeholder=\"Type a message\" />\r\n    <button onclick=\"sendMessage()\">Send Message</button>\r\n    <div id=\"output\"></div>\r\n\r\n    <script>\r\n        const socket = new WebSocket(\"ws://localhost:8080\");\r\n\r\n        socket.onopen = () => {\r\n            console.log(\"Connected to the server\");\r\n            document.getElementById('output').innerHTML += \"<p>Connected to the server</p>\";\r\n        };\r\n\r\n        socket.onmessage = (event) => {\r\n            const message = event.data;\r\n            console.log(\"Received:\", message);\r\n            document.getElementById('output').innerHTML += `<p>Received: ${message}</p>`;\r\n        };\r\n\r\n        socket.onclose = () => {\r\n            console.log(\"Disconnected from the server\");\r\n            document.getElementById('output').innerHTML += \"<p>Disconnected from the server</p>\";\r\n        };\r\n\r\n        socket.onerror = (error) => {\r\n            console.error(\"WebSocket error:\", error);\r\n            document.getElementById('output').innerHTML += `<p>Error: ${error}</p>`;\r\n        };\r\n\r\n        function sendMessage() {\r\n            const input = document.getElementById('messageInput');\r\n            const message = input.value;\r\n\r\n            // It is important to send an object with the correct structure\r\n            const eventMessage = JSON.stringify({\r\n                eventName: \"messageCreate\",\r\n                data: { text: message }\r\n            });\r\n\r\n            socket.send(eventMessage);\r\n            console.log(\"Sent:\", eventMessage);\r\n            document.getElementById('output').innerHTML += `<p>Sent: ${eventMessage}</p>`;\r\n            input.value = '';\r\n        }\r\n    </script>\r\n</body>\r\n</html>\r\n```\r\n\r\n**Note:** It is important to send an object with the correct structure:\r\n```javascript\r\n{\r\n    eventName: \"messageCreate\",\r\n    data: { text: message,author : \"someone\" }\r\n}\r\n```\r\n\r\nThe correct structure includes two properties:\r\n\r\n- `eventName`: A string representing the name of the event.\r\n- `data`: An object containing the data associated with the event.\r\n\r\nThe correct structure includes these two properties to ensure that the server can correctly process and respond to the events.","readmeFilename":"readme.md"}