Socketchat is a chat application library using websockets, written in JavaScript for node.js to ease building of chat application easy using websockets.
This library relies on WebSocket to create websocket server.
Message format
The client communicate with server using JSON format with some important fields. The server supports two operations as of now: authentication and messaging. Clients cannot exchange messages without authenticating themselves.
Steps #1
Authentication is used to register client's socket information in server, so that others can communicate using username of the user. Authentication Message must be in the format : {type:'authenticate',username:'xyz',password:'1234'}
Steps #2
Messaging also follows a certain format. Messages must be in the format {type:'message',to:'username',message:'xyz'}
Installation
npm install socketchat
Server-side Example
var socketchatlib=;//Sample users listvar users= //"username":"password" "rx":"r123" "2":"1234" "3":"12345";//Sample Authentication functionvar { return usersusername===password}//Creating a object of the library.//Passing portnumber and authentication functionvar socketchat=1337auth;//Start listening in the specified portsocketchat;//Upon successfully listening this event will be firedsocketchat;//Whenever a client tries authenticate, this event will be firedsocketchat;//When users exchange message, this event is also firedsocketchat;//Errors can be listener using this event listenersocketchat;
output:
Connected
listening on port 1337
Client-side Example
var ws; { ws = "ws://localhost:1337/"; // Set event handlers. ws { ; }; ws { // e.data contains received string. var json=JSON; console; ifjsontype==='authentication'&&jsonstatus==='success' documentstyledisplay = "none"; documentstyledisplay = "block"; else ifjsontype==='authentication'&&jsonstatus==='fail' ; else ifjsontype==='message' ; }; ws { ; }; ws { ; }; } { var input = document; var to = document; var data=type:'message'; datamessage=inputvalue; datato=tovalue; // You can send message to the Web Socket using ws.send. ifinputvaluelength>0 ws; ; inputvalue = ""; input; } { var username = document; var password = document; var data=type:'authenticate'; datausername=usernamevalue; datapassword=passwordvalue; ws; } { ws; } { var log = document; loginnerHTML = "<strong>"+str + "</strong><br>"++"<br><br>" + loginnerHTML; }
API
new socketchat(port,authenticationFunction)
The passed port number is used for listening and the passed authentication function is used to authenticate users.
socketchat.listen()
Starts listening on the port number passed in the constructor
Events
listen
Emitted when starting to listen
authentication
Emitted whenever a user tries to authenticate. Emits origin of the connection and status of authentication. socketchat.on('authentication',function(origin,stat){ }); stat is a JSON in the format: {type:'authentication',status:'success'} if authentication is successful otherwise {type:'authentication',status:'fail'}
message
Emitted whenever a user sends message. Messages are in the format: {type:'message',to:'username',message:'xyz'}
error
Emitted when error occurs.
License
MIT