Project

ws_chatter

0.0
No commit activity in last 3 years
No release in over 3 years
Chat gem for Ruby on Rails based on WebSockets
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

Runtime

 Project Readme

WsChatter

WsChatter is a WebSocket-based gem for Ruby on Rails which lets you easily add a chat functionality to your application. WsChatter requires Warden for authentication. The simplest way to use Warden in your application is to install devise and create a User model, which automatically gets used by WsChatter.

Installation

Run @rails generate ws_chatter:install@ to create the initializer, migration adding status column to the user model and migration creating the message model. Three options are available:

--scope SCOPE changes the warden scope and the user model that the authentication is performed on. Default: user

--status-column STATUS_COLUMN changes the name of the column on the user model where the boolean value representing whether the user is online is stored. Default: online

--messages MESSAGES name of the model in which the messages are stored. Default: message

Run rake db:migrate after running the generator.

Javascript API

On the client side, you connect to the chat backend with the following code:

var onmessage = function(id, message, time) {
  // insert code here...
}

var onconnection = function(id, message, time) {
  // insert code here...
}

var chatter = new WsChatter(onmessage, onconnection)

Where onmessage() is a handler which gets called after receiving a new message and receives the id of the user sending the message, the message body and the time of creation. onconnection() gets called when a connection with another user on the server gets created/destroyed to inform the user of the change of status.

You can use the following functions during the run of the application:

chatter.send(user_id, message)

chatter.read(user_id)

chatter.close()

chatter.send() sends the message to the user with a given id, chatter.read() marks all the messages from a given user as read, and chatter.close() closes the connection.

License

MIT License.