MCPcopy Create free account
hub / github.com/miguelgrinberg/python-socketio / emit

Method emit

src/socketio/async_client.py:205–249  ·  view source on GitHub ↗

Emit a custom event to the server. :param event: The event name. It can be any string. The event names ``'connect'``, ``'message'`` and ``'disconnect'`` are reserved and should not be used. :param data: The data to send to the server. Data

(self, event, data=None, namespace=None, callback=None)

Source from the content-addressed store, hash-verified

203 break
204
205 async def emit(self, event, data=None, namespace=None, callback=None):
206 """Emit a custom event to the server.
207
208 :param event: The event name. It can be any string. The event names
209 ``'connect'``, ``'message'`` and ``'disconnect'`` are
210 reserved and should not be used.
211 :param data: The data to send to the server. Data can be of
212 type ``str``, ``bytes``, ``list`` or ``dict``. To send
213 multiple arguments, use a tuple where each element is of
214 one of the types indicated above.
215 :param namespace: The Socket.IO namespace for the event. If this
216 argument is omitted the event is emitted to the
217 default namespace.
218 :param callback: If given, this function will be called to acknowledge
219 the server has received the message. The arguments
220 that will be passed to the function are those provided
221 by the server.
222
223 Note: this method is not designed to be used concurrently. If multiple
224 tasks are emitting at the same time on the same client connection, then
225 messages composed of multiple packets may end up being sent in an
226 incorrect sequence. Use standard concurrency solutions (such as a Lock
227 object) to prevent this situation.
228
229 Note 2: this method is a coroutine.
230 """
231 namespace = namespace or '/'
232 if namespace not in self.namespaces:
233 raise exceptions.BadNamespaceError(
234 namespace + ' is not a connected namespace.')
235 self.logger.info('Emitting event "%s" [%s]', event, namespace)
236 if callback is not None:
237 id = self._generate_ack_id(namespace, callback)
238 else:
239 id = None
240 # tuples are expanded to multiple arguments, everything else is sent
241 # as a single argument
242 if isinstance(data, tuple):
243 data = list(data)
244 elif data is not None:
245 data = [data]
246 else:
247 data = []
248 await self._send_packet(self.packet_class(
249 packet.EVENT, namespace=namespace, data=[event] + data, id=id))
250
251 async def send(self, data, namespace=None, callback=None):
252 """Send a message to the server.

Callers 12

sendMethod · 0.95
callMethod · 0.95
test_emit_namespaceMethod · 0.95
test_emit_binaryMethod · 0.95
test_emit_not_binaryMethod · 0.95

Calls 2

_send_packetMethod · 0.95
_generate_ack_idMethod · 0.45