Storybook Channel - A deprecated package that provides shims for the internal channels module used for communication between Storybook Manager and Renderer
—
Factory functions provide convenient ways to create pre-configured Channel instances for common use cases, particularly browser-based Storybook setups with appropriate transport combinations.
The primary factory function for creating browser-optimized channels with appropriate transports for Storybook Manager and Preview communication.
/**
* Creates a new browser channel instance with appropriate transports
* Automatically configures PostMessage transport and optionally WebSocket transport
* @param options - Configuration options for the channel
* @returns Configured Channel instance ready for browser use
*/
function createBrowserChannel(options: Options): Channel;
interface Options {
/** Page identifier - determines transport configuration */
page: 'manager' | 'preview';
/** Optional additional transports to include */
extraTransports?: ChannelTransport[];
}Usage Examples:
import { createBrowserChannel } from "@storybook/channels";
// Create channel for Storybook Manager
const managerChannel = createBrowserChannel({ page: 'manager' });
// Create channel for Storybook Preview
const previewChannel = createBrowserChannel({ page: 'preview' });
// Create channel with extra transports
const enhancedChannel = createBrowserChannel({
page: 'manager',
extraTransports: [
new CustomTransport({ endpoint: '/api/events' })
]
});
// Use the channel
managerChannel.addListener('story-changed', (data) => {
console.log('Story changed in manager:', data);
});
managerChannel.emit('request-story-list');The factory function automatically configures appropriate transports based on the page type and environment.
Default Transport Configuration:
For page: 'manager':
For page: 'preview':
Environment Detection:
// The factory function detects the environment and configures transports accordingly
// Development mode: PostMessage + WebSocket
// Production mode: PostMessage only
// Custom environments: PostMessage + extraTransportsCreating channels with custom transport combinations and configurations.
Custom Transport Integration:
import { createBrowserChannel, WebsocketTransport } from "@storybook/channels";
// Create channel with custom WebSocket configuration
const customChannel = createBrowserChannel({
page: 'manager',
extraTransports: [
new WebsocketTransport({
url: 'wss://custom-storybook-server.com/ws',
onError: (error) => console.error('Custom WebSocket error:', error),
page: 'manager'
})
]
});Multiple Transport Scenarios:
// Channel with multiple custom transports
const multiTransportChannel = createBrowserChannel({
page: 'preview',
extraTransports: [
new WebsocketTransport({
url: 'ws://localhost:9001',
onError: (error) => console.error('WebSocket connection error:', error),
page: 'preview'
}),
new CustomHttpTransport({ endpoint: '/events' }),
new CustomServerSentEventsTransport({ url: '/sse' })
]
});Utility function for determining the source URL of postMessage events, used internally by the PostMessage transport.
/**
* Determines the source URL of a postMessage event
* Used internally for validating and routing postMessage communications
* @param event - MessageEvent from postMessage listener
* @returns Source URL string or null if unable to determine
*/
function getEventSourceUrl(event: MessageEvent): string | null;Usage Examples:
import { getEventSourceUrl } from "@storybook/channels";
// Used internally by PostMessage transport
window.addEventListener('message', (event) => {
const sourceUrl = getEventSourceUrl(event);
if (sourceUrl && isAllowedOrigin(sourceUrl)) {
// Process the message
handleChannelMessage(event.data);
}
});
// Custom implementation example
function isAllowedOrigin(url: string): boolean {
const allowedOrigins = [
'http://localhost:6006',
'https://your-storybook.netlify.app'
];
return allowedOrigins.some(origin => url.startsWith(origin));
}Simplified Setup:
Consistency:
Extensibility:
extraTransports optionEnvironment Adaptation:
Install with Tessl CLI
npx tessl i tessl/npm-storybook--channels