WebSockets in PowerShell.
Get-WebSocket gets a websocket.
This will create a job that connects to a WebSocket and outputs the results.
If the -Watch
parameter is provided, will output a continous stream of objects.
Create a WebSocket job that connects to a WebSocket and outputs the results.
Get-WebSocket -WebSocketUri "wss://localhost:9669/"
Get is the default verb, so we can just say WebSocket.
-Watch
will output a continous stream of objects from the websocket.
For example, let’s Watch BlueSky, but just the text.
websocket wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=app.bsky.feed.post -Watch |
% {
$_.commit.record.text
}
Watch BlueSky, but just the text and spacing
$blueSkySocketUrl = "wss://jetstream2.us-$(
'east','west'|Get-Random
).bsky.network/subscribe?$(@(
"wantedCollections=app.bsky.feed.post"
) -join '&')"
websocket $blueSkySocketUrl -Watch |
% { Write-Host "$(' ' * (Get-Random -Max 10))$($_.commit.record.text)$($(' ' * (Get-Random -Max 10)))"}
EXAMPLE 4
websocket wss://jetstream2.us-east.bsky.network/subscribe?wantedCollections=app.bsky.feed.post
Watch BlueSky, but just the emoji
websocket jetstream2.us-east.bsky.network/subscribe?wantedCollections=app.bsky.feed.post -Tail |
Foreach-Object {
$in = $_
if ($in.commit.record.text -match '[\p{IsHighSurrogates}\p{IsLowSurrogates}]+') {
Write-Host $matches.0 -NoNewline
}
}
EXAMPLE 6
$emojiPattern = '[\p{IsHighSurrogates}\p{IsLowSurrogates}\p{IsVariationSelectors}\p{IsCombiningHalfMarks}]+)'
websocket wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=app.bsky.feed.post -Tail |
Foreach-Object {
$in = $_
$spacing = (' ' * (Get-Random -Minimum 0 -Maximum 7))
if ($in.commit.record.text -match "(?>(?:$emojiPattern|\#\w+)") {
$match = $matches.0
Write-Host $spacing,$match,$spacing -NoNewline
}
}
EXAMPLE 7
websocket wss://jetstream2.us-east.bsky.network/subscribe?wantedCollections=app.bsky.feed.post -Watch |
Where-Object {
$_.commit.record.embed.'$type' -eq 'app.bsky.embed.external'
} |
Foreach-Object {
$_.commit.record.embed.external.uri
}
BlueSky, but just the hashtags
websocket wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=app.bsky.feed.post -WatchFor @{
{$webSocketoutput.commit.record.text -match "\#\w+"}={
$matches.0
}
}
BlueSky, but just the hashtags (as links)
websocket wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=app.bsky.feed.post -WatchFor @{
{$webSocketoutput.commit.record.text -match "\#\w+"}={
if ($psStyle.FormatHyperlink) {
$psStyle.FormatHyperlink($matches.0, "https://bsky.app/search?q=$([Web.HttpUtility]::UrlEncode($matches.0))")
} else {
$matches.0
}
}
}
EXAMPLE 10
websocket wss://jetstream2.us-west.bsky.network/subscribe?wantedCollections=app.bsky.feed.post -WatchFor @{
{$args.commit.record.text -match "\#\w+"}={
$matches.0
}
{$args.commit.record.text -match '[\p{IsHighSurrogates}\p{IsLowSurrogates}]+'}={
$matches.0
}
}
The Uri of the WebSocket to connect to.
Type | Required | Position | PipelineInput | Aliases |
---|---|---|---|---|
[Uri] |
false | 1 | true (ByPropertyName) | Url Uri |
A ScriptBlock that will handle the output of the WebSocket.
Type | Required | Position | PipelineInput |
---|---|---|---|
[ScriptBlock] |
false | named | false |
Any variables to declare in the WebSocket job. These variables will also be added to the job as properties.
Type | Required | Position | PipelineInput |
---|---|---|---|
[IDictionary] |
false | named | false |
The name of the WebSocket job.
Type | Required | Position | PipelineInput |
---|---|---|---|
[String] |
false | named | false |
The script to run when the WebSocket job starts.
Type | Required | Position | PipelineInput |
---|---|---|---|
[ScriptBlock] |
false | named | false |
The buffer size. Defaults to 16kb.
Type | Required | Position | PipelineInput |
---|---|---|---|
[Int32] |
false | named | false |
The ScriptBlock to run after connection to a websocket. This can be useful for making any initial requests.
Type | Required | Position | PipelineInput |
---|---|---|---|
[ScriptBlock] |
false | named | false |
The ScriptBlock to run when an error occurs.
Type | Required | Position | PipelineInput |
---|---|---|---|
[ScriptBlock] |
false | named | false |
The ScriptBlock to run when the WebSocket job outputs an object.
Type | Required | Position | PipelineInput |
---|---|---|---|
[ScriptBlock] |
false | named | false |
The Scriptblock to run when the WebSocket job produces a warning.
Type | Required | Position | PipelineInput |
---|---|---|---|
[ScriptBlock] |
false | named | false |
If set, will watch the output of the WebSocket job, outputting results continuously instead of outputting a websocket job.
Type | Required | Position | PipelineInput | Aliases |
---|---|---|---|---|
[Switch] |
false | named | false | Tail |
If set, will output the raw text that comes out of the WebSocket.
Type | Required | Position | PipelineInput | Aliases |
---|---|---|---|---|
[Switch] |
false | named | false | Raw |
If set, will output the raw bytes that come out of the WebSocket.
Type | Required | Position | PipelineInput | Aliases |
---|---|---|---|---|
[Switch] |
false | named | false | RawByte RawBytes Bytes Byte |
If set, will watch the output of a WebSocket job for one or more conditions. The conditions are the keys of the dictionary, and can be a regex, a string, or a scriptblock. The values of the dictionary are what will happen when a match is found.
Type | Required | Position | PipelineInput | Aliases |
---|---|---|---|---|
[IDictionary] |
false | named | false | WhereFor Wherefore |
The timeout for the WebSocket connection. If this is provided, after the timeout elapsed, the WebSocket will be closed.
Type | Required | Position | PipelineInput |
---|---|---|---|
[TimeSpan] |
false | named | false |
The maximum number of messages to receive before closing the WebSocket.
Type | Required | Position | PipelineInput |
---|---|---|---|
[Int64] |
false | named | false |
The maximum time to wait for a connection to be established. By default, this is 7 seconds.
Type | Required | Position | PipelineInput |
---|---|---|---|
[TimeSpan] |
false | named | false |
The Runspace where the handler should run. Runspaces allow you to limit the scope of the handler.
Type | Required | Position | PipelineInput |
---|---|---|---|
[Runspace] |
false | named | false |
The RunspacePool where the handler should run. RunspacePools allow you to limit the scope of the handler to a pool of runspaces.
Type | Required | Position | PipelineInput | Aliases |
---|---|---|---|---|
[RunspacePool] |
false | named | false | Pool |
Get-WebSocket [[-WebSocketUri] <Uri>] [-Handler <ScriptBlock>] [-Variable <IDictionary>] [-Name <String>] [-InitializationScript <ScriptBlock>] [-BufferSize <Int32>] [-OnConnect <ScriptBlock>] [-OnError <ScriptBlock>] [-OnOutput <ScriptBlock>] [-OnWarning <ScriptBlock>] [-Watch] [-RawText] [-Binary] [-WatchFor <IDictionary>] [-TimeOut <TimeSpan>] [-Maximum <Int64>] [-ConnectionTimeout <TimeSpan>] [-Runspace <Runspace>] [-RunspacePool <RunspacePool>] [<CommonParameters>]