1# WebSocket Connection 2 3 4## Use Cases 5 6You can use WebSocket to establish a bidirectional connection between a server and a client. Before doing this, you need to use the **createWebSocket()** API to create a **WebSocket** object and then use the **connect()** API to connect to the server. If the connection is successful, the client will receive a callback of the **open** event. Then, the client can communicate with the server using the **send()** API. When the server sends a message to the client, the client will receive a callback of the **message** event. If the client no longer needs this connection, it can call the **close()** API to disconnect from the server. Then, the client will receive a callback of the **close** event. 7 8If an error occurs in any of the preceding processes, the client will receive a callback of the **error** event. 9 10 11## Available APIs 12 13The WebSocket connection function is mainly implemented by the WebSocket module. To use related APIs, you must declare the **ohos.permission.INTERNET** permission. The following table describes the related APIs. 14 15| API | Description | 16| -------- | -------- | 17| createWebSocket() | Creates a WebSocket connection. | 18| connect() | Establishes a WebSocket connection to a given URL. | 19| send() | Sends data through the WebSocket connection. | 20| close() | Closes a WebSocket connection. | 21| on(type: 'open') | Enables listening for **open** events of a WebSocket connection. | 22| off(type: 'open') | Disables listening for **open** events of a WebSocket connection. | 23| on(type: 'message') | Enables listening for **message** events of a WebSocket connection. | 24| off(type: 'message') | Disables listening for **message** events of a WebSocket connection. | 25| on(type: 'close') | Enables listening for **close** events of a WebSocket connection. | 26| off(type: 'close') | Disables listening for **close** events of a WebSocket connection. | 27| on(type: 'error') | Enables listening for **error** events of a WebSocket connection. | 28| off(type: 'error') | Disables listening for **error** events of a WebSocket connection. | 29 30 31## How to Develop 32 331. Import the required WebSocket module. 34 352. Create a **WebSocket** object. 36 373. (Optional) Subscribe to WebSocket open, message, close, and error events. 38 394. Establish a WebSocket connection to a given URL. 40 415. Close the WebSocket connection if it is no longer needed. 42 43 ```js 44 import webSocket from '@ohos.net.webSocket'; 45 46 var defaultIpAddress = "ws://"; 47 let ws = webSocket.createWebSocket(); 48 ws.on('open', (err, value) => { 49 console.log("on open, status:" + JSON.stringify(value)); 50 // When receiving the on('open') event, the client can use the send() API to communicate with the server. 51 ws.send("Hello, server!", (err, value) => { 52 if (!err) { 53 console.log("Message sent successfully"); 54 } else { 55 console.log("Failed to send the message. Err:" + JSON.stringify(err)); 56 } 57 }); 58 }); 59 ws.on('message', (err, value) => { 60 console.log("on message, message:" + value); 61 // When receiving the `bye` message (the actual message name may differ) from the server, the client proactively disconnects from the server. 62 if (value === 'bye') { 63 ws.close((err, value) => { 64 if (!err) { 65 console.log("Connection closed successfully"); 66 } else { 67 console.log("Failed to close the connection. Err: " + JSON.stringify(err)); 68 } 69 }); 70 } 71 }); 72 ws.on('close', (err, value) => { 73 console.log("on close, code is " + value.code + ", reason is " + value.reason); 74 }); 75 ws.on('error', (err) => { 76 console.log("on error, error:" + JSON.stringify(err)); 77 }); 78 ws.connect(defaultIpAddress, (err, value) => { 79 if (!err) { 80 console.log("Connected successfully"); 81 } else { 82 console.log("Connection failed. Err:" + JSON.stringify(err)); 83 } 84 }); 85 ``` 86