{"id":18904299,"url":"https://github.com/mohdrashid/socketchat","last_synced_at":"2026-04-25T23:35:30.652Z","repository":{"id":57365492,"uuid":"79385114","full_name":"mohdrashid/socketchat","owner":"mohdrashid","description":"A node.js webscoket chat library with addon authentication functionality","archived":false,"fork":false,"pushed_at":"2017-06-28T19:42:46.000Z","size":477,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-02T12:07:49.318Z","etag":null,"topics":["frontend","fullstack-javascript","javascript","nodejs","socketchat","websockets"],"latest_commit_sha":null,"homepage":"https://droidhat.com/real-time-chat-application-using-nodejs","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/mohdrashid.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-01-18T21:16:05.000Z","updated_at":"2017-03-02T22:24:06.000Z","dependencies_parsed_at":"2022-08-23T20:10:44.349Z","dependency_job_id":null,"html_url":"https://github.com/mohdrashid/socketchat","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/mohdrashid%2Fsocketchat","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohdrashid%2Fsocketchat/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohdrashid%2Fsocketchat/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mohdrashid%2Fsocketchat/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mohdrashid","download_url":"https://codeload.github.com/mohdrashid/socketchat/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239889029,"owners_count":19713702,"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":["frontend","fullstack-javascript","javascript","nodejs","socketchat","websockets"],"created_at":"2024-11-08T09:08:01.559Z","updated_at":"2026-03-04T12:30:20.754Z","avatar_url":"https://github.com/mohdrashid.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"Socketchat is a chat application library using websockets, written\nin JavaScript for node.js to ease building of chat application easy using websockets.\n\nThis library relies on [WebSocket](https://www.npmjs.com/package/websocket) to create websocket server.\n\n\u003ca name=\"format\"\u003e\u003c/a\u003e\n## Message format\nThe client communicate with server using JSON format with some important fields.\nThe server supports two operations as of now: authentication and messaging.\nClients cannot exchange messages without authenticating themselves.\n\nSteps #1\n--------\nAuthentication is used to register client's socket information in server, so that others can communicate using username of the user.\nAuthentication Message must be in the format : {type:'authenticate',username:'xyz',password:'1234'}\n\nSteps #2\n--------\nMessaging also follows a certain format. Messages must be in the format\n{type:'message',to:'username',message:'xyz'}\n\n\n\u003ca name=\"install\"\u003e\u003c/a\u003e\n## Installation\n\n```sh\nnpm install socketchat\n```\n\n\u003ca name=\"example\"\u003e\u003c/a\u003e\n## Server-side Example\n\n```js\nvar socketchatlib=require('./app.js');\n//Sample users list\nvar users={\n  //\"username\":\"password\"\n  \"rx\":\"r123\",\n  \"2\":\"1234\",\n  \"3\":\"12345\"\n};\n//Sample Authentication function\nvar auth=function(username,password){\n  return (users[username]===password)\n}\n//Creating a object of the library.\n//Passing portnumber and authentication function\nvar socketchat=new socketchatlib(1337,auth);\n//Start listening in the specified port\nsocketchat.listen();\n//Upon successfully listening this event will be fired\nsocketchat.on('listen',function(message){\n  console.log(message);\n});\n//Whenever a client tries authenticate, this event will be fired\nsocketchat.on('authentication',function(origin,stat){\n  console.log(origin+\": \"+JSON.stringify(stat));\n});\n//When users exchange message, this event is also fired\nsocketchat.on('message',function(message){\n  console.log(\"Message: \"+JSON.stringify(message));\n});\n//Errors can be listener using this event listener\nsocketchat.on('error',function(err){\n  console.log(\"Error: \"+err);\n});\n\n```\n\noutput:\n```\nConnected\nlistening on port 1337\n\n```\n\n## Client-side Example\n\n```js\n    var ws;\n    function init() {\n      ws = new WebSocket(\"ws://localhost:1337/\");\n\n      // Set event handlers.\n      ws.onopen = function() {\n        output(\"Connection established\");\n      };\n      ws.onmessage = function(e) {\n        // e.data contains received string.\n        var json=JSON.parse(e.data);\n        console.log(json);\n        if(json.type==='authentication'\u0026\u0026json.status==='success')\n        {\n          document.getElementById(\"login\").style.display = \"none\";\n          document.getElementById(\"message\").style.display = \"block\";\n        }\n        else if(json.type==='authentication'\u0026\u0026json.status==='fail'){\n          output(\"Login Failed\");\n        }\n        else if(json.type==='message'){\n          output(\"Message from \" + json.from+\" : \"+json.message);\n        }\n      };\n      ws.onclose = function() {\n        output(\"onclose\");\n      };\n      ws.onerror = function() {\n        output(\"onerror\");\n      };\n    }\n    function onSubmit() {\n      var input = document.getElementById(\"input\");\n      var to = document.getElementById(\"to\");\n      var data={type:'message'};\n      data.message=input.value;\n      data.to=to.value;\n      // You can send message to the Web Socket using ws.send.\n      if(input.value.length\u003e0){\n        ws.send(JSON.stringify(data));\n        output(\"Send to \"+to.value+\" : \"+input.value);\n        input.value = \"\";\n        input.focus();\n      }\n\n    }\n    function login() {\n      var username = document.getElementById(\"username\");\n      var password = document.getElementById(\"password\");\n      var data={type:'authenticate'};\n      data.username=username.value;\n      data.password=password.value;\n      ws.send(JSON.stringify(data));\n    }\n    function onCloseClick() {\n      ws.close();\n    }\n    function output(str) {\n      var log = document.getElementById(\"log\");\n      log.innerHTML = \"\u003cstrong\u003e\"+str + \"\u003c/strong\u003e\u003cbr\u003e\"+new Date()+\"\u003cbr\u003e\u003cbr\u003e\" + log.innerHTML;\n    }\n```\n\n\n\n\n\u003ca name=\"api\"\u003e\u003c/a\u003e\n## API\n\n  * \u003ca href=\"#object\"\u003e\u003ccode\u003enew \u003cb\u003esocketchat()\u003c/b\u003e\u003c/code\u003e\u003c/a\u003e\n  * \u003ca href=\"#listen\"\u003e\u003ccode\u003esocketchat.\u003cb\u003elisten()\u003c/b\u003e\u003c/code\u003e\u003c/a\u003e\n\n-------------------------------------------------------\n\u003ca name=\"object\"\u003e\u003c/a\u003e\n### new socketchat(port,authenticationFunction)\n\nThe passed port number is used for listening and the passed authentication function is used to authenticate users.\n\n-------------------------------------------------------\n\u003ca name=\"request\"\u003e\u003c/a\u003e\n### socketchat.listen()\n\nStarts listening on the port number passed in the constructor\n\n-------------------------------------------------------\n\n\u003ca name=\"Events\"\u003e\u003c/a\u003e\n## Events\n\n  * \u003ca href=\"#listen\"\u003e\u003ccode\u003e\u003cb\u003elisten\u003c/b\u003e\u003c/code\u003e\u003c/a\u003e\n  * \u003ca href=\"#authentication\"\u003e\u003ccode\u003e\u003cb\u003eauthentication\u003c/b\u003e\u003c/code\u003e\u003c/a\u003e\n  * \u003ca href=\"#message\"\u003e\u003ccode\u003e\u003cb\u003emessage\u003c/b\u003e\u003c/code\u003e\u003c/a\u003e\n  * \u003ca href=\"#error\"\u003e\u003ccode\u003e\u003cb\u003eerror\u003c/b\u003e\u003c/code\u003e\u003c/a\u003e\n  \n-------------------------------------------------------\n\u003ca name=\"listen\"\u003e\u003c/a\u003e\n## listen\n\nEmitted when starting to listen\n\n-------------------------------------------------------\n\u003ca name=\"authentication\"\u003e\u003c/a\u003e\n### authentication\n\nEmitted whenever a user tries to authenticate. Emits origin of the connection and status of authentication.\nsocketchat.on('authentication',function(origin,stat){\n});\nstat is a JSON in the format: {type:'authentication',status:'success'} if authentication is successful\notherwise  {type:'authentication',status:'fail'}\n\n-------------------------------------------------------\n\u003ca name=\"message\"\u003e\u003c/a\u003e\n### message\n\nEmitted whenever a user sends message.\nMessages are in the format:\n{type:'message',to:'username',message:'xyz'}\n\n-------------------------------------------------------\n\u003ca name=\"error\"\u003e\u003c/a\u003e\n### error\n\nEmitted when error occurs.\n\n-------------------------------------------------------\n\n\u003ca name=\"license\"\u003e\u003c/a\u003e\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmohdrashid%2Fsocketchat","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmohdrashid%2Fsocketchat","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmohdrashid%2Fsocketchat/lists"}