Simple back end app for chat. Provides API to login/logout and chating.
API available on http://chaty.st.lviv.ua/api
.
For chat functionality with websockets, use http://chaty.st.lviv.ua
address.
General flow of using API:
- (Optional) Checking, if choosen name is available. [API]
- Loging user (with name, sex, etc.) [API]
- Open WebSocket for chat [WS]
- Send initial message (
enterChat
) [WS] - Send and receive chat messages and user statuses [WS]
- Close socket and/or logout user. [API/WS]
*API - use via XMLHttpRequest, WS - WebSocket
All objects should be send and will be received in JSON.
{
name: "someName"
sex: ["male"/"female"]
image: STILL IN PROGRESS! (sorry for that :))
}
{
id: "23fasdbas323rfsadf32fawe32f2" (hash)
name: "someName"
sex: ["male"/"female"]
image: STILL IN PROGRESS! (sorry for that :))
}
{
id: "23fasdbas323rfsadf32fawe32f2" (hash)
}
{
user: "someUserName",
message: "Some message text",
[isYourMessage: true] - if you posted this message
}
{
success: true/false
}
{
message: "Some error message. Also HTTP status code will be available."
}
##API methods
Check, if user is online.
Output: successObject
.
Get particular user object, if userName provided or array of users, which is online.
Output: userObject/[userObject]
.
Login user.
Input: userObject
.
Output: secureUserObject
.
Logout user.
Input: userIDObject
or secureUserObject
.
Output: successObject
.
Chat functionality implemented with socket.io. So, it's recommended to use it on client side, unless you are using native WebSocket API or another lib. In this case, you have to mimic socket.io protocol.
Join your socket to chat room.
Input: userObject
.
Also, it triggers server event chatEntered
event for you, and userOnline
for all users online. Example:
var socket = io.connect(chatUrl);
socket.on("connect", function(){
socket.emit("enterChat", user);
socket.on("chatEntered", function(){
// some code to notify user, that chat is available: blink button, popup, alert :)
});
});
Post your message in the chat.
Input: messageObject
Triggers after you entered the chat. Only YOU will be notified.
Output nothing :)
Notify ALL about new message in the chat.
Output: messageObject
.
Notify ALL about new user in chat.
Output: userObject
.
Notify ALL about some user has left the chat.
Output: userObject
.
If something is wrong, server will emit error event.
Output: errorObject
.
Just to be make sure, that difference between client event and server one is crystal clear:
Send data to the server by emiting (.emit()
) client events (enterChat, postMessage, ...).
Receive data from the server by listening (.on()
) server events (userOnline, message...)
If something went wrong (server is down, bugs (I'm sure, they are there), or this doc sucks) email me!