I have a chat application here . I was going through the wiki of WebSockets . There the code is described in the ChatController like this :
class ChatController < WebsocketRails::BaseController
def initialize_session
# perform application setup here
controller_store[:message_count] = 0
end
end
My Question : how should i implement this in my chatcontroller
which is exteding the ApplicationController
? .
Whether i should create a new controller for using the websockets
or modify the existing chatcontroller
which should extend the WebsocketRails
? . I am new to WebSockets so any help regarding this will really help .
Thank you .
The answer, with relation to the websocket-rails gem, as well as Faye and Plezi is YES, you have to create a new controller class, different than the one used by Rails.
Your websocket-rails must inherit
WebsocketRails::BaseController
, while your Rails controller must inheritActionController::Base
(usually by inhering you ApplicationController, which inherits this class).Ruby doesn't support double class inheritance (although Mixins are possible when using modules).
Faye, on the other hand doesn't use the Controller in the same object oriented way and there are more options there. For instance, you COULD map websocket events to your controller CLASS methods, but you might have an issue initializing a Rails controller for each websocket connection, since some of the Controller's internal mechanisms might break. Session info, for instance, will NOT be available and you would probably have to avoid any and all Rails specific methods.
With Plezi, these inheritance issues don't exit, but Plezi automatically creates Http routes to any public methods your Controller has - so your Rails methods will be exposed in a way you didn't intend. Plezi Controllers, on the other hand, can answer both Http and Websockets.
The reason I wrote also about Faye and Plezi is because the websocket-rails gem had last been updated (according the the CHANGELOG) on March 2014...
I don't know how well it will survive (or had survived) the latest updates to Rails, but I would recommend moving on.
For now, November 2015, Faye is the more common option and Plezi is a new player in the field. I'm Plezi's author.
Edit (answering the comment)
Both Faye and Plezi should allow one user to send a message to another.
I think Plezi is easiyer to use, but this is because I wrote Plezi. I am sure the person that wrote Faye will think that Faye is easier.
There are a few options about the best way(s) to implement a chat, depending on how you want to do so.
You can look at the plezi application demo code if you install Plezi and run (in your terminal):
Here's a quick hack for adding Plezi websocket broadcasting to your existing application.
If I had access to your database, I would have done it a little differently... but it's good enough as a proof of concept... for now.
Add the following line to your
Gemfile
:Create a
plezi_init.rb
file and add it to yourconfig/initializers
folder. Here is what it hold for now (most of it is hacking the Rails cookie, because I don't have access to your database and I can't add fields):That's almost all the Plezi application you need for this one.
Just add the following line in to your
ChatsController#create
method, just before therespond_to
:That's it for the server... Now, the client.
Add the following script to your
chat.html.erb
template (or, because turbo-links might mess up your script's initialization, add the script to yourapplication.js
file... but you will be refusing a lot of connections until your users log in):Done.