# actix_web::actix::actix::dev::StreamHandler - Rust
API documentation for the Rust `StreamHandler` trait in crate `actix_web`.
Keywords: rust, rustlang, rust-lang, StreamHandler

# Trait actix_web::actix::actix::dev::StreamHandler [−]

    pub trait StreamHandler<I, E>: Actor + Actor {
        fn handle(&mut self, item: I, ctx: &mut Self::Context);

        fn started(&mut self, ctx: &mut Self::Context) { ... }
        fn error(&mut self, err: E, ctx: &mut Self::Context) -> Running { ... }
        fn finished(&mut self, ctx: &mut Self::Context) { ... }
        fn add_stream<S>(fut: S, ctx: &mut Self::Context) -> SpawnHandle
        where
            E: 'static,
            I: 'static,
            S: Stream<Item = I, Error = E> + 'static,
            Self::Context: AsyncContext<Self>,
        { ... }
    }

Stream handler

This is helper trait that allows to handle `Stream` in a similar way as normal actor messages. When stream resolves to a next item, `handle()` method of this trait get called. If stream produces error, `error()` method get called. Depends on result of the `error()` method, actor could continue to process stream items or stop stream processing. When stream completes, `finished()` method get called. By default `finished()` method stops actor execution.

## Required Methods

### `fn handle(&mut self, item: I, ctx: &mut Self::Context)`

Method is called for every message received by this Actor

## Provided Methods

### `fn started(&mut self, ctx: &mut Self::Context)`

Method is called when stream get polled first time.

### `fn error(&mut self, err: E, ctx: &mut Self::Context) -> Running`

Method is called when stream emits error.

If this method returns `ErrorAction::Continue` stream processing continues otherwise stream processing stops. Default method implementation returns `ErrorAction::Stop`

### `fn finished(&mut self, ctx: &mut Self::Context)`

Method is called when stream finishes.

By default this method stops actor execution.

### `fn add_stream<S>(fut: S, ctx: &mut Self::Context) -> SpawnHandle where
E: 'static,
I: 'static,
S: Stream<Item = I, Error = E> + 'static,
Self::Context: AsyncContext<Self>,`

This method register stream to an actor context and allows to handle `Stream` in similar way as normal actor messages.

    use actix::prelude::*;
    use futures::stream::once;

    #[derive(Message)]
    struct Ping;

    struct MyActor;

    impl StreamHandler<Ping, io::Error> for MyActor {

        fn handle(&mut self, item: Ping, ctx: &mut Context<MyActor>) {
            println!("PING");
        }

        fn finished(&mut self, ctx: &mut Self::Context) {
            println!("finished");
        }
    }

    impl Actor for MyActor {
       type Context = Context<Self>;

       fn started(&mut self, ctx: &mut Context<Self>) {
           // add stream
           Self::add_stream(once::<Ping, io::Error>(Ok(Ping)), ctx);
       }
    }

## Implementors

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)