enterlab / rente

Rente: Clojure, ClojureScript, Reagent (react) + Sente (web sockets). Heroku-ready!
http://enterlab.com
Eclipse Public License 1.0
57 stars 12 forks source link

:rente/testevent received as :chsk/recv #4

Closed tuhlmann closed 9 years ago

tuhlmann commented 9 years ago

In the rente example screen, when you push the button the send an event without a callback,

it sends a :rente/testevent to the server. The server receives it and supposedly sends a :rente/testevent back (ws.clj, event-msg-handler).

Now, on the client, this event is not received by event-msg-handler :rente/testevent but by event-msg-handler :chsk/recv. When commenting out the latter handler I see this message:

Unhandled event: [:chsk/recv [:rente/testevent {:message "Hello socket from server Event (no callback), received: {:message \"Hello socket Event!\"}"}]]

At some point the :rente/testevent gets wrapped into a :chsk/recv event.

Am I correct that this is a bug and the event should indeed be received by the :rente/testevent handler?

Maybe this is related to bug #2

luposlip commented 9 years ago

Hi tuhlmann,

Yes this is indeed a bug, and it is related to #2. I haven't invested a lot of time into this issue, so I'm not certain if it's a bug in my use of Sente, or if it actually a bug in sente.

I'm quite busy right now, so if you have the time to investigate I'll be happy if you do!

Best, Henrik

tuhlmann commented 9 years ago

I checked the sente source code (just grep for :chsk/recv), and it seems all push events are by default wrapped with that event. There's even a comment in there for a planned feature to make that optional.

In order to handle the event correctly, I receive the :chsk/recv event in a msg handler, unwrap the inner event and call a new event handler for user events:

(defmulti push-msg-handler :id)       ; Dispatch on user event key

(defmethod push-msg-handler :rente/testevent
  [{:as ev-msg :keys [event]}]
  (logf "rente/testevent received from server: %s " event))

(defmethod event-msg-handler :chsk/recv
  [{:as ev-msg :keys [?data event]}]
  (logf "Push event from server: %s" ?data)
  (push-msg-handler {:id (first ?data), :event (second ?data)}))

logf is a log method from encore.

Feel free to add this code.

This setup works for me and the message is passed to the right handler. But still that error from issue #2 is still appearing.

This error does not appear if I run the sente example app and make it return that same push event that rente returns. I'll further compare the setup between the example and rente and see if I find the difference.

tuhlmann commented 9 years ago

Peter replied on my inquiry regarding this and confirmed that user push events are always wrapped inside a :chsk/recv event. I simplified the above code a little bit into:

(defmulti push-msg-handler (fn [[id _]] id))       ; Dispatch on event key which is 1st elem in vector

(defmethod push-msg-handler :rente/testevent
  [[_ event]]
  (logf "rente/testevent received from server: %s " event))

(defmethod event-msg-handler :chsk/recv
  [{:as ev-msg :keys [?data event]}]
  (logf "Push event from server: %s" ?data)
  (push-msg-handler ?data))
luposlip commented 9 years ago

I've now had the time to look at your suggestions. It makes perfect sense! I've re-used your code, and will push an update shortly.

Thanks for your input! :+1: