kaizen23

kaizen23

Two function in one time

Hi everyone.This is my first post.I’m sorry for newby question but I have problem with invoke two function in one time.I was created client for RabbitMQ. While I was starting this script sequence of execute function depends on which function is implementation first in method START.Talking about features listen_for_messages(channel, queue_data.queue) and wait_for_message(user, channel).I want to both features “listen” in one time.Function wait_for_message it’s responsible for text which user write while listen_for_messages it’s responsible for recieve messages from another client. Please give me any suggestion because i have no idea how solve this problem.This is my CODE:

defmodule ElixirChat do

  def start do
    user = IO.gets("Type in your name: ") |> String.strip
    IO.puts "Hi #{user}, you just joined a chat room! Type your message in and press enter."

    {:ok, conn} = AMQP.Connection.open
    {:ok, channel} = AMQP.Channel.open(conn)
    {:ok, queue_data } = AMQP.Queue.declare(channel, "")

    AMQP.Exchange.fanout(channel, "super.chat")
    AMQP.Queue.bind(channel, queue_data.queue, "super.chat")

		**listen_for_messages(channel, queue_data.queue)**
**		wait_for_message(user, channel)**


		
    
  end
  
  def display_message(user, message) do
    IO.puts "#{user}: #{message}"
  end

  def wait_for_message(user, channel) do
     message = IO.gets("") |> String.strip
     publish_message(user, message, channel)
     wait_for_message(user, channel)
  end

  def listen_for_messages(channel, queue_name) do
	  AMQP.Basic.consume(channel,queue_name,nil,no_ack: true)
  	
  	receive do
  		{:basic_deliver, payload, _meta}->
  		{status, list} = JSON.decode(payload)
		  display_message(list["user"], list["message"])
		  listen_for_messages(channel, queue_name) 
    end	


		
  end

  def publish_message(user, message, channel) do
    { :ok, data } = JSON.encode([user: user, message: message])
    AMQP.Basic.publish channel, "super.chat", "", data
  end

end

ElixirChat.start

Most Liked

OvermindDL1

OvermindDL1

Yeah I’d spool it up in a new Task, which is a simple wrapper around spawn with convenience methods (and can be fit into OTP supervisors, but ignore that part for now)> :slight_smile:

kaizen23

kaizen23

Hi.@mbuhot.Many thanks for help. This is saved me a lot of time.I didn’t think that could be so simple.It’s WORKING:)

mbuhot

mbuhot

Try running the listen_for_messages function in another process using spawn: http://elixir-lang.org/getting-started/processes.html#spawn

Where Next?

Popular in Questions Top

Jim
As a follow up to my earlier question: I have the code compiling and running but not getting a successful login from the rest server. ...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
Codball
Mix format works fine if run from the cmd. I’ve followed this to facilitate the implementation into VSC which involves downloading an ext...
New
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
script
If I have a string “1000 cfu/ml” . I want to remove the characters and / and space . So the string is like this "1000" What is the ...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New

We're in Beta

About us Mission Statement