Michał Gajek - 75 - Aug 4, 2014

I’m trying to use SSE with my AngularJS project (no ss-utils.js used). Is there any way to prevent ServiceStack from sending all the “commands” (“cmd.onConnect” etc)? I’m using SSE in very narrow area and would like to keep it as simple as possible, I don’t need any client-side command dispatcher.

You always want ‘cmd.onConnect’ which happens only once as it contains the urls for auto heartbeats and unregistering. You can disable the other control commands like “cmd.onJoin” and “cmd.onLeave” by setting NotifyChannelOfSubscriptions = false
See https://github.com/ServiceStackApps/Chat#registering

Michał Gajek:

I’ve read both html5rocks sse tutorial as well as w3c specs. While I understand the idea of channels and subscriptions and the problems they address, I dont get why would I need to take care of heartbeats manually in my simple scenario (where sse is to be used for sending “live” output of console app being ran by logged in user via process.start). Neither of those concepts is covered (or at least the problem mentioned) in any of the sse sources. Besides I don’t get why does ServiceStack use “selector” (message data prefix?) instead of event type (which is part of standard, plus it doesn’t require manual client-sise dispatching)?

You don’t need to take care of manual heartbeats as ss-utils.js automatically registers and handles it for you when you $(source).handleServerEvents(). It needs to do the housekeeping because ASP.NET and HttpListener doesn’t get notified when a HTTP Client is no longer connected so will happily keep writing the non-existing response whilst the list of “active” connections keep growing. The “selector” is a messaging concept used to route the message to the appropriate handler. It’s not sending separate named DOM events because the selectors aren’t known ahead of time and it’s more efficient to register a single event listener than multiple event listeners to an unbounded list of selectors which is why ss-utils only listens to the default “message” event.

You can of course choose not use ss-utils and register it as a normal EventSource. 

Michał Gajek:

Thanks Demis, I’ve written a simple AngularJS, jQuery-independent version which takes care of heartbeat & unregistering when route / state is changing.  If anyone is interested: https://gist.github.com/migajek/dede8e9184a087e19bd9

1 Like

Very useful, thanks for posting it!!