anchor.diff:  -record(state, {channels, iorio}).
   
   init(_Transport, Req, Opts, _Active) ->
  -    io:format("listen init ~p~n", [Opts]),
  +    lager:info("listener init ~p~n", [Opts]),
       {ok, Req, #state{channels=[], iorio=iorio}}.
   
   get_channel_args(Msg) ->
  
positive.code:                         IsSubscribed = contains(Key, Channels),
                          if
                              IsSubscribed ->
                                  Iorio:unsubscribe(Bucket, Stream, self()),
                                  NewChannels = remove(Key, Channels),
                                  io:format("old ~p~nnew ~p~n", [Channels, NewChannels]),
                                  State1 = State#state{channels=NewChannels},
                                  {encode_ok(Id), State1};
  
                              true ->
                                 {encode_error(<<"not subscribed">>, Id), State}
  
                          end
                  end, Msg, Id, Req, State).
  
  encode_error(Reason, Id) ->
      IdBin = integer_to_binary(Id),
      <<"{\"ok\":false,\"id\":", IdBin, ",\"reason\":\"", Reason/binary, "\"}">>.
  
  stream(Data, Req, State) ->
      io:format("message received ~s~n", [Data]),
      Msg = jsx:decode(Data),
      Id = proplists:get_value(<<"id">>, Msg, 0),
      case proplists:get_value(<<"cmd">>, Msg) of
  
negative.code:     end.
  
  info({entry, BucketName, Stream, Entry}, Req, State) ->
      lager:debug("entry received ~s/~s~n", [BucketName, Stream]),
      Json = entry_to_json(Entry, BucketName, Stream),
      JsonBin = jsx:encode(Json),
      {reply, JsonBin, Req, State}.
  
  terminate(_Req, #state{channels=Channels, iorio=Iorio}) ->
      lists:map(fun ({Bucket, Stream}) ->
                        lager:debug("unsubscribing from ~s/~s~n", [Bucket, Stream]),
                        Iorio:unsubscribe(Bucket, Stream, self())
                end, Channels),
      ok.
  