Long Polling
ServiceStack services are generally meant for Request / Response type services. The easiest Comet-style / (aka HTTP Push) solution to implement would be using long-polling which I go into a bit of detail on how to do that in this ServiceStack group thread.
Keeping a worker thread open
ServiceStack also has support for a IStreamWriter
where you can return an object that writes directly to the HTTP Response Output stream, an example of this can be seen in this web service example service that writes an Image directly to a response stream. The issue here is that if you wanted to keep an open connection you would be blocking the HTTP Worker thread. Long-polling releases the connection after a short-time so
By-pass ServiceStack using your own Custom ASP.NET IHttpAsyncHandler
Finally another option would be to by-pass ServiceStack's web services for this specific task and just implement your own solution on top of ASP.NET's IHttpHandler
and IHttpAsyncHandler
. You can do this in ServiceStack by registering your own RawHttpHandlers
with this:
SetConfig(new EndpointHostConfig {
RawHttpHandlers = { httpReq =>
httpReq.PathInfo.StartsWith("/streaming")
? new MyStreamingHandler()
: null;
}
});
In your handler you can access your ServiceStack dependencies via the Singleton, e.g:
var myDep = EndpointHost.AppHost.TryResolve<IMyDependency>();