Pluggable websockets behaviour #451

Closed
wants to merge 4 commits into
from

Projects

None yet

3 participants

@mmzeeman
Member
mmzeeman commented Nov 8, 2012

I changed the way websockets are setup and can be used. This update makes it possible to use your own websocket handlers in your controllers.

You can provide your own websocket_start similar too controller websocket, and pass a ws_handler containing the name of a websocket handler module in the Context.

Example:

websocket_start(ReqData, Context) ->
  Context1 = z_context:set(ws_handler, ?MODULE, Context),
  controller_websocket:websocket_start(ReqData, Context).

When passing a custom handler module, the default handler websocket will not be used, but the specified one. Controller websocket contains the code for the default zotonic handler. It attaches itself as websocket handler to the page session.

WebSocket Handler API

% Called when the websocket is initialized.
websocket_init(Context) 

% Called when a message arrives on the websocket.
websocket_message(Msg, Context)

% Called when another type of message arrives.
websocket_info(Msg, Context)

% Called when the websocket terminates.
websocket_terminate(Reason, Context)
@kaos
Member
kaos commented Nov 8, 2012

Really nice!

Just thinking, where does this fit into the docs?
I guess there ought to be a chapter about websockets/ajax under the in-depth section.

Would it be too much to ask to put the info you've already written in this pull request into a rst file along with this pull request?
Better to start with something, and fill it out later... :)

@arjan
Member
arjan commented Nov 8, 2012

I agree with @kaos

let's keep the motto: more features = more docs :-)

controller_websocket would be a good place document stuff like this, right?

@mmzeeman
Member
mmzeeman commented Nov 8, 2012

Lol. Not yet documented :-) Copy pasting the description.

I agree with @kaos

let's keep the motto: more features = more docs :-)

controller_websocket would be a good place document stuff like this, right?


Reply to this email directly or view it on GitHub.

@mmzeeman
Member
mmzeeman commented Nov 8, 2012

Fyi, this where some core zotonic changes I needed to implement a persistent connection module similar to bullet.

@kaos
Member
kaos commented Nov 9, 2012

👍

@arjan
Member
arjan commented Nov 9, 2012

I dont think the code example will display correctly, markdown is not RST...

should be done like this:

.. code-block:: erlang

   foo() ->
       bar.

http://sphinx-doc.org/markup/code.html

@arjan
Member
arjan commented Nov 9, 2012

Can this also be used from a dispatch rule?

Like this:

{customws, ["socket", "custom"], controller_websocket, [{ws_handler, my_ws_handler}]}

If so, that would be nice to document it like that.

I also think it's wise to give the complete source code of the custom WS controller (instead of the just its websocket_start function)

@mmzeeman
Member
mmzeeman commented Nov 9, 2012

+1 Brilliant, didn't even think about that one. That should work as ws_handler is paced in the context.

This probably needs an fully worked out example, but please wait a little for the complete bullet like solution because that one is even better.

@arjan
Member
arjan commented Nov 9, 2012

Oh so this is not ready for merge yet?

btw I saw your comment about github "magically" adding commits to the pull request.
Its not magic, but on github, a pull request is for an entire branch, not just for a commit. That's why it's good to create a topic branch and do a PR from that branch.

@mmzeeman
Member
mmzeeman commented Nov 9, 2012

Yes, this is ready. The other thing will be a separate module, but it needed this addition to the api.

Good to know. I worked from my own clone, created a branch locally. But for some reason I could not push that branch to github. So I merged the branch locally and then made PR. I still have to find the right git command to be able to push that branch I guess.

@mmzeeman
Member
mmzeeman commented Nov 9, 2012

Thanks, that was the missing part. I was working on the branch but when I
just did a git push.

I'll drop the PR and fix it next week. We are in the middle of moving
offices.

http://zotonic.com/docs/dev/contributing.html


Reply to this email directly or view it on GitHub:
#451 (comment)

@mmzeeman mmzeeman closed this Nov 9, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment