# Changing subscriptions when Url changes

**URL:** <https://discourse.elm-lang.org/t/changing-subscriptions-when-url-changes/4451>\
**Category:** Learn\
**Created:** [October 8, 2019, 5:28pm UTC](https://discourse.elm-lang.org/t/changing-subscriptions-when-url-changes/4451 "2019-10-08T17:28:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![charlie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/charlie/32/871_2.png) [@charlie](https://discourse.elm-lang.org/u/charlie)\
**Post date:** [October 8, 2019, 5:28pm UTC](https://discourse.elm-lang.org/t/changing-subscriptions-when-url-changes/4451/1 "2019-10-08T17:28:49Z")

</div>

I have a single-page app where I would like to listen for keyboard subscriptions when a certain route is selected, and I can’t figure out how this is done. I’ve recreated the problem in [a simple repo](https://github.com/armatures/dynamic-elm-subscriptions) based on the Elm Guide’s [example Navigation app](https://guide.elm-lang.org/webapps/navigation.html). Am I allowed to put a conditional in the `subscriptions` function? [This is the line](https://github.com/armatures/dynamic-elm-subscriptions/blob/master/src/Main.elm#L91) that breaks the keyboard listener for me.

I also put this on [Ellie](https://ellie-app.com/6S3myFbpNhka1), but I don’t think routes are supported there so it doesn’t compile.

---

<div class="post-metadata">

**Author:** ![joelq](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/joelq/32/445_2.png) [@joelq](https://discourse.elm-lang.org/u/joelq)\
**Post date:** [October 8, 2019, 9:24pm UTC](https://discourse.elm-lang.org/t/changing-subscriptions-when-url-changes/4451/2 "2019-10-08T21:24:24Z")

</div>

Yes, it’s normal to put a conditional in the `subscriptions` function. Here’s [an example](https://github.com/thoughtbot/whiteboard/blob/master/assets/src/Main.elm#L304-L326) where I do it on one of my projects.

What kind of error are you getting? Does it just not work when you try to run it? Do you get a weird compiler error?

I’ve heard of there being a compiler bug that can cause things to behave strangely for complex `subscriptions` functions but I’ve not run into it myself so don’t have much context on it. Maybe someone else here can fill us in 🙂

---

<div class="post-metadata">

**Author:** ![Carlsson87](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/carlsson87/32/1872_2.png) [@Carlsson87](https://discourse.elm-lang.org/u/Carlsson87)\
**Post date:** [October 9, 2019, 4:23am UTC](https://discourse.elm-lang.org/t/changing-subscriptions-when-url-changes/4451/3 "2019-10-09T04:23:38Z")

</div>

I looked through the code and I think this looks very similar to a problem I encountered. I reported it as an issue to elm/browser ([https://github.com/elm/browser/issues/49](https://github.com/elm/browser/issues/49)) since I thought it had to do with routing somehow. It has also been reported to elm/core with a different description ([https://github.com/elm/compiler/issues/1776](https://github.com/elm/compiler/issues/1776)).

---

<div class="post-metadata">

**Author:** ![berend](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/berend/32/2250_2.png) [@berend](https://discourse.elm-lang.org/u/berend)\
**Post date:** [October 9, 2019, 4:34am UTC](https://discourse.elm-lang.org/t/changing-subscriptions-when-url-changes/4451/4 "2019-10-09T04:34:00Z")

</div>

I have had severe problems with conditionals in subscriptions. They don’t work reliably. Might work in one app, but not in another. Don’t use them.

---

<div class="post-metadata">

**Author:** ![francescortiz](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/francescortiz/32/1721_2.png) [@francescortiz](https://discourse.elm-lang.org/u/francescortiz)\
**Post date:** [October 9, 2019, 5:14am UTC](https://discourse.elm-lang.org/t/changing-subscriptions-when-url-changes/4451/5 "2019-10-09T05:14:34Z")

</div>

What do you propose to do when you have 20 pages each one with different subscription needs?

---

<div class="post-metadata">

**Author:** ![berend](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/berend/32/2250_2.png) [@berend](https://discourse.elm-lang.org/u/berend)\
**Post date:** [October 9, 2019, 5:20am UTC](https://discourse.elm-lang.org/t/changing-subscriptions-when-url-changes/4451/6 "2019-10-09T05:20:15Z")

</div>

I think that’s an exception. That’s a case that actually works I think. I’m more thinking about subscriptions that depend on a state in the current model, that doesn’t work reliably for me. But yes, I have a case statement based on page in my main subscriptions, that works, but an if statement in there based on some property to enable something after a model change does not work reliably for me.

---

<div class="post-metadata">

**Author:** ![charlie](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.elm-lang.org/charlie/32/871_2.png) [@charlie](https://discourse.elm-lang.org/u/charlie)\
**Post date:** [October 15, 2019, 9:08pm UTC](https://discourse.elm-lang.org/t/changing-subscriptions-when-url-changes/4451/7 "2019-10-15T21:08:37Z")

</div>

Thanks everyone for your insights on this! I pushed a few updates informed by your suggestions. First, I tried removing the field in the model, and extracting the logic that had populated it to a function. This meant only the route needed to update, but [this change](https://github.com/armatures/dynamic-elm-subscriptions/commit/ea6f0b323de7ea6a780451772e3a67f31e005dde#diff-de84bd170bc37fbce0a7076c0125dd29R83) didn’t seem to make a difference.

Next, I added a `NoOp` `Msg` constructor that has no effect when triggered, and returned that from my subscription when the route is not set to listen to the keyboard. [This way](https://github.com/armatures/dynamic-elm-subscriptions/commit/00e594184195a6e5d095e199288f6e43a747c402), every keystroke returns a `Msg`. This was suggested in the bug documentation that @Carlsson87 linked.

This still didn’t seem to be working, so I [installed elm-live](https://github.com/armatures/dynamic-elm-subscriptions/commit/0229f6d5a0b713ad960f2c6c490089389f977b1c) to get debugger info about `Msg`s. The app _will_ start listening for the keyboard after the first keystroke. The first one returns a `NoOp`, and after that, it updates the `Msg` it sends back. Has anyone had any luck getting things to update more eagerly?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex035/uploads/elm_lang/original/1X/50a05e53677a2c3b47776d7abd0f113eb50193a1.png) [@system](https://discourse.elm-lang.org/u/system)\
**Post date:** [October 25, 2019, 9:10pm UTC](https://discourse.elm-lang.org/t/changing-subscriptions-when-url-changes/4451/8 "2019-10-25T21:10:42Z")

</div>

Closed on October 25, 2019, 9:10pm UTC

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.
