chat-client
A simple chat client to be used with node-chat-server.
Installation
npm install chat-client
Usage
This chat client is compliant and meant to be used with node-chat-server.
var ChatClient = ; // creating a ChatClient and connecting the undelying socket.var chatClient = url: 'ws://localhost:4001' ; // you can also connect at a later time: chatClient = ; chatClient; // wait for the socket to be opened.chatClient;
Options
When creating an instance of ChatClient
you may pass an 'options' object to it, containing some or all of the following fields:
- url - String - if preset will connect the underlying socket to
url
. - onOpen - Function - will be called when the socket opens.
- onError - Function - will be called when the socket cannot connect or is closed unexpectedly.
- onMessage - Function - will be called on any incoming socket message.
- onClose - Function - will be called whenever the socket closes.
Methods
- connect - (url: String) - connects the underlying socket to
url
. - on - (eventName: String, listener: Function) - binds
listener
to theeventName
event. - off - (eventName: String, listener: Function) - unbinds
listener
to theeventName
event. iflistener
is not provided, all listeners foreventName
will be removed. if bothlistener
andeventName
are not provided, all listeners for all events will be removed. - emit - (eventName: String, ...args) - emit the
eventName
event. any additional arguments will be passed to bound listeners. - run - (type: String, data: Object) => Promise - runs an action on the server.
type
is the name of the action on the server.data
will be passed to the action on the server. a deffered promise is returned from this function. the promise will be resolved or rejected based on the server action's response. - whenOpened - (callback: Function) - runs
callback
whenever the socket opens. note that if the socket is already opened,callback
will run immediately. - authorize - (data: Object) - runs the
authorize
action on the server, passingdata
. - send - (message: Object) - sends a chat message to the server. this will run the
create
action on the server, passingdata
. - read - (data: Object) - mark a chat message as having been read. this will run the
read
action on the server, passingdata
. - error - (err: any) - a default error handler which just logs the error to the console. if you pass
onError
to the constructor it will override this function.
Events
- message - fired for every incoming socket message.
- open - fired whenever the socket openes.
- close - fired whenever the socket closes.
- error - fired when the socket cannot connect or is closed unexpectedly.
- chatMessage - fired for every incoming chat message.