% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/stream.R
\name{stream_to_js}
\alias{stream_to_js}
\title{Write data to a \pkg{shidashi} stream binary file}
\usage{
stream_to_js(
  abspath,
  data,
  type = c("raw", "json", "int32", "float32", "float64"),
  ...
)
}
\arguments{
\item{abspath}{Character scalar. Absolute path to the target \code{.bin}
file.  Use \code{\link{stream_path}(id)} to obtain a path under the
app's \code{www/stream/} directory.}

\item{data}{The data to serialize.  See Details for how each \code{type}
interprets this argument.}

\item{type}{Character scalar. One of \code{"raw"}, \code{"json"},
\code{"int32"}, \code{"float32"}, or \code{"float64"}.}

\item{...}{Additional named scalar values to embed in the JSON header and
expose to the JavaScript caller via \code{result.header}.}
}
\value{
Invisibly returns \code{abspath}.
}
\description{
Serializes \code{data} together with a JSON header into the binary
envelope expected by the JavaScript \code{window.shidashi.fetchStreamData(id)}
method.
}
\details{
\strong{Wire format}
\preformatted{
  [endianFlag: 1 byte]  [headerLen: uint32 LE]  [header: UTF-8 JSON]  [body]
}
\code{endianFlag} is always \code{0x01} (little-endian).
\code{header} is a JSON object containing at minimum \code{data_type}
plus any extra fields passed via \code{...}.

\strong{Body encoding by type}
\describe{
  \item{\code{"raw"}}{Passed through verbatim; \code{data} must be a
    \code{raw} vector or will be coerced via \code{as.raw()}.}
  \item{\code{"json"}}{\code{data} is serialized with
    \code{jsonlite::toJSON(auto_unbox = TRUE)}.}
  \item{\code{"int32"}}{\code{data} is coerced to integer and written as
    4-byte little-endian signed integers.}
  \item{\code{"float32"}}{\code{data} is coerced to double and written as
    4-byte little-endian IEEE 754 single-precision floats.}
  \item{\code{"float64"}}{\code{data} is coerced to double and written as
    8-byte little-endian IEEE 754 double-precision floats.}
}
}
\seealso{
\code{\link{stream_path}}
}
