Skip to main content

Group Chat with ActionCable: Part 2 Discussion

General • Asked by Chris Oliver

Hi Chris,
Is it possible to make a REST API for this app and create a Mobile app using Jquery mobile or Android and do real time chat with people chatting on desktop?
I have an application with Rails 5, Action Cable and Devise. I am planning to create mobile app to let mobile users do realtime chat with users on desktop or any other device. My website is device responsive but I am wondering if we can create a mobile app with REST API and do real time chat with Action Cable? Or is Action Cable only applicable to desktop users?

Basecamp uses Turbolinks and the ActionCable JS on mobile to power their app, however I'm sure you could port the client over to a native Swift or Obj-C client that would allow you to build a native app. It'll probably be a decent amount of work to start, but this is an example of someone who build an ActionCable client in Ruby (rather than the default JS) so you could probably use it as a basis for building a Swift client.

If you just go with Turbolinks views on mobile, you'll have a really easy time making this all work because the mobile UI will be the exact same code you use on the desktop browser.

If you want to develop a native iOS app, you can use a Swift client like this: https://github.com/NullVoxP...


Hi, When I try to leave the second (and last) chatroom I joined, I get this error instead of returning me to the chatroom path: https://uploads.disquscdn.c...

My routes look fine to me (when comparing to the source code), so i'm not sure why it isn't working.

Nevermind, `chatrooms_path` shouldn't have the `@` before it! I hope this helps someone else coming across this.


@excid3:disqus awesome tutorial series!

You've said around 6:15 it would be good to add indices to the chatroom_users table. I just can't figure out of to add an uniq index for the chatroom and user references. Do you got a tip for that?

You would want something like this:

add_index(:chatroom_users, [:chatroom_id, :user_id], unique: true)

Thank you for your fast reply! Idk why I got an error first that i should remove the existing indices. It works now tho. Thank you very much!


@excid3:disqus

Hi Chris, can you explain again what the "before_action :set_chatroom" in the ChatroomUsers controller does? Does it just point all the join and leave methods to the specific chatroom id?

Thanks!

Because this controller is used to manage who is actually subscribed to a chatroom, we always include the chatroom ID in the URL. Then we just look up the chatroom and permanently add or remove the join table records to denote who is in the room or not. So yes, basically what you were saying. It's auto-scoping the actions its taking by the chatroom ID in the URL.

https://github.com/gorails-...

Gotcha. I think I've been clumsily adding that code as the first line to all my controller actions in previous projects. Didn't even think to just write it once and set it for all controller actions for simple things, like you did here. Thanks!

Hah! Yeah, it's a fantastic improvement to keep things consistent. One protip I'd recommend is actually studying how Rails generates scaffolds and you can cherry pick ideas from what that generates to use in your own code.


I have implemented this system in my own project. A problem I have is that I do not know when to destroy_all users (as you have done) when the user closes the website for example. How can I detect if they have left the site, and therefore their record in ChatroomUsers needs to be destroyed? I am using this also to show how many active users are in the chatroom, and the number does not go down when the user leaves by closing their browser because of this. Thanks.


Login or Create An Account to join the conversation.

Subscribe to the newsletter

Join 24,647+ developers who get early access to new screencasts, articles, guides, updates, and more.

    By clicking this button, you agree to the GoRails Terms of Service and Privacy Policy.

    More of a social being? We're also on Twitter and YouTube.