socket.io connect code example
Example 1: socket io script
<script src="/socket.io/socket.io.js"></script>
<script>
const socket = io();
</script>
Example 2: import socket io frontend
<script src="/socket.io/socket.io.js"></script><script> const socket = io('http://localhost');</script>
Example 3: how to run socket.io server
const express = require("express");
const http = require("http");
const socketIo = require("socket.io");
const port = process.env.PORT || 8001;
const index = require("./routes/index");
const app = express();
app.use(index);
const server = http.createServer(app);
const io = socketIo(server);
const getApiAndEmit = "TODO";
Example 4: socket io query
const socket = io({
query: { token: 'cde' }
});
io.on('connection', (socket) => {
let token = socket.handshake.query.token;
});
Example 5: socket.io doc
const socket = new WebSocket('ws://localhost:3000');socket.onopen(() => { socket.send('Hello!');});socket.onmessage(data => { console.log(data);});