{"id":23286686,"url":"https://github.com/allnulled/multisocket","last_synced_at":"2025-04-06T15:34:06.187Z","repository":{"id":268757130,"uuid":"905377391","full_name":"allnulled/multisocket","owner":"allnulled","description":"Manage socket.io and socket.io-client communications and events from the same JavaScript class.","archived":false,"fork":false,"pushed_at":"2024-12-18T17:48:38.000Z","size":5,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-30T05:32:55.016Z","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/allnulled.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-12-18T17:44:40.000Z","updated_at":"2024-12-18T17:51:57.000Z","dependencies_parsed_at":"2024-12-18T18:38:50.167Z","dependency_job_id":null,"html_url":"https://github.com/allnulled/multisocket","commit_stats":null,"previous_names":["allnulled/multisocket"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allnulled%2Fmultisocket","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allnulled%2Fmultisocket/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allnulled%2Fmultisocket/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/allnulled%2Fmultisocket/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/allnulled","download_url":"https://codeload.github.com/allnulled/multisocket/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247503587,"owners_count":20949465,"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-12-20T02:14:04.537Z","updated_at":"2025-04-06T15:34:06.151Z","avatar_url":"https://github.com/allnulled.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# multisocket\n\nManage socket.io and socket.io-client communications and events from the same JavaScript class.\n\n## Installation\n\n```sh\nnpm i -s @allnulled/multisocket\n```\n\n## Usage\n\n```js\nconst Multisocket = require('@allnulled/multisocket');\n\nconst $msServer = new Multisocket(true, 3000);\nConfigure_server: {\n  $msServer.server.register('test', (data, $eventClient) =\u003e {\n    $eventClient.send({\n      type: 'response',\n      message: 'Respuesta del servidor'\n    });\n  });\n}\n\nconst $msClient = new Multisocket(false, 3000, 'http://localhost:3000');\nConfigure_client: {\n  $msClient.client.register('response', (data) =\u003e {\n    console.log('Cliente recibió:', data);\n  });\n}\n\nHello: {\n  $msClient.client.send({\n    type: 'test',\n    message: '¡Hola servidor!'\n  });\n}\n```\n\n## API\n\n```js\nclass MultisocketClient {\n\n  constructor(multisocket) {\n    this.multisocket = multisocket;\n    this.events = {};\n  }\n\n  send(data, headers = {}) {\n    this.multisocket.socketClient.emit(\"MultisocketEvent\", { data, headers, });\n  }\n\n  register(eventType, dispatcher) {\n    if(!(eventType in this.events)) {\n      this.events[eventType] = [];\n    }\n    this.events[eventType].push(dispatcher);\n  }\n\n  dispatch(eventItem) {\n    if(typeof eventItem === \"string\") {\n      console.log(\"MultisocketEvent on client of type message: \" + eventItem);\n    } else {\n      // @TODO: complete\n      if (eventItem \u0026\u0026 eventItem.data) {\n        const eventType = eventItem.data.type;\n        if (eventType in this.events) {\n          this.events[eventType].forEach(dispatcher =\u003e dispatcher.call(this, eventItem.data));\n        }\n      }\n    }\n  }\n\n}\n\nclass MultisocketServer {\n\n  constructor(multisocket) {\n    this.multisocket = multisocket;\n    this.events = {};\n  }\n\n  send(data, headers = {}) {\n    this.multisocket.socketClient.emit(\"MultisocketEvent\", { data, headers, });\n  }\n\n  register(eventType, dispatcher) {\n    if(!(eventType in this.events)) {\n      this.events[eventType] = [];\n    }\n    this.events[eventType].push(dispatcher);\n  }\n\n  dispatch(eventItem, socketClient) {\n    if(typeof eventItem === \"string\") {\n      console.log(\"MultisocketEvent on server of type message: \" + eventItem);\n    } else {\n      // @TODO: complete\n      if (eventItem \u0026\u0026 eventItem.data) {\n        const eventType = eventItem.data.type;\n        if (eventType in this.events) {\n          this.events[eventType].forEach(dispatcher =\u003e dispatcher.call(this, eventItem.data, socketClient));\n        }\n      }\n    }\n  }\n\n}\n\nclass MultisocketServerEventClient {\n\n  constructor(multisocket, serverEventClient) {\n    this.multisocket = multisocket;\n    this.events = {};\n    this.client = serverEventClient;\n  }\n\n  send(data, headers = {}) {\n    this.client.emit(\"MultisocketEvent\", { data, headers, });\n  }\n\n  register(eventType, dispatcher) {\n    if(!(eventType in this.events)) {\n      this.events[eventType] = [];\n    }\n    this.events[eventType].push(dispatcher);\n  }\n\n  dispatch(eventItem, socketClient) {\n    if(typeof eventItem === \"string\") {\n      console.log(\"MultisocketEvent on server of type message: \" + eventItem);\n    } else {\n      // @TODO: complete\n      if (eventItem \u0026\u0026 eventItem.data) {\n        const eventType = eventItem.data.type;\n        if (eventType in this.events) {\n          this.events[eventType].forEach(dispatcher =\u003e dispatcher.call(this, eventItem.data, socketClient));\n        }\n      }\n    }\n  }\n\n}\n\nclass Multisocket {\n  constructor(isServer = false, port = 3000, serverUrl = '') {\n    this.isServer = isServer;\n    this.port = port;\n    this.serverUrl = serverUrl;\n\n    // Propiedades para los objetos de servidor HTTP, servidor Socket.io y cliente Socket.io\n    this.httpServer = null;\n    this.socketServer = null;\n    this.socketClient = null;\n    this.client = new MultisocketClient(this);\n    this.server = new MultisocketServer(this);\n\n    if (this.isServer) {\n      this.startServer();\n    } else {\n      this.startClient();\n    }\n  }\n\n  // Inicia el servidor socket.io\n  startServer() {\n    this.httpServer = http.createServer((req, res) =\u003e {\n      res.writeHead(200, { 'Content-Type': 'text/plain' });\n      res.end('Servidor en ejecución');\n    });\n\n    this.socketServer = socketIo(this.httpServer);\n\n    this.socketServer.on('connection', (socket) =\u003e {\n      console.log('Cliente conectado');\n      socket.on('MultisocketEvent', (data) =\u003e {\n        console.log('Mensaje del cliente:', data);\n        const temporaryClient = new MultisocketServerEventClient(this, socket);\n        this.server.dispatch(data, temporaryClient);\n      });\n\n      socket.emit('MultisocketEvent', '¡Hola desde el servidor!');\n\n      socket.on('disconnect', () =\u003e {\n        console.log('Cliente desconectado');\n      });\n    });\n\n    this.httpServer.listen(this.port, () =\u003e {\n      console.log(`Servidor escuchando en http://localhost:${this.port}`);\n    });\n  }\n\n  // Inicia el cliente socket.io\n  startClient() {\n    this.socketClient = ioClient(this.serverUrl);\n\n    this.socketClient.on('connect', () =\u003e {\n      console.log('Conectado al servidor');\n      this.socketClient.emit('MultisocketEvent', '¡Hola desde el cliente!');\n    });\n\n    this.socketClient.on('MultisocketEvent', (data) =\u003e {\n      console.log('Mensaje del servidor:', data);\n      this.client.dispatch(data);\n    });\n\n    this.socketClient.on('connect_error', (err) =\u003e {\n      console.error('Error de conexión:', err);\n    });\n  }\n}\n\nMultisocket.default = Multisocket;\n\nmodule.exports = Multisocket;\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fallnulled%2Fmultisocket","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fallnulled%2Fmultisocket","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fallnulled%2Fmultisocket/lists"}