V
V
vadimstroganov2016-06-12 02:29:07
Ruby on Rails
vadimstroganov, 2016-06-12 02:29:07

How to chat on web sockets?

Hello! I'm trying to make a chat on RoR using websocket.
I have never worked with web sockets and have gone into a stupor.
Installed the tubesock gem to open a web socket connection. The connection opens, and if you send something, then the answer comes, here is the code:

def socket
  hijack do |tubesock|
    tubesock.onopen do
      tubesock.send_data "Hello, friend"
    end

    tubesock.onmessage do |data|
      # как тут определить какой юзер и кому отправляет, чтобы создать запись в БД, и после этого отослать получателю сообщение
      tubesock.send_data "You said: #{data}"
    end
  end
end

I need to make a chat between different people, I have written methods to send a message from one user to another. But I don’t understand how to put it all through the web socket? How do I determine that User1 is connected to the websocket and wants to send a message to User2 ?
Please help me figure it out

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Karim Kyatlottyavi, 2016-06-14
@constXife

Why not try Rail ActionCable? It should work not only under Rails 5, but also under Rails 4 and there are quite a few examples on it - https://github.com/rails/rails/tree/master/actioncable

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question