- Socket.IO Cookbook
- Tyson Cadenhead
- 640字
- 2021-07-09 21:49:09
Creating a Node HTTP server with Socket.IO
In order to get Socket.IO running, we need to have at least one client and one server set up to talk to each other. In this recipe, we will set up a basic Node HTTP server with the built-in Node http
module.
Getting ready
To get started with Socket.IO, you will need to install Node.js. This can be downloaded from https://nodejs.org/.There is a download link on the Node.js website, or you can get one of the binaries at https://nodejs.org/download/.
Once Node.js is installed, you will need to navigate to the directory where your project is located and create a new NPM package by entering npm init
in your console.
Now, you will need to install Socket.IO. You can use NPM to install Socket.IO by entering npm install socket.io --save
on your terminal.
How to do it…
To create a Node HTTP server with Socket.IO, follow these steps:
- Create a new file called
server.js
. This will be your server-side code:var http = require('http'), socketIO = require('socket.io'), fs = require('fs'), server, io; server = http.createServer(function (req, res) { fs.readFile(__dirname + '/index.html', function (err, data) { res.writeHead(200); res.end(data); }); }); server.listen(5000); io = socketIO(server); io.on('connection', function (socket) { socket.emit('greeting-from-server', { greeting: 'Hello Client' }); socket.on('greeting-from-client', function (message) { console.log(message); }); });
- You may see that
server.js
will read a file calledindex.html
. You'll need to create this as well, as shown in the following code:<!DOCTYPE html> <html> <head> </head> <body> <script src="/socket.io/socket.io.js"></script> <script> var socket = io('http://localhost:5000'); socket.on('greeting-from-server', function (message) { document.body.appendChild( document.createTextNode(message.greeting) ); socket.emit('greeting-from-client', { greeting: 'Hello Server' }); }); </script> </body> </html>
- With your two files in place, you an start your server by entering
node server
on your terminal from the same directory where your files are. This will start a new Node server on port5000
. Node can listen on any port, but we will specifically tell it to listen on port5000
in ourserver.js
file. If you navigate tohttp://localhost:5000
, you should see a message that says Hello Client in your browser: - You should also see a message on your terminal with an object that contains a message that says 'Hello Server':
Congratulations! Your client and your server are now talking to each other.
How it works…
The browser displays a message that originated on the server, whereas the server displays a message that originated on the client. These messages are both relayed by Socket.IO.
The client side also initializes a function, but in the client's case, we need to pass a string containing the server and port number if the server is not running on port 80
. In our case, we will run the server on port 5000
, so we need to pass http://localhost:5000
in the io function.
The /socket.io/socket.io.js
file is served dynamically by Socket.IO, so you don't need to manually add this file anywhere. As long as your server and Socket.IO are set up correctly, the script will be present. There is also a CDN available to host your socket.io.js
file. The current version can be found at http://socket.io/download.
The io.on('connection')
method in the server-side code listens for any new client-side socket connections. When the client loads a page with Socket.IO on the client side, a new connection will be created here.
When the server gets a new socket connection, it will emit a message to every available socket that says Hello Client. When the client gets this message, it will render it to the DOM. It also emits a message of its own that the server will listen for.
There's more…
Although all the examples in this book use Node.js on the server side, there are server-side libraries for many other languages, including, PHP, C#, Ruby, Python, and so on. Whatever your server-side language of choice happens to be, there is likely to be a library to interface with Socket.IO on your server.
- 教孩子學編程:C++入門圖解
- Bootstrap 4:Responsive Web Design
- AutoCAD VBA參數化繪圖程序開發與實戰編碼
- 零基礎輕松學SQL Server 2016
- Java程序設計
- C語言程序設計實驗指導 (第2版)
- Unity UI Cookbook
- 區塊鏈架構之美:從比特幣、以太坊、超級賬本看區塊鏈架構設計
- C++從零開始學(視頻教學版)(第2版)
- C語言進階:重點、難點與疑點解析
- C語言從入門到精通(第4版)
- Mastering Android Application Development
- Sony Vegas Pro 11 Beginner’s Guide
- 競技游戲設計實戰指南:MOBA+RTS+TCG+FPS
- 精通Django 3 Web開發