idi527

idi527

If I didn’t need distribution (although it would be possible to add later), I’d probably have a genserver tracking the new users.

def handle_info({:new_user, socket}, [{us1, ref1}, {us2, ref2}]) do
  # we had 2 users, a new one joined, so that's 3
  user_sockets = [socket, us1, us2]
  # we can create a room for them now
  create_new_room(user_sockets)
  # and clean the state
  Enum.each([ref1, ref2], fn ref ->
    Process.demonitor(ref)
  end)
  {:noreply, []}
end

def handle_info({:new_user, socket}, waiting_user_sockets) do
  # otherwise just add the new users to the waiting users list
  ref = Process.monitor(socket)
  {:noreply, [{socket, ref} | waiting_user_sockets]}
end

def handle_info({:DOWN, ref, :process, _object, _reason}, waiting_user_sockets) do
  # remove the disconnected socket
end
3
Post #11
ankhnaton

ankhnaton

sorry for the late reply but yes

0
Post #12
Zesky665

Zesky665

I don’t have the time to do anything like that nowadays.

I could send you a working example, if you’re still interested.

0
Post #13
qqdipps

qqdipps

Hi Did u happen to make a video tutorial for user to user private messages?

0
Post #16
qqdipps

qqdipps

Can you send me a working example?

0
Post #17
Akshay-laitkor

Akshay-laitkor

I need your help in project i have been assigned ,i have to make api using elixir and Phoenix framework . I have made login / signup api but i have confusion in chat api ,i have to use websocket and store message , this is one to one using postgresql, tables made user and chat table, in which field are
user:
field :email, :string
field :mobileno, :string
field :password_hash, :string
field :username, :string
field :password, :string, virtual: true

chat:
field :message, :string
field :flag, :integer
field :id_from ,belongs_to :user,Talk2me.Accounts.User
field :id_to ,belongs_to :user,Talk2me.Accounts.Users

reply me soon.

used guardian jwt

i need a example to create one to one chatapp
also api for chat

0
Post #18
kokolegorille

kokolegorille

I am not using api for chat, I do it with websocket.

The trick is to have at least a channel per user, identified by id.

Whenever a message occur (either by api, or websocket) You get the id of the destination user, then it’s simple to use the user channel and broadcast to it.

0
Post #19
Akshay-laitkor

Akshay-laitkor

can you plz upload your project in github so i can get idea of websocket

0
Post #20
kokolegorille

kokolegorille

Searching for Phoenix Chat will returns a lot of result.

Like this 3 parts serie about Phoenix and Chat

0
Post #21
Akshay-laitkor

Akshay-laitkor

okay :slight_smile:

1
Post #22