-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Add Node.js WebSocket server example #2115
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 2 commits
f5b49e8
f49c1e6
2427f70
3f22611
076d6ff
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,3 +6,54 @@ description: ngrok HTTP endpoints support WebSockets without any special configu | |
|
|
||
| WebSocket endpoints work through ngrok's HTTP endpoints without any changes. | ||
| However, there is currently no support for introspecting WebSockets beyond the initial `101 Switching Protocols` response. | ||
|
|
||
| ## What you'll need | ||
|
|
||
| - Node.js installed on your machine. | ||
| - An [ngrok account](https://dashboard.ngrok.com/signup). | ||
| - Your [ngrok authtoken](https://dashboard.ngrok.com/get-started/your-authtoken). | ||
|
|
||
| ## 1. Install dependencies | ||
|
|
||
| ```sh | ||
| npm install ws | ||
| ``` | ||
|
|
||
| ## 2. Create the server | ||
|
|
||
| HTTP and WebSocket share the same port. | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot combine 24-26 into one line that says "Create a
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 2427f70 — combined into a single "Create a |
||
|
|
||
| Create a `server.js` file: | ||
|
|
||
| ```js | ||
| import { createServer } from 'http'; | ||
| import { WebSocketServer } from 'ws'; | ||
|
|
||
| const server = createServer((req, res) => { | ||
| res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
| res.end('OK\n'); | ||
| }); | ||
|
|
||
| const wss = new WebSocketServer({ server }); | ||
|
|
||
| wss.on('connection', (ws) => { | ||
| ws.on('message', (data) => ws.send(`Echo: ${data}`)); | ||
| ws.send('Hello from WebSocket'); | ||
| }); | ||
|
|
||
| server.listen(8000); | ||
| ``` | ||
|
|
||
| A single ngrok endpoint handles traffic for both protocols. | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @copilot put line 47 after line 49
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done in 076d6ff — moved "A single ngrok endpoint handles traffic for both protocols." to after the |
||
|
|
||
| ## 3. Start ngrok | ||
|
|
||
| ```sh | ||
| ngrok http 8000 | ||
| ``` | ||
|
|
||
| Connect to your WebSocket using the `wss://` scheme: | ||
|
|
||
| ```js | ||
| const ws = new WebSocket('wss://<your-ngrok-domain>'); | ||
| ``` | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot add a line after this sentence that says "If you want to use WebSockets with HTTP, you only need one ngrok endpoint: you can start a server where listeners for both protocols share a single port."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done in 3f22611 — added the sentence after line 7.