Skip to content

Instantly share code, notes, and snippets.

@ScrapCodes
Created November 1, 2011 12:13
Show Gist options
  • Save ScrapCodes/1330399 to your computer and use it in GitHub Desktop.
Save ScrapCodes/1330399 to your computer and use it in GitHub Desktop.
p2p erlang messanger
-module(erlMessage).
-export([start/1,sendMessage/2,msgReceive/0]).
% msgReceive blocks on the message queue and waits for a message!
% and prints the message as soon as it arrives and again tail recurses!
msgReceive() ->
receive
{Message,Message_from} -> io:format("~p said:~p~n",[Message_from,Message]);
Message ->io:format("~p ~n",[Message])
end,
msgReceive(). %tail recursion
%sendMessage simply passes your message to the node specified and prints an acknowledgement.
sendMessage(Message,{Name,Node}) ->
{Name,Node}!{Message,node()},
io:format("Message: ~p sent to ~p ~n",[Message,{Name,Node}]).
%start the seperate process for waiting for incoming messages!
start(User_name) ->
register(User_name,spawn(erlMessage,msgReceive,[])).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment