Elixir GenServer basics

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

This is a really great video. Thanks for the wonderful explanation.

👍︎︎ 3 👤︎︎ u/DeodorantMan 📅︎︎ Nov 30 2016 🗫︎ replies

Great video! Would love to see more like this!

👍︎︎ 2 👤︎︎ u/old_to_me_downvoter 📅︎︎ Nov 30 2016 🗫︎ replies

Awesome video! Just started learning Elixir, and this video helped understand how GenServer works.

👍︎︎ 1 👤︎︎ u/pelmenept 📅︎︎ Nov 30 2016 🗫︎ replies

Thanks for the video. Very helpful and straight forward. Looking forward to follow up videos :) I've subscribed to your Youtube channel!

👍︎︎ 1 👤︎︎ u/zachid 📅︎︎ Dec 01 2016 🗫︎ replies
Captions
in elixir jet server module provides a standard way for writing concurrent methods and holding state it works by wrapping your regular module in a set of special functions that abstract the way processes talk to each other the syntax of these functions can seem a bit overwhelming and because of their rigid format requirements figuring out proper setup isn't Rivia there are many examples and tutorials including the standard documentation the work great this presentation however aims to take more of a deconstructive approach we're going to go over the structure of a typical gen server module break down the setup and the syntax of these special functions and use pointy arrows to show what's happening at every step first let's talk about the setup this is the most basic example of the gen server module the start link method starts a gen server process when you run it you get a tapa with okay and process ID when it started successfully now the process ID is its own special data type it's not a string or a number so you can just copy and paste it to use it you have to assign it to a variable and you will need it in order to call methods on that specific gen server process notice inside there we call Jen server module start link the first argument is the module name itself more often though what you're going to see is the special module variable which is an elixir atom the refers to the name of the module that it's in so what that means is the Jen server module wraps all of the methods inside of this current module and starts a detached process with all of the modules functionality available inside it the second argument is the initial state that is going to be held inside of this process but this is a bit of an incomplete picture there is an implied method that gets called here the anit method gets called when you call start link and whatever this data happens to be it gets passed into here it's also important to note that whatever else happens inside of this method you are required to respond with a two element tuple with the first element being okay and the second element being the initial state that this gen server process is going to hold so this initial data can be any elixir data type a string or map or a list this implicit calling of a method is a little unusual for elixir but it does happen if we change this method a bit and add this line but you're going to see when you call basic start link is this output the init method is not being called explicitly but it is being called in fact as you saw earlier you don't need it in order to start a Jen server process the thing to take away from this is that the init method is used to set the state of the Jen server process and it must respond in the given format now what you're going to see in case you don't have the proper return value is this error response with the Gentz of a process not starting successfully so just remember okay data tuple is what the return value of the init method should be and the purpose of the init method is to set the initial State now let's discuss how we can access and update this state let's go back to our basic example but instead of nothing let's pass in something more meaningful like a greeting and instead of a string let's assign this greeting map or the greeting key and said that is the initial state so now that we said this is the initial State how do we access it and this is where the special gen server callback functions come in notice these two new methods get my state and handle call let's go over how they're used first we start a gen server process then we use the basic pin variable to call get my state which yields greeting hello map when you call get my state inside of it the process ID gets passed to the gen server call method this method takes the process ID and a tuple that is used to identify this method inside of a running process a callback method handle call gets triggered notice how we're not passing the process ID into it we are not explicitly calling it it's triggered by the gen server call method so this tuple matched this specific handle call method in fact you could just run gen server call method with a given process ID completely outside of this module and it would trigger handle call method in exactly the same way so in this case get my state is just there for convenience let's zoom in on the handle call method handle call is synchronous meaning it will block further code execution until it's done in this case until it sends the reply the first argument is a topo that has an atom to match to a particular handle call method that topic could also hold extra arguments for this method we'll see an example of this later the second argument is a reference to the origin of this callback we won't be using it here the third argument is the current state if you recall from the example it will be the Greenies map I want to emphasize again that you are not passing any of these arguments explicitly the particular module will be identified by the process ID and the particular handle call method inside of the process will be triggered by a pattern matching to this topple the process itself is what calls the handle comma and you have to structure it in this exact way next let's talk about the response again it has to be structured in this exact format the first element is the reply ad it indicates that this method will have a reply that is typically the case for the handle call the second element is the response of the return value here we're responding with the entire state but that's not always the case the third element is the new state that this process will hold in this case we are just keeping it the same and that's the basic anatomy of this method now let's try something more interesting let's add a method to update the state with a new greeting and the method that just returns the current greeting I want to make a quick point that the module method names don't have to correspond to the gen server call tuple alright first let's start the gen server process now let's see what the current greeting is we pass in the process ID here to this method which triggers this callback and responds with this now let's change the greeting to something different notice we now pass the process ID and the new greeting and now the gen server call method has a tuple with two elements that matches this method with a new greeting as an additional argument in this handle call method we update the greetings map set and return the new state now if we run get my greeting method again we should get ahoy one thing to note is that when we are setting the greeting we don't necessarily want it to return anything we just care that the gen server process state gets updated if we don't need to wait for this method to respond we could take advantage of another callback that could operate asynchronously meaning not blocking for the code execution take an advantage of elixirs ability to execute code concurrently but more on that and another time my hope for this video was that it caused a light bulb moment for someone and they finally get a new concept obviously there's a lot more to learn and if you still feel lost or like you're missing something don't give up follow a few tutorials come back and read the documentation again you'll be surprised where you'll find the missing puzzle pieces along the way thank you for watching
Info
Channel: omgneering
Views: 17,964
Rating: 5 out of 5
Keywords: elixir, genserver, gen_server, basics, otp
Id: zC7TcrRi46Q
Channel Id: undefined
Length: 8min 9sec (489 seconds)
Published: Tue Nov 29 2016
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.